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

时间:2015-04-29 03:38:05

标签: 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 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

$("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":"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 ";
}

1 个答案:

答案 0 :(得分:3)

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

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

无论如何,他们不了解你以前的角色和XMLHttpRequest你的语句,即ajax,但你只使用jQuery的最后一个函数$ .ajax来简化所有事情。

另一个更正是你说服务器响应的类型是&#34; Json&#34;但你只是通过一个小姐回馈一个字符串。您可以做的是将其更改为:

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

然后在JS而不是

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

放置这个:

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