ajax不返回php索引页
请注意,我正在尝试使用ajax和mysql从index.php/html跳转到test.php页面,如果在表中找不到文本输入,则很简单,所以它应该转到test.php,否则将使用ajax警报停留在index.php/html页面上,但每次从索引页面接收不可用,有时提交按钮不起作用,下面是代码FYRajax不返回php索引页,php,mysql,ajax,Php,Mysql,Ajax,请注意,我正在尝试使用ajax和mysql从index.php/html跳转到test.php页面,如果在表中找不到文本输入,则很简单,所以它应该转到test.php,否则将使用ajax警报停留在index.php/html页面上,但每次从索引页面接收不可用,有时提交按钮不起作用,下面是代码FYR //index.php $(document).ready(function() { //default form not submitted $("
//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>
//index.php$(document).ready(function(){
//默认表单未提交
$(“提交”)。数据(“已提交”,错误);
//防止输入字段中的enter键提交表单
$(“欢迎”)。关于(“提交”,功能(e){
if($('#submit')。数据(“已提交”)==false){
e、 预防默认值();
}
});
//触发表单提交
$(“#提交”)。在(“单击”,函数(){
验证();
});});
//默认表单未提交
//$(“#提交
函数验证(){
var num=$('#invst_num').val();
$.ajax({
键入:“POST”,
url:“check_test.php”,
数据:num,
cache:false,
数据类型:“json”,
成功:功能(数据){
如果(数据){
警报(“不可用”);
}否则{
$(“提交”)。数据(“已提交”,真实);
$(“#欢迎”).submit();
}
}}
输入存货#*:
//检查_test.php您的条件是向后的
if(data){
应该是
if(!data){
或者,应在侦听器添加逻辑的情况下翻转警报
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
ajax调用从edata
key发送值,因此只发送文本字段内容
您可能应该发送这样的邮件:
$.ajax({
type: 'POST',
url: 'check_test.php',
data: { 'invst_num': num },
...
在浏览器中打开开发工具,检查ajax调用期间发送的内容。警告:如果您只是在学习PHP,请不要学习过时的mysql\u查询接口。
接口非常糟糕,在PHP 7中已被删除。类似的现代替代品。类似的指南有助于解释最佳实践。始终确保您的ser参数是因为您在这段代码中有严重错误。感谢您的回答,我也更改了“数据”,现在仍然会弹出不允许进入索引页的警报,我进一步注意到,在单击submit按钮时,DEV工具显示以下两条警告消息,使用/@表示sourceMappingURL pragmas已被弃用。请使用/#而不是jquery.min。js:4:0 getPreventDefault()的使用已弃用。请改用defaultPrevented。jquery.min.js:2:8283As@Iwnnay说条件肯定是错误的-您正在等待接收数据,但如果收到数据,则会发送警报。我的网站上很少有通知:使用http响应代码指示是否有任何错误,因此在本例中,200是可以的,400是错误的。请在js中根据此进行验证。有关更多信息,请看,您仍然没有编写发布的数据和响应是什么-在您的开发工具中启用“保留日志”之类的功能,并检查实际发生的情况,谢谢lwnnay:)我想知道将条件语句更改为“如果(!数据”){'但是,在编译显示阻止函数警告的开发工具时,当两次单击submit按钮时,表单转到next index.php页面,而不显示alert if语句false。