So ill start off with my code:
$(function() {
$("#employeeName").keyup(function() {
var dataString = 'find='+ $("#employeeName").val();
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "finduser.php",
data: dataString,
success: function(response) {
//On success
$("#search").html(response);
}
});
return false;
});
});
That is my ajax to search for an employees name
My html looks similar to this:
<input type='text' id='employeeName' name='employeeName'>
<select id='search' name='id'></select>
<input type='submit'>
Basically, it searches the database and returns <option>‘s with the user’s id as the value, and their name as the name, example: <option value='5'>John Doe</option>
And this all works great, except that when I select a name and submit the form, it doesn’t send the updated option that the person selected, it just sends a blank select(So $_POST['id'] doesn’t exist and throws an error.
How else could I make this work? I tried adding onchange="$('#employeeName').val($(this).val())" to the select so that when they select a name it updates the search box and they can submit the form that way, but that was to no avail..
If
$_POST['id']doesn’t exist, then your option likely doesn’t have avalue.If
$('#search').val()yields an integer, but it isn’t there when you submit the form, then either your<select>is not a part of the form, or the value gets updated on submit, or you’re submitting aGETrequest, so that your value is available through$_GET['id'].method="POST"$('#the-form').serialize()contains “id=5”.