如何發佈一些文本到一個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';
}
?>
----編輯解決/工作如下版本:-----
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';
}
?>
附加'方法 – aldrin27
@ aldrin27'$ .post'是一個快捷包裝對於'$ .ajax'已經有方法設置 – charlietfl
你的問題是什麼,具體是什麼?看起來你的示例代碼已經有99%了 - 你問你是否正確解析提交的字符串?如果jQuery正在工作?更具體的問題更容易回答。 – brichins