2013-12-13 116 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