I am trying to pass 2 values to a AJAX
JQuery
function.
My function is triggered onChange
of a drop down list that which gets data from a DB and runs through a while
loop and outputs it with PHP
, it looks like this:
$dropDown = "<label for='drop_down_list'>Drop Down: </label>
<select name='drop_down_list' id='drop_down_list' onChange='getInfo(this.value, ". $val2 .")'>
<option selected='selected' disabled='disabled' value=''>Options</option><br/>
" . $dropDownOptions . "
</select></br>";
echo $dropDown;
Here I am trying to send the selected options value, as well as a value I have in a PHP variable (which does have a value because I echo it out elsewhere).
My getinfo
function looks like this:
function getInfo(val1, val2) {
$.ajax({
type: "POST",
url: "get_info.php",
data: 'value1='+val1+'&value2'+val2,
success: function(data){
$("#info").html(data);
}
});
};
I try to output the values I return from get_info.php like this:
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
echo "First Value: " .$value1;
echo "<br>";
echo "Second Value: " .$value2;
My output is:
First Value: 1
Second Value:
I have error reporting on and I am getting an undefined error on $value2
so I think somewhere that PHP variable is not passing properly, I'm not quite sure if that is how I am supposed to properly pass the PHP value I need, am I doing something wrong here?
How can I pass my PHP
variable into an AJAX
JQuery
function and and get the output the values properly?
example:
First Value: 1
Second Value: 2