成功完成AJAX Post后防止重定向

时间:2014-04-29 03:08:50

标签: php jquery mysql ajax

我现在已经玩了一段时间了,今晚没有成功,所以我知道我会被你们中的一些人弄得一团糟,但是一点帮助可以挽救我最后一小时的睡眠。在成功进行AJAX调用后,我需要阻止我的页面重定向。这是我的代码:

<!doctype html>
<html>
<head>
<title>Registration</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, width=device-width,    height=device-height" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/calls.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".regsubmit").click(function(e){
    e.preventDefault();
                $.ajax({
                    type: "GET",
                    url: "http://localhost/sc/new/lib/verifyreg.php", 
                    data: {
                            cell: $("#cell").val(),
                            name: $("#name").val(),
                            email: $("#email").val(),
                            country:     $("#country").val(),
                            },
                    timeout: 6000, 
                    success: function returndata(){
                        alert('congratulations!');
                                },
                    error: function() { 
                        alert('Server connection failed! Retry or check your device.'); 
                        },
                });                                                 
            });
});

</script>
</head>
<body>
<form name="registrationform" id="registrationform"     action="http://localhost/sc/new/lib/verifyreg.php" method="GET">
<input type="tel" name="cell" id="cell" placeholder="Your Cell Number" />
<input type="text" name="name" id="name" placeholder="Full Name" />
<input type="text" name="email" id="email" placeholder="Your Email" />
<select name="country"> 
    <option value="" selected="selected">Select Country</option> 
      <option value="United States">United States</option> 
      <option value="United Kingdom">United Kingdom</option> 
   </select>
    <button class="submit" name="submit" id="regsubmit">Submit</button>
</form>


</body>
</html>
  

及以下是用于处理mysql thingy的PHP文件

<?php
header('Access-Control-Allow-Origin: *');
 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

//$dest = $_REQUEST['number'];
$cell = $_POST['cell'];
$name = $_POST['name'];
$email = $_POST['email'];
$country = $_POST['country']; 


$link = mysql_connect("localhost", "drumbeat24", "@An716288");
mysql_select_db("snoopc", $link);


 $sql = "INSERT INTO snoopers(cell, name, email, country) VALUES ('$cell', '$name',      '$email', '$country')";


$result = mysql_query($sql) or die ('Unable to run query:'.mysql_error());
echo $cell;



?>

1 个答案:

答案 0 :(得分:2)

您正在使用id for button并使用class作为按钮的选择器,您的选择器错误,请使用id选择器,更改:

$(".regsubmit").click(function(e){
...

$("#regsubmit").click(function(e){
...