Reputation: 1191
When a hyperlink 'Add More' is clicked, a dropdown list is added using jquery. I want the values in this dropdown to be generated from the mysql database using php. I tried many ways,but i not able to get the values from php to jquery.. The values retrieved from mysql database using while loop should be displayed in the dropdown using jquery. Thanks
The values in $val should be displayed in dropdown options Here is the sample
php code retrieving values from database
<?php
$aquery = "SELECT * from filter";
$aresult =$base->execute_query($aquery);
while($row=mysql_fetch_array($aresult))
{
$val=$row[1];
}
?>
jquery where $val values should be added in dropdown
<script type="text/javascript">
jQuery(document).ready(function($){
$('.my-form .add-box').click(function(){
var box_html = $('<select><option><?php echo $val; ?></option>');
$('.add-box:last').before(box_html);
return false;
});
});
</script>
Please help me.Thanks
Upvotes: 4
Views: 1214
Reputation: 123
The PHP Script is actually over-writing on your $val.
Perhaps you can do the following:
<?php
$val = "";
while($row = mysql_fetch_array($aresult)) {
$val .= "<option value=". $row[1] .">". $row[1] ."</option>";
}
?>
and your JavaScript code should suffice in this case, just remove the option tags before the php script. Hope this helps.
var box_html = $('<select><?php echo $val; ?></select>');
Upvotes: 1
Reputation: 878
php will build you an array
you need to convert it to java object json_encode
try var abc=
also when your printing it you need to run a loop through the object after creating the select section
see each() jquery
Upvotes: 0
Reputation: 16436
If you want this on button click as your code then try this solution:
var box_html = '<select><?php foreach ($val as $value) {
echo "<option>$value</option>" ;
}?>';
Upvotes: 2