gpt4 book ai didi

javascript - 通过 Ajax 成功将数据插入 mysql 后无法重定向

转载 作者:行者123 更新时间:2023-11-29 01:55:41 25 4
gpt4 key购买 nike

我使用ajax、php 向mysql 数据库中插入数据。但是,在执行完所有查询后,它什么也不做。我的意思是,当我点击表单中的提交按钮时,作为一个用户,我没有看到任何事情发生,即使我设置了我的 Ajax 函数来提醒数据。但是在后端,所有数据都被正确地插入到数据库中。

因此,我不确定如何通知用户表单提交成功。有人可以帮忙吗?提前谢谢你。

我在这里提到过,但没有帮助:Page redirect with successful Ajax request

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
var xmlhttp = false;
try
{
xmlhttp = new ActiveXObject(Msxml2.XMLHTTP);
//alert("javascript version greater than 5!");
}
catch(e)
{
try
{
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
// alert("you're using IE!");
}
catch(E)
{
xmlhttp = new XMLHttpRequest();
//alert("non IE!");
}
}

//Ajax function for some other element before submitting form

           function sendtobox(param,param2)
{
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (this.responseText !== null) {
var ajaxElm = document.getElementById('boxA');
ajaxElm.innerHTML = this.responseText + ajaxElm.innerHTML; // append in front
}

}
}

xmlhttp.open("GET","getsubjects.php?q="+param+"&r="+param2,true);
xmlhttp.send();

}

//Ajax after submit button clicked

$("document").ready(function(){
$(".form").submit(function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html("<br />JSON: " + data["json"] );
//alert("Form submitted successfully.\nReturned json: " + data["json"]);
window.location='tutor-profile.php'
//header('Location:tutor-profile.php');
}
});
return false;
});
});
</script>

响应.php

if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}

//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){

include('inc/config.php');
$return = $_POST;
//$return ='{"slider1":"150","level":{"Upper Secondary":"2"},"sub":{"8":""},"rate2":{"7":"89","8":"150","9":"150","10":"150","11":"150","12":"150","13":"150","14":"150"},"a_end":"on","e_week":"on","e_end":"on","name":"ting1","phone":"098-098-0980","email":"ting1@Gmail.com","postcode":"56547","gender":"on","password":"nk","action":"test"}';
$return["json"] = json_encode($return);
// json_encode($return);
//
//below code to store in database
$data = json_decode($return["json"], true);
//var_dump($data); // Dump all data of the Array
>>>>>>INSERT QUERIES GO HERE>>>>>>>>>>>>>>>>
echo"form submitted ";
}

最佳答案

在Javascript中重定向到另一个页面的代码是

window.location.href ='tutor-profile.php'

反正他们不理解你以前的 Angular 色和XMLHttpRequest你的声明,也就是ajax,但是你只用jQuery的最后一个函数$.ajax简化了一切。

另一个更正是你说服务器响应将是“Json”类型,但你只是通过未命中返回一个字符串。您可以做的是将其更改为:

exit( json_encode( array('response'=>'put your text here') ) );

然后用JS代替

$(".the-return").html("<br />JSON: " + data["json"] );

放置这个:

$(".the-return").html("<br />JSON: " + data.response );

关于javascript - 通过 Ajax 成功将数据插入 mysql 后无法重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933890/

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