簡體   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