2016-06-08 125 views
1

我創建了一個簡單的表單,並將表單中的值存儲在數據庫中。現在這部分工作正常。 現在我想在成功提交表單後將用戶重定向到另一個頁面。 我該怎麼做?目前我收到了警告框說已成功保存,但是當我點擊該警報框時,它不會重定向到其他頁面(實際上我想將它重定向到名爲first.php的頁面)。通過AJAX成功提交表單後重定向到其他頁面

這裏是我的代碼

控制器

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> 
+0

可能的重複[如何使頁面重定向使用jQuery?](http://stackoverflow.com/questions/503093/how-can-i-make-a-page-redirect-using-jquery) –

回答

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

如果你希望用戶看到你的消息,如果你想使用JavaScript和jQuery你可以使用JavaScript的以下屬性重定向做到這一點

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

location.href = 'first.php';

或您想重定向到occure

location.replace('first.php');

地方你的代碼。