我需要在锚标记点击checkID.php上发送文本框值,并在html页面中显示从php页面发送的响应。我尝试使用javascript。但是页面被重定向到checkID.php页面。需要显示在同一个html页面中。
<a href="#" onclick="javascript:fillContent('checkID.php'); ">
Check Availability
</a>
在表单操作中,我指定为checkID.php。以下是我使用的javascript
<script language="javascript">
function fillContent(resource)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", resource, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4) {
el = document.getElementById('availabilityResponse')
el.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
</script>
下面是我的php代码
<?php
require_once('lib/nusoap.php');
$client=new nusoap_client("http://localhost/server.php?wsdl");
$error = $client->getError();
if ($error) {
return $error;
}
$alias=$_POST['id'];
$response = $client->call("checkAvailability", array("id" => $alias));
if($client->fault)
{
return $client->faultstring;
}
else
{
$error = $client->getError();
if ($error) {
return $error;
}
else {
return $response;
}
}
?>
如何在html页面中获取响应和显示?
不确定它被重定向的原因,但在您的PHP文件-中
代替
return $response;
使用
echo $response;
在ajax情况下始终使用echo
而不是return
该代码不应重定向到该页面。取消单击操作,看看这是否有帮助。
onclick="fillContent('checkID.php');return false"
我建议您使用jquery。它的非常简单
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$("#myID").on("click",function(){
$.ajax({ url: 'checkID.php',
type: 'post',
success: function(output) {
$("#availabilityResponse").html(output);
}
});
})
</script>
</head>
<body>
<a href="javascript:;" id="myID" ">Check Availability</a>
</body>
</html>