0

I am trying to connect to a database with PHP and then put all the values returned from the MySQL query into a JavaScript string array.

Here is the code that I have so far.

JavaScript Code:

var noDressing = document.getElementById("temp1").innerHTML;
var noDressingNew = noDressing.split(',');
alert(NoDressingNew);

I am alerting the list for debugging purposes.

Here is the PHP Code (I know that it connects to the database successfully):

<div id="temp1" style="display: none;">
    <?php
        include "/home/pi/config.php";
        $toPrint = "";
        $connect = mysqli_connect("localhost", $name, $pass, "items");
        if(mysqli_connect_errno()){
            echo "<p>Failed to connect to products database!</p>";
        }
        $result = mysqli_query($connect, "SELECT * FROM products WHERE options=''");
        while($row = mysqli_fetch_array($result)){
            $id = $row['id'];
            $toPrint .= $id . ",";
        }
        echo rtrim($toPrint, ",");
    ?>
</div>

This is what my table looks like in the database:

ID | NAME | DESCRIPTION | PRICE | TYPE | OPTIONS

I would like to get all of the records that have an OPTIONS value of "" (an empty string) and then put their IDs into a list.

However, when I run this code, no Alert Box is Displayed at all.

Thanks,

6
  • How are you executing your JavaScript code? Show more of that. Commented Apr 1, 2015 at 14:08
  • Have you checked the console for JS errors? Perhaps the code is running before the DOM is ready? Commented Apr 1, 2015 at 14:10
  • The code is being executed when a HTML number input is changed. Commented Apr 1, 2015 at 14:11
  • The JavaScript console on Chrome says: Uncaught ReferenceError: NoDressingNew is not defined Commented Apr 1, 2015 at 14:12
  • Well, you have written noDressingNew and then NoDressingNew. :) JS is case sensitive. Commented Apr 1, 2015 at 14:13

2 Answers 2

1

It would make more sense to add these to an array, then call implode() on the array;

$toPrint = array();
while($row = mysqli_fetch_array($result)){
    $toPrint[] = $row['id'];
}
$toPrint = implode(',', $toPrint);

If there is no alert box it is likely that a Javascript error is the cause of that specific issue, though it may mean that you have problems further up the chain. Start by opening your browser's Developer tools by pressing F12, then go to the Console and refresh the page. Check if any errors appear in the console, and try to correct those.

Try running your SQL on the database directly before doing it with PHP to ensure that your SQL is working as intended. You can use any number of tools for this. PHPMyAdmin, HeidiSQL, or SQLYog come to mind for this purpose. Check if any errors exist there.

If none exist with your SQL, use var_dump to display the results of your query to the web page(remove the "display: none;" while doing this). Make sure that you're getting results from SQL. Try var_dump in your loop to check that you're getting the correct values in the loop itself.

Finally, you need to ensure that anything you display directly on the form is properly HTML escaped to avoid Cross-site scripting attacks. You can use htmlspecialchars for this. Echo your string like this:

echo htmlspecialchars($toPrint);
Sign up to request clarification or add additional context in comments.

1 Comment

Nevermind, the problem was a spelling typo. Thanks for the tips on htmlspecialchars though.
0

Here is how to do it with the open source project http://www.jinqJs.com

var result = jinqJs().from($result).where('OPTIONS == ""').select('ID');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.