簡體   English   中英

Codeigniter表單使用Ajax提交

[英]codeigniter form submit with ajax

我搜尋了一天以尋找更好的Ajax提交的Codeigniter表單教程,但是我找不到一個更好的教程或一個清晰的教程。 在堆棧溢出中,也沒有直接更好的解決方案。 那里只提到有關ajax的部分。 沒有提到如何從控制器訪問以及如何在視圖中提交和顯示響應的方法。 我認為最好有人可以為此提供更好的指導。謝謝!

這就是我嘗試過的...

視圖

<html>
<body>
    <form method="post" name="myForm1" id="myForm1" enctype="multipart/form-data" >
    Email: <input type="text" name="email" id="email">
    Question: <input type="text" name="qText" id="qText">
    <input id="submitbutton" type="submit">
    </form>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>   //no need to specify the language
       $(document).ready(function() {

       $('#myForm1').submit(function(e) {

            var form = $(this);
            var dataString = $("#myForm1").serialize();
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "<?php echo site_url('form_controller/insert_into_db'); ?>",
                data: dataString,
                dataType: "html",
                success: function(data){
                    alert("success");

                },
                error: function() { alert("Error posting feed."); }
           });

        });
        });
    </script>   
</body>
</html>

控制者

<?php
class Form_controller extends CI_controller{

function index(){
    $this->load->view('submit_form');
}
public function insert_into_db(){
return true;
}

}

模型

 <?php

 class Form_model extends CI_Model{

function insertQ(){
    echo $email = $this->input->post('email');
    echo $text = $this->input->post('qText')
    $this->db->query("insert into form (email,text) values('$email','$text')");

}

請執行以下步驟:1.從控制器函數insert_into_db()調用insertQ()。 無需返回true。 2.創建新的視圖文件並加載到控制器函數insert_into_db()中。 3.在ajax成功函數中的任何div中加載響應。

例如:

public function insert_into_db(){
   $this->Form_model->insertQ();
   $this->load->view('success_view');
}

javascript ajax函數:

success: function(data){
    $('abc').html(data);
}

最簡單的方法如下

視圖

     <form method="post" name="myForm1" id="myForm1" enctype="multipart/form-data" >
     Email: <input type="text" name="email" id="email">
     Question: <input type="text" name="qText" id="qText">
     <input id="submitbutton" onclick="sub();" type="submit">
     </form>

      <script>
       $(document).ready(function() {

       function sub()
       {
              var dataString = $("#myForm1").serialize();
          $.post("<?php echo site_url('form_controller/insert_into_db');?>",
          {
            vals: dataString,
          },
         function(response)
         { 

          });
        }
       });
     </script>

控制者

function insert_into_db()
  {

    $data['email'] = $this->input->post('email');
    $data['qtext'] = $this->input->post('qtext');
    $this->modelname->insertQ($data)
  }

模型

public function insertQ($dat)
   {
    if($this->db->insert('table_name',$dat))
    {
        return $this->db->insert_id();
    }
    else
    {
        return false;
    }   
}   

如果我們使用ajax調用函數,則該函數應回顯或打印內容。PHP腳本打印或回顯的任何內容,即ajax調用所請求的任何輸出,都將作為響應返回給客戶端。

在控制器中

<?php
class Form_controller extends CI_controller{

function index(){
    $this->load->view('submit_form');
}
public function insert_into_db(){
   $data['email'] = $this->input->post('email');
    $data['qtext'] = $this->input->post('qtext');
    $response = $this->modelname->insertQ($data)
    echo $reponse;

}

}

有關ajax的詳細信息,請參考以下鏈接。

http://webdevelopingcat.com/jquery-php-beginner-tutorial-ajax/

<?php
class Form_controller extends CI_controller{
function index(){
    $this->load->view('submit_form');
}
public function insert_into_db(){
$this->load->model('Form_model');  
$response = $this->Form_model->insertQ();
echo $response;
}
}

您沒有在控制器中回顯任何內容,也沒有實例化模型。 將您的控制器更改為此,然后重試。

暫無
暫無

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

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