2013-12-13 99 views
0

我需要发送锚点标签上的文本框值点击checkID.php,并显示从PHP页面发送的HTML页面中的响应。我试过使用javascripts。但该页面被重定向到checkID.php页面。需要在相同的html页面中显示。如何从php页面获取响应?

<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页面的响应和显示?

+0

更具体,你想要什么? – Dev

+0

你能显示你的完整HTML代码吗? – skos

+0

我需要显示来自php代码的响应。这是我的php代码 – anipaul

回答

0

该代码不应该重定向到该页面。取消点击操作,看看是否有帮助。

onclick="fillContent('checkID.php');return false" 
+0

谢谢。我使用它,页面不重定向。但从PHP代码返回值不显示 – anipaul

+0

我的猜测是它返回一个错误,而不是成功。添加一个错误方法,看看它是否被调用。 – epascarello

+0

我使用了错误的方法。我发布了我的PHP脚本。你能检查它的正确性吗? – anipaul

-1

我建议你使用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> 
+0

什么是显示当我点击检查可用性 – anipaul

+0

检查是否有Ajax调用正在发生。您可以在浏览器控制台中看到它 –

+0

在控制台中运行时出错。 (“click”,function(){ – anipaul

1

不知道为什么它被重定向,但在你的PHP文件 -

代替

return $response; 

使用

echo $response; 

始终使用echo代替return in ajax cases

+0

我试过,但它不工作 – anipaul

+0

是否仍然重定向? – skos

+0

@anto - 您是否将'return'更改为' echo'在你的错误处理,以及它们都需要'echo' – webnoob