使用JS和PHP的AJAX,获取输入 [英] AJAX with JS and PHP, Getting Input Back
问题描述
我正在尝试使用此代码,但它不起作用。它适用于JS和PHP的AJAX。这是客户代码:
< !DOCTYPE html >
< html >
< head >
< script type = text / javascript >
function ajax()
{
if ( window .XMLHttpRequest)
{ // IE7 +,Firefox,Chrome,Opera,Safari的代码
xmlhttp = new XMLHttpRequest();
}
其他
{ // IE6代码,IE5
xmlhttp = 新 ActiveXObject( Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 )
{
document .getElementById( response)。innerHTML = xmlhttp.responseText;
}
}
var text = document .getElementById(< span class =code-string> text)。value;
xmlhttp.open( GET, server.php?q = + text, true );
xmlhttp.send();
}
< / 脚本 >
< / head >
< body >
< 表格 >
< < span class =code-leadattribute> input type = text id = text >
< 按钮 önclick = ajax() > 发送< / button >
< /表格 >
< p id = 响应 > < / p >
< / body >
< / html >
服务器代码:
<?php
$ q = $ _REQUEST [' q'跨度>];
echo $ q ;
?>
我需要它才能显示使用AJAX而不是任何其他方法按下按钮时在框中输入的文本。我打算将它用于实时消息服务。请帮忙。谢谢。
q =
_REQUEST [' q'];
echo
q ;
?>
我需要它才能显示使用AJAX而不是任何其他方法按下按钮时在框中输入的文本。我打算将它用于实时消息服务。请帮忙。感谢。
I am trying to use this code but it will not work. It is meant to be used for AJAX with JS and PHP. This is the client code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ajax()
{
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("response").innerHTML = xmlhttp.responseText;
}
}
var text = document.getElementById("text").value;
xmlhttp.open("GET","server.php?q=" + text, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" id="text">
<button önclick="ajax()">Send</button>
</form>
<p id="response"></p>
</body>
</html>
Server code:
<?php
$q = $_REQUEST['q'];
echo $q;
?>
I need it to display the text entered in the box when pressing the button using AJAX not any other method. I am going to use this for a live message service. Please help. Thanks.
q =
_REQUEST['q']; echo
q; ?>
I need it to display the text entered in the box when pressing the button using AJAX not any other method. I am going to use this for a live message service. Please help. Thanks.
这篇关于使用JS和PHP的AJAX,获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!