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

标签 javascript php jquery mysql ajax

我使用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":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d3cec9c096e7e0cac6cecb89c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>","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/

相关文章:

php - Yii 过滤器未显示在 CGridView 中

php - 如何在 WooCommerce 管理订单列表上创建过滤器下拉菜单?

javascript - Yii - 如何打开 Jquery 对话框小部件并提交表单

jQuery + CSS : switch to "loading"

javascript - Typescript - "this"关键字错误

javascript - 更改 url 结尾以反射(reflect)当前月份的脚本 Ex 04/01/2019

javascript - Google map 空闲事件未触发

javascript - 您如何保护浏览器使用的 RESTful API 免受 CSRF 攻击?

linux - 运行系统命令的html

jquery - 简易饼图 : Bug percentage not centered?