2015-09-23 30 views
0

如何發佈一些文本到一個PHP文檔,然後PHP文檔檢查它並返回響應?PHP與jQuery的反應?

在這個例子中檢查輸入值是否爲「test」,然後返回並提示TRUE否則返回並提醒FALSE。

HTML/JavaScript的(test_php_response.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test_response</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript"> 

function post_to_php() { 

$.post("test_response.php", function(data) { 
    if (data == true){ 
     alert("true"); 
    }else{ 
    alert("false"); 
    } 
}); 

} 

</script> 
</head> 


<body> 
<a href="javascript:post_to_php();" class='button'>click_here</a> 
<form name="response_form" id="response_form" action="test_response.php" method="POST"> 
     <input id="response_form_textbox" name="response_form_textbox" type="text" > 
    </form> 
</body> 
</html> 

PHP(test_response.php)

<?php 
$text_field = $_POST['response_form_textbox']; 
if ($text_field == 'test'){ 
    echo 'true'; 
}else{ 
    echo 'false'; 
} 
?> 

DEMO

----編輯解決/工作如下版本:-----

HTML/JavaScript的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test_response</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript"> 

function post_to_php() { 

$.post("test_response.php", $('#response_form').serialize(), function(data) { 
    if (data === "true"){ 
     alert("true"); 
    }else{ 
    alert("false"); 
    } 
}); 

} 

</script> 
</head> 


<body> 
<a href="javascript:post_to_php();" class='button'>click_here</a> 
<form name="response_form" id="response_form" action="test_response.php" method="POST"> 
     <input id="response_form_textbox" name="response_form_textbox" type="text" > 
    </form> 
    v.05 
</body> 
</html> 

PHP

<?php 
    $text_field = $_POST['response_form_textbox']; 
    if ($text_field == 'test'){ 
     echo 'true'; 
    }else{ 
     echo 'false'; 
    } 
    ?> 
+0

附加'方法 – aldrin27

+0

@ aldrin27'$ .post'是一個快捷包裝對於'$ .ajax'已經有方法設置 – charlietfl

+0

你的問題是什麼,具體是什麼?看起來你的示例代碼已經有99%了 - 你問你是否正確解析提交的字符串?如果jQuery正在工作?更具體的問題更容易回答。 – brichins

回答

2

你是不是送任何數據。 $.post的第二個參數是針對該數據的,但是您省略了它。

因此在您的php $_POST將是空的。

發送形式的數據的最簡單的方法是使用serialize()

$.post("test_response.php", $('#response_form').serialize(), function(data) { 
    if (data){ 
     alert("true"); 
    }else{ 
    alert("false"); 
    } 
}); 

參考:在JS'POST'`:

+0

用您的DEMO中的建議替換了我的代碼,並且還添加了console.log(data),現在我在日誌中獲得了返回false和true的正確信息,但它仍然只是提醒虛假 –

+0

檢查瀏覽器開發工具網絡中的實際請求。可以準確看到發送的內容和返回的內容。在php中做一個$ _POST的轉儲可以看到接收到的東西 – charlietfl

+0

ok現在已經完成「dump POST」並得到這個回報: array(1){[「response_form_textbox」] => string(4)「test」} true –