2
我使用ajax,php將數據插入到mysql數據庫中。但是,在完成執行所有查詢之後,它不會執行任何操作。我的意思是,當我單擊表單中的提交按鈕時,作爲用戶,即使設置了我的Ajax函數來提醒數據,我也沒有看到任何事情發生。但是在後端,所有的數據都被正確地插入到數據庫中。通過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 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();
}
//前阿賈克斯點擊提交按鈕後
$("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>
response.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":"[email protected]","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 ";
}
您response.php必須返回JSON數據: 'echo $ data'不是'echo form submitted' – madalinivascu