通过AJAX成功提交表单后重定向到其他页面

时间:2016-06-08 05:55:56

标签: javascript php jquery ajax forms

我创建了一个简单的表单,并将表单中的值存储在数据库中。这部分现在工作正常。 现在我想在成功提交表单后将用户重定向到另一个页面。 我怎样才能做到这一点?目前我正在获取成功保存的警告框,但是当我单击该警报框时,它不会重定向到另一页(实际上我想将其重定向到名为first.php的页面)。

这是我的代码

控制器

public function user_add() {
    $data_save = array(
        "Mnumber" => $this->input->post("Mnumber"),
        "email" => $this->input->post("email"),
        "fname" => $this->input->post("fname"),
        "address" =>$this->input->post("address"),
        "sitename" =>$this->input->post("sitename"),
        /* "reqnum" => $this->input->post("reqnum"),*/
        "title" => $this->input->post("title"),
        "descr" => $this->input->post("descr"),
        /*"payment" => $this->input->post("payment"),*/
        "uniquekey" => $this->input->post("uniquekey")
        /*"subscription" => $this->input->post("subscription"),
        "email_sent" => $this->input->post("email_sent"),*/
    );
    if ($this->user_mod->AddUser($data_save)) {
        echo "Successfully Saved";
    }
    else {
        echo "error";
    }
}

模型

public function AddUser($data_save) {
    if ($this->db->insert('users', $data_save)) {
        return true;
    } else {
        return false;
    }
}

查看

<script>
    function save_user_new() {
        var Mnumber = $('#Mnumber').val();
        var email = $('#email').val();
        var fname = $('#fname').val();
        var address = $('#address').val();
        var sitename = $('#sitename').val();
        /*var reqnum = $('#reqnum').val();*/
        var title = $('#title').val();
        var descr = $('#descr').val();
        var uniquekey = $('#uniquekey').val();
        /*var subscription = $('#subscription').val();
        var email_sent = $('#email_sent').val();
        var payment = $('#payment').val();*/

        if (sitename != "" && email != "") {
            $.ajax({
                type: "post",
                async: false,
                url: "<?php echo site_url('form_con/user_add'); ?>",
                data: {
                    "Mnumber": Mnumber,
                    "email": email,
                    "fname": fname,
                    "address": address,
                    "sitename": sitename,
                    /*"reqnum": reqnum,*/
                    "title": title,
                    "descr": descr,
                    "uniquekey": uniquekey
                    /*"subscription": subscription,
                    "email_sent": email_sent,
                    "payment":payment*/
                },
                dataType: "html",
                success: function (data) {
                    alert(data);
                    if (data == 'error') {
                        $('#success_msg').hide();
                        $('#error_msg1').show();
                        $('#error_msg1').html("Error : Something wrong.");
                    } else if (data == 'have') {
                        $('#success_msg').hide();
                        $('#error_msg1').show();
                        $('#error_msg1').html("Error : This Sitename is already exists.");
                    } else {
                        $('#error_msg1').hide();
                        $('#success_msg').show();
                        $('#success_msg').html("User details successfully saved.");
                    }
                }

            });
        } else {
            $('#success_msg').hide();
            $('#error_msg1').show();
            $('#error_msg1').html("Error : Please enter User Details.");
        }
    }
</script>

3 个答案:

答案 0 :(得分:0)

使用window.location.hrefwindow.location.replace

function save_user_new() {

  var Mnumber = $('#Mnumber').val();
  var email = $('#email').val();
  var fname = $('#fname').val();
  var address = $('#address').val();
  var sitename = $('#sitename').val();
  /*var reqnum = $('#reqnum').val();*/
  var title = $('#title').val();
  var descr = $('#descr').val();
  var uniquekey = $('#uniquekey').val();
  /*var subscription = $('#subscription').val();
  var email_sent = $('#email_sent').val();
  var payment = $('#payment').val();*/

  if (sitename != "" && email != "") {
    $.ajax({
      type: "post",
      async: false,
      url: "<?php echo site_url('form_con/user_add'); ?>",
      data: {
        "Mnumber": Mnumber,
        "email": email,
        "fname": fname,
        "address": address,
        "sitename": sitename,
        /*"reqnum": reqnum,*/
        "title": title,
        "descr": descr,
        "uniquekey": uniquekey
          /*"subscription": subscription,
          "email_sent": email_sent,
          "payment":payment*/
      },
      dataType: "html",
      success: function(data) {
        alert(data);
        if (data == 'error') {
          $('#success_msg').hide();
          $('#error_msg1').show();
          $('#error_msg1').html("Error : Something wrong.");

        } else if (data == 'have') {
          $('#success_msg').hide();
          $('#error_msg1').show();
          $('#error_msg1').html("Error : This Sitename is already exists.");
        } else {
          $('#error_msg1').hide();
          $('#success_msg').show();
          $('#success_msg').html("User details successfully saved.");
          window.location.href = 'http://example.com/view-details'; // Keeping current page history

         // or

          window.location.replace = 'http://example.com/view-details'; // Current page history will be replaced
        }

      }

    });
  } else {
    $('#success_msg').hide();
    $('#error_msg1').show();
    $('#error_msg1').html("Error : Please enter User Details.");
  }
}

答案 1 :(得分:0)

如果您希望用户看到您的消息,请执行此操作

$('#success_msg').html("User details successfully saved.");
setTimeout(function() { location="first.php"},2000);

答案 2 :(得分:0)

如果要使用JavaScript和jQuery重定向,可以使用Javascript的以下属性。

location.href = 'first.php';

OR

location.replace('first.php');

将代码放在要重定向到的位置。