php - JavaScript Arrays and MySQL -


i trying connect database php , put values returned mysql query javascript string array.

here code have far.

javascript code:

var nodressing = document.getelementbyid("temp1").innerhtml; var nodressingnew = nodressing.split(','); alert(nodressingnew); 

i alerting list debugging purposes.

here php code (i know connects 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 connect products database!</p>";         }         $result = mysqli_query($connect, "select * products options=''");         while($row = mysqli_fetch_array($result)){             $id = $row['id'];             $toprint .= $id . ",";         }         echo rtrim($toprint, ",");     ?> </div> 

this table looks in database:

id | name | description | price | type | options 

i of records have options value of "" (an empty string) , put ids list.

however, when run code, no alert box displayed @ all.

thanks,

it make more sense add these array, call implode() on array;

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

if there no alert box javascript error cause of specific issue, though may mean have problems further chain. start opening browser's developer tools pressing f12, go console , refresh page. check if errors appear in console, , try correct those.

try running sql on database directly before doing php ensure sql working intended. can use number of tools this. phpmyadmin, heidisql, or sqlyog come mind purpose. check if errors exist there.

if none exist sql, use var_dump display results of query web page(remove "display: none;" while doing this). make sure you're getting results sql. try var_dump in loop check you're getting correct values in loop itself.

finally, need ensure display directly on form html escaped avoid cross-site scripting attacks. can use htmlspecialchars this. echo string this:

echo htmlspecialchars($toprint); 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -