簡體   English   中英

通過AJAX成功提交表單后重定向到其他頁面

[英]Redirect to a other page after successful form submission through AJAX

我創建了一個簡單的表單並將表單中的值存儲在數據庫中。 這部分現在工作正常。 現在我想在成功提交表單后將用戶重定向到另一個頁面。 我怎樣才能做到這一點? 目前我正在獲取成功保存的警告框,但是當我單擊該警報框時,它不會重定向到另一個頁面(實際上我想將其重定向到名為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>

使用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.");
  }
}

如果您希望用戶看到您的消息,請執行此操作

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

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

location.href = 'first.php';

要么

location.replace('first.php');

將代碼放在要重定向到的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM