I am currently working with AJAX/JS to auto submit form. The function works well with text input fields but with a select box is not working well. Is there a way to get the JS function to echo the value of the selected item? EXAMPLE
AJAX/JS submit function
<script>
$(document).ready(function() {
var timer = null;
var dataString;
function submitForm(){
$.ajax({ type: "POST",
url: "test1.php",
data: dataString,
success: function(result){
$('#condition_input').text( $('#resultval', result).html());
}
});
return false;
}
$('#condition').on('keyup', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 1000);
var condition = $("#condition").val();
dataString = 'condition='+ condition;
});
});
</script>
Html/PHP
if (isset($_POST['condition'])){
$condition = $_POST['condition'];
echo ('<div id="condition"><span id="resultval"><h2>Description:</h2>'.$condition.'</span></div>');
}
<select name="condition" id="condition" value="<? $condition ?>" >
<option value=""></option>
<option value="new">New</option>
<option value="used">Used</option>
<option value="not Working">Not Working or for parts</option>
</select>
You have to bind the “on change”-event:
And your markup is not correct. You can not set a value attribute on select element. To select the correct value you have to loop over the options and mark the correct one by setting the attribute “selected”: