繁体   English   中英

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

[英]Prevent redirect after successful AJAX Post

我已经玩了一段时间了,今晚没有成功,所以我知道我会被你们中的一些人弄得一团糟,但是一点帮助可以节省我最后一小时的睡眠时间。 在成功进行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的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;



?>

你使用id作为按钮并使用class作为按钮的选择器,你的选择器是错误的,使用id选择器,更改:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM