简体   繁体   English

Codeigniter表单使用Ajax提交

[英]codeigniter form submit with ajax

I have search for a day for find a better tutorial for codeigniter form submit in ajax, unfortunately I couldn't find a better one or clear one. 我搜寻了一天以寻找更好的Ajax提交的Codeigniter表单教程,但是我找不到一个更好的教程或一个清晰的教程。 In stack overflow also there is no directbetter solution for it. 在堆栈溢出中,也没有直接更好的解决方案。 there mention only about ajax part. 那里只提到有关ajax的部分。 there is no mentioned way for how to access from controller and how to submit and display response in view. 没有提到如何从控制器访问以及如何在视图中提交和显示响应的方法。 I think it's better if any one can provide a better guidance for this.thank you! 我认为最好有人可以为此提供更好的指导。谢谢!

this is what I have tried... 这就是我尝试过的...

view 视图

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

controller 控制者

<?php
class Form_controller extends CI_controller{

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

}

model 模型

 <?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')");

}

Follow below steps: 1. call insertQ() from controller function insert_into_db(). 请执行以下步骤:1.从控制器函数insert_into_db()调用insertQ()。 No need of return true. 无需返回true。 2. create new view file and load in controller function insert_into_db(). 2.创建新的视图文件并加载到控制器函数insert_into_db()中。 3. in ajax success function load response in any div. 3.在ajax成功函数中的任何div中加载响应。

ex: 例如:

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

javascript ajax function : javascript ajax函数:

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

The Most Simple Method as follows 最简单的方法如下

View 视图

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

Controller 控制者

function insert_into_db()
  {

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

Model 模型

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

If we call a function using ajax, that function should echo or print something.. Anything printed or echoed by a PHP script, ie any output, that has been requested by an ajax call, will be returned to the client as a response. 如果我们使用ajax调用函数,则该函数应回显或打印内容。PHP脚本打印或回显的任何内容,即ajax调用所请求的任何输出,都将作为响应返回给客户端。

In controller 在控制器中

<?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;

}

}

For detailed information regarding ajax ,please refer the following link.. It may help you 有关ajax的详细信息,请参考以下链接。

http://webdevelopingcat.com/jquery-php-beginner-tutorial-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;
}
}

You did not echo anything in the controller and also you have not instantiated your model. 您没有在控制器中回显任何内容,也没有实例化模型。 change your controller to this and try again. 将您的控制器更改为此,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM