Ajax代码无法正常工作

问题描述:

(对不起,我的英语)。Ajax代码无法正常工作

我想做的是基本数据库插入。实际插入部分工作正常的问题是index.php插入后重定向process.php。但它应该停留在同一页面并清除所有字段我是什么做错了。

这是index.php文件

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 

</head> 
<body> 
<form action="process.php" id="myForm" method="post"> 
    UserName <input type="text" name="uname"><br> 
    Pass <input type="text" name="pass"><br> 
    FirstName <input type="text" name="fname"><br> 
    LastName <input type="text" name="lname"><br> 
    <button id="submit" value="register"></button> 


</form> 
<div id="ack">Ack</div> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script type="text/javascript" src="site.js"></script> 
</body> 
</html> 

这是site.js

$("#submit").click(function() { 

    $.post($("#myForm").attr("action"), 
     $("#myForm :input").serializeArray(), 
     function(info) { 

      $("#ack").empty(); 
      $("#ack").html(info); 
      clear(); 
     }); 

    $("#myForm").submit(function() { 
     return false; 
    }); 
}); 

function clear() { 

    $("#myForm :input").each(function() { 
     $(this).val(""); 
    }); 

} 

这是插入

<?php 
require("config.php"); 
$uname =$_POST["uname"]; 
$pass = $_POST["pass"]; 
$fname = $_POST["fname"]; 
$lname = $_POST["lname"]; 

$insert=$db->prepare("insert into users (uname,pass,fname,lname) values(?,?,?,?)"); 
$insert->bind_param("ssss",$uname,$pass,$fname,$lname); 
if($insert->execute()){ 
    echo "ok"; 
} 
else 
{$db->error;} 

?> 
+0

您没有提交来捕获,但您需要阻止点击的默认操作。 –

+0

**切勿存储纯文本密码!**请使用PHP的[内置函数](http://jayblanchard.net/proper_password_hashing_with_PHP.html)来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。确保你*** [不要越狱密码](http://stackoverflow.com/q/36628418/1011527)***或在哈希之前使用其他任何清理机制。这样做*更改密码并导致不必要的附加编码。 –

+0

感谢您的帮助和提示 –

你试试这样;

$("#myForm").submit(function() { 
    return false; 
    }); 

$("#submit").click(function() { 

$.post($("#myForm").attr("action"), 
    $("#myForm :input").serializeArray(), 
    function(info) { 

     $("#ack").empty(); 
     $("#ack").html(info); 
     clear(); 
    }); 
}); 

function clear() { 

    $("#myForm :input").each(function() { 
     $(this).val(""); 
    }); 
} 
+0

感谢您的帮助 –

+0

完全没有。来自土耳其的问候:) –