douyu7618 2014-02-09 18:59
浏览 41
已采纳

AJAX,PHP和多个变量(不是JQuery)

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条回答 默认 最新

  • dongmou1964 2014-02-09 19:09
    关注

    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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)