請注意,即時通訊嘗試從index.php/html跳轉到test.php頁面使用ajax和mysql,如果在表中找不到文本輸入,那麼它應該去test.php else留在阿賈克斯警報的index.php/html頁面上,但是從索引頁,每次接收不可用,有時提交按鈕不起作用,下面的代碼FYR ...ajax不返回到PHP索引頁
//index.php $(document).ready(function() {
//default form not submitted
$("#submit").data("submitted",false);
//preventing enter key in input field from submitting form
$("#welcome").on("submit", function(e){
if($('#submit').data("submitted")===false) {
e.preventDefault();
}
});
//trigger form submission
$("#submit").on("click",function(){
validate();
});});
//default form not submitted
//$("#submit
function validate() {
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
cache: false,
dataType: "json",
success: function(data) {
if(data){
alert("NOT AVAILABLE");
} else {
$("#submit").data("submitted", true);
$("#welcome").submit();
}
}}</script> <form action="check_test.php" method="post" name="welcome" id="welcome" /> <table width="550" border='0' align='center' cellpadding='0' cellspacing='0'> <tr>
<td align="center"><label>
Enter Inv. # *:
<input name="invst_num" type="text" id="invst_num" size="40" />
<input name="submit" type='submit' id='submit' /> </label></td> </tr></table> </form>
//check_test.php <?php
include ("config/config.php");
//get the username
if (isset($_POST['submit'])) {
$invst_num = $_POST['invst_num'];
//mysql query to select field username if it's equal to the username that we check '
$sql = mysql_query("select invst_num_ar from shareholders_ar where invst_num_ar = '$invst_num' ");
if ($result = mysql_num_rows($sql)>0) {
echo ($result);
}
}
?>
// if not found...test.php should load
<html>
<form
...
Register Data
/form>
</html>
**警告**:如果您只是學習PHP,請不要學習過時的'mysql_query'界面。這很糟糕,並且已經在PHP 7中被刪除。像[PDO這樣的現代替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo - 用於數據庫訪問/)。像[PHP The Right Way](http://www.phptherightway.com/)這樣的指南可以幫助解釋最佳實踐。因爲你有嚴重的[SQL注入漏洞](http://bobby-tables.com/),所以一定要絕對**確定**你的用戶參數[妥善轉義](http://bobby-tables.com/php)在這個代碼中。 – tadman