Skip to main content
Participant
May 25, 2010
Question

Dynamic Dropdown / PHP / MySQLi / Javascript Problem

  • May 25, 2010
  • 1 reply
  • 1397 views

I have 2 pages, namely main.html and list.php The Main Pageincludes the following code and consists of a dropdown that i want to pull some database records into by calling the function fillCategory contained in list.php

Content on main.html


********************

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Page Title</title>
<script language="javascript" src="list.php"></script>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();">
<FORM name="drop_list" action="yourpage.php" method="POST" >
<SELECT NAME="Category" >
<Option value="">Category</option>
</SELECT> 
</form>
</body>
</html>

Content of list.php
*******************

<?php

require_once('mysqli_connect.php');
echo "
function fillCategory(
";

$q="select * from category";

echo mysqli_error($dbc);

$r=@mysqli_query($dbc,$q);

while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo "addOption(document.drop_list.Category, '$row[cat_id]', '$row[category]');";
}

?>


I know that records are being pulled from the MySQL database since when i view 127.0.0.1/list.php i can see the following diplayed on the page:

function fillCategory( addOption(document.drop_list.Category, '1', 'Sport');addOption(document.drop_list.Category, '2', 'Music');addOption(document.drop_list.Category, '3', 'Art');

which are the contents (Sport, Music, Art) that i'd like to appear in my dropdown. However, i suspect that i shouldn't see the above being displayed in list.php Instead, it should probably be passed to main.html "unseen". Can anyone point out the probable error in my syntax. I can provide more detail on the database structure if it will help.

Thanks,
Mark
This topic has been closed for replies.

1 reply

Randy Edmunds
Adobe Employee
Adobe Employee
May 30, 2010

I think that this is the line in error:

<script language="javascript" src="list.php"></script>

The <script> tag is generally used to include client-side script. I believe that PHP will recognize on the server-side, but you need to change "javascript" to "php". You may want to use a PHP include or require statement, instead.

Also, you might need to remove the "<?php" and "?>" delimiters from list.php.

HTH,

Randy