gpt4 book ai didi

php - 防止 AJAX Post 成功后重定向

转载 作者:行者123 更新时间:2023-11-29 08:02:50 24 4
gpt4 key购买 nike

我已经在这个问题上玩了一段时间了,今晚没有成功,所以我知道我会被你们中的一些人折磨,但一点帮助将挽救我最后一个小时的 sleep 。我需要防止我的页面在成功的 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>

and below is the PHP file for processing the mysql thingy

<?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){
...

关于php - 防止 AJAX Post 成功后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com