I’m using jQuery to send an AJAX request after a selection is made on a form, and I want echo back different things into a div depending on what selection is made. Here is my form:
<form name="myform" id="myform" method="post" action="#">
<select name="myselect" id="myselect">
<option>-- Make a selection --</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</form>
Here is the javascript earlier in the page:
$(document).ready(function() {
$('#myselect').change(function() {
$(this).parents("form").submit();
});
$('#myform').submit(function() {
$.post(
'myscript.php',
$(this).serialize(),
function(data){
$("#mydiv").html(data)
}
);
return false;
});
});
And here is myscript.php:
<?php
if ($_POST['myselect'] = "1") {
echo "Div contents 1";
}
if ($_POST['myselect'] = "2") {
echo "Div contents 2";
}
?>
My problem is this: ‘Div contents 1Div contents 2’ gets echoed into mydiv after I make any selection on the form. I was trying to get it to echo ‘Div contents 1’ if option 1 is selected and ‘Div contents 2’ if option 2 is selected.
Thanks and apologies for the long question.
The code :
and
Will always evaluate to true, since its assignment.
You need to use :