I'm trying to pass two variables into this AJAX function, how would I go about doing that?
我试图将两个变量传递给这个AJAX函数,我该怎么做呢?
function showInfo(str)
{
if (str=="")
{
document.getElementById("txt").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)
{
document.getElementById("txt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","query1test.php?"+str,true);
xmlhttp.send();
And what I want tot pass into the AJAX requestion is lname, and year? Is there a way to go about that?
而我想要传递给AJAX请求的是lname和year?有办法解决这个问题吗?
<input type="text" name="lname" Onchange="showUser(this.value)"> <br>
<select name="year" >
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select> <br>
<input type="submit" value="submit">
Thanks for your help, I'm still a beginner.
谢谢你的帮助,我还是初学者。
2 个解决方案
#1
5
I would have used JQuery for this. It is much easier if you are using different browsers. The Ajax part (get request) is done with one line and you can use selectors to find the needed values.
我本来会用JQuery。如果您使用不同的浏览器会更容易。 Ajax部分(获取请求)使用一行完成,您可以使用选择器来查找所需的值。
In this bit of code, I set the onchange event for input named "lname". When the onchange event is triggered I get the value from the name field and the year from the select element. Then I send a get request to query1test.php
.
在这段代码中,我为名为“lname”的输入设置了onchange事件。当触发onchange事件时,我从name字段获取值,从select元素获取year。然后我向query1test.php发送一个get请求。
$(document).ready(function() {
$('input[name="lname"]').change(function() {
var name = $('input[name="lname"]').val();
var year = $('select[name="year"]').val();
$.get('query1test.php?name='+name+'&year='+year);
});
});
In the PHP script (query1test.php
) you can retrive the values from $_GET. For example:
在PHP脚本(query1test.php)中,您可以从$ _GET中检索值。例如:
<?php
$name = $_GET['name'];
$year = $_GET['year'];
echo $name . ' - ' . $year;
You might want to do something with the data returned from the PHP script. An example of that is:
您可能希望对PHP脚本返回的数据执行某些操作。一个例子是:
$.get('query1test.php?name='+name+'&year='+year, function(data) {
alert(data);
});
#2
0
I don't know what you're passing as str, but by the looks of things it is expected to be your query string - the very answer to your question. So in my response, I will replace str with what it would be without any validation:
我不知道你传递的是什么,但是通过事物的外观,它应该是你的查询字符串 - 你问题的答案。所以在我的回复中,我将用没有任何验证的情况替换str:
xmlhttp.open("GET","query1test.php?lname="+document.getElementById("lname").value+"&year="+document.getElementById("year").value,true);
And in addition I would give an id of lname to your text input and an id of year to your select.
此外,我会给你的文字输入一个id的id,给你的select一年的id。
<input type="text" id="lname" name="lname" Onchange="showUser(this.value)"> <br>
<select id="year" name="year" >
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select> <br>
<input type="submit" value="submit">
That would pass your request as $_GET vars.
这会将您的请求作为$ _GET vars传递。
Any reason why you would not want to use jQuery for this?
有什么理由你不想为此使用jQuery?
#1
5
I would have used JQuery for this. It is much easier if you are using different browsers. The Ajax part (get request) is done with one line and you can use selectors to find the needed values.
我本来会用JQuery。如果您使用不同的浏览器会更容易。 Ajax部分(获取请求)使用一行完成,您可以使用选择器来查找所需的值。
In this bit of code, I set the onchange event for input named "lname". When the onchange event is triggered I get the value from the name field and the year from the select element. Then I send a get request to query1test.php
.
在这段代码中,我为名为“lname”的输入设置了onchange事件。当触发onchange事件时,我从name字段获取值,从select元素获取year。然后我向query1test.php发送一个get请求。
$(document).ready(function() {
$('input[name="lname"]').change(function() {
var name = $('input[name="lname"]').val();
var year = $('select[name="year"]').val();
$.get('query1test.php?name='+name+'&year='+year);
});
});
In the PHP script (query1test.php
) you can retrive the values from $_GET. For example:
在PHP脚本(query1test.php)中,您可以从$ _GET中检索值。例如:
<?php
$name = $_GET['name'];
$year = $_GET['year'];
echo $name . ' - ' . $year;
You might want to do something with the data returned from the PHP script. An example of that is:
您可能希望对PHP脚本返回的数据执行某些操作。一个例子是:
$.get('query1test.php?name='+name+'&year='+year, function(data) {
alert(data);
});
#2
0
I don't know what you're passing as str, but by the looks of things it is expected to be your query string - the very answer to your question. So in my response, I will replace str with what it would be without any validation:
我不知道你传递的是什么,但是通过事物的外观,它应该是你的查询字符串 - 你问题的答案。所以在我的回复中,我将用没有任何验证的情况替换str:
xmlhttp.open("GET","query1test.php?lname="+document.getElementById("lname").value+"&year="+document.getElementById("year").value,true);
And in addition I would give an id of lname to your text input and an id of year to your select.
此外,我会给你的文字输入一个id的id,给你的select一年的id。
<input type="text" id="lname" name="lname" Onchange="showUser(this.value)"> <br>
<select id="year" name="year" >
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select> <br>
<input type="submit" value="submit">
That would pass your request as $_GET vars.
这会将您的请求作为$ _GET vars传递。
Any reason why you would not want to use jQuery for this?
有什么理由你不想为此使用jQuery?