0

I have a PHP file that has some radio inputs on them which I wish to use to create a two variables (text string and a integer) via an AJAX (Not JQuery) call which I can use in the PHP file. I can work with a single variable which out puts to "getElementById" but as soon as I add a second, the code fails. 1/ How do I get more than one variable back from the AJAX? 2/ I can work with "getElementById" for collecting the new PHP variables but as I just want to set a couple of PHP variables for use later, I'm not sure this is the best method of collecting the response.

After reading around the web and Stackoverflow, I have got as far as the code below but no success.

<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("Div1").innerHTML="";
document.getElementById("Div2").innerHTML="";
return;
} 

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = (ajaxRequest.responseText).split(';',2);
var intVariable = response[0];
var StringVariable = response[1];
document.getElementById("Div1").innerHTML=xmlhttp.intVariable;
document.getElementById("Div2").innerHTML=xmlhttp.StringVariable;   
}
}
xmlhttp.open("GET","ajax_info.php?bookingstate="+str,true);
xmlhttp.send();
}
</script>


<form name='users'>
<radiogroup>
<input type="radio" name="user" value="1" onclick="showUser(this.value)" <?php if         ($select_state == '1') echo 'checked="checked"'; ?> >1
    <br>
    <input type="radio" name="user" value="2" onclick="showUser(this.value)" <?php if ($select_state == '2') echo 'checked="checked"'; ?> >2
<br>
<input type="radio" name="user" value="3" onclick="showUser(this.value)" <?php if     ($select_state == '3') echo 'checked="checked"'; ?> >3
<br>
</radiogroup>
</form>

And my ajax_info.php file

<?php

if ($bookingstate == "1") {
$select_state_name = "Booking state: 1";
$select_state1 = "1";


} elseif ($bookingstate =="2") {
$select_state_name = "Booking state: 2";
$select_state1 = "2";

} elseif ($bookingstate == "3") {
$select_state_name = "Booking state: 3";
$select_state1 = "3";

}

echo '$select_state1;$select_state_name;';

?>

1 Answer 1

1

you are settings those variables locally so

document.getElementById("Div1").innerHTML=xmlhttp.intVariable;
document.getElementById("Div2").innerHTML=xmlhttp.StringVariable; 

should just be

document.getElementById("Div1").innerHTML=intVariable;
document.getElementById("Div2").innerHTML=StringVariable; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked after changing 'var response = (ajaxRequest.responseText).split(';',2);' to ''var response = (xmlhttp.responseText).split(';',2);' as well.
ahhh, OK, that makes sense, glad I could help get you moving in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.