peter-cooper
asked on
How do I update select listbox after search
Hi
I have code that searches mysql from button click and should then place the data into a select listbox,(#boxdest). However, there is no data being entered into the list even though when I alert(data) I can see the result. There will only be 1 result expected from query.
I know that I probably need to code something along the lines of append option, but unsure how to code such hence my post. Thanks for any help.
html Button
html select
jquery code
php code
I have code that searches mysql from button click and should then place the data into a select listbox,(#boxdest). However, there is no data being entered into the list even though when I alert(data) I can see the result. There will only be 1 result expected from query.
I know that I probably need to code something along the lines of append option, but unsure how to code such hence my post. Thanks for any help.
html Button
Search for a box<br /><input type="text" id="srcBox" /><input type="button" id="srcsubmit" Value="Search" />
html select
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("sample",$conn);
$result = mysql_query("SELECT * FROM boxes where department = '{$_GET['dept']}' and status = 1 ORDER BY custref ASC");
?>
<select name="boxdest[]" id="boxdest" size="7" multiple="multiple">
<?php
$i=0;
while($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row["custref"];?>"><?php echo $row["custref"];?></option>
<?php
$i++;
}
?>
</select>
jquery code
<script type="text/javascript">
$(function() {
$(function() {
$('#srcsubmit').click(function(e) {
e.preventDefault();
var value = $('#srcBox').val();
//alert(value);
var qString = 'sub=' + value;
//alert(qString);
$.post('sub_db_handler.php', qString, processResponse);
});
function processResponse(data) {
//$('#resultsGoHere').html(data);
$('#boxdest').empty();
$('#boxdest').append(data);
//alert(data);
}
});
});
</script>
php code
<?php
$con = mysql_connect("localhost","root","");
if(!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("sample", $con);
$sub = $_POST['sub'];
$query = "SELECT * FROM boxes WHERE department = 'WILLS AND DEEDS' AND status = 1 AND custref = '$sub'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$r = $row['custref'];
echo json_encode($r);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER