php Codeigniter & jquery Ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42006000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:32:01  来源:igfitidea点击:

Codeigniter & jquery Ajax

phpjqueryajaxcodeigniter

提问by J.Antonio

I can not insert the data by forms in the database with ajax,There is no firebug error someone can help me

我无法使用ajax在数据库中通过表单插入数据,没有萤火虫错误有人可以帮助我

View:

看法:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Blog</title>
 <link rel="stylesheet" href="<?php echo base_url("assets/css/bootstrap.min.css"); ?>">
</head>
<body>
 <h3 style="text-align: center;">CODEIGNITER AJAX</h3>
 <div class="row">
  <div class="alert alert-success" id="message" style="display: none;">
  </div>
  <div class="col-md-4"></div>
  <div class="col-md-4">
   <?php echo form_open('blog_c',array('id'=>'myForm'));?>
    <div class="form-group">
     <label>EMAIL:</label>
     <input type="text" name="email" id="email" class="form-control" placeholder="EMAIL">
    </div>
    <input type="submit" value="ENVOYER" id="btn">
   <?php echo form_close()?>
  </div>
 </div>
 <script type="text/javascript" src="<?php echo base_url()?>assets/js/jquery-3.1.1.js"></script>
 <script type="text/javascript">
  $(document).ready(function(){ 
   $('#btn').click(function(){
    var email=$('#email').val();
    $.ajax({              //request ajax
     url:"<?php echo site_url('blog_c/registre')?>",
     type:POST,
     data:{email:email},
     dataType:json,
      success: function(repons) {
                      $("#message").html(repons);
                     
                   },
                  error: function() {
                     alert("Invalide!");
                  }
    });
   });
  });
  
 </script>
</body>
</html>

Model:

模型:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog_m extends CI_Model
{
 function __construct()
  {
     parent:: __construct();
   }
  function registre($data)
 {
  $this->db->insert('utilisateurs',$data);
 } 

}

Controler:

控制器:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog_c extends CI_Controller 
{
 public function __construct()
 {
  parent::__construct();
  
 }

 public function index()
 {
  $this->load->view('blog_w');
 }

 public function registre()
 {
  // set rules
 $this->form_validation->set_rules('email','EMAIL','trim|required|valid_email|is_unique[utilisateurs.email]');
 if($this->form_validation->run()==FALSE)
  {
   echo validation_errors();
  }else{
   $data=array(
    'email'=>$this->input->post('email'));
   $this->blog_m->registre($data);
   

   echo "<div class='alert'>Inscription success</div>";
   echo "email";
  }
 }

}

There is no error but the data does not insert in the database and there is no success message.

没有错误但是数据没有插入到数据库中,也没有成功信息。

采纳答案by Abdulla Nilam

Try this.

尝试这个。

In View (AJAX Part)

在视图中(AJAX 部分)

<script>
    $(function(){
        $( "#btn" ).click(function(event)
        {
            event.preventDefault();
            var email= $("#email").val();

            $.ajax(
                {
                    type:"post",
                    url: "<?php echo base_url(); ?>index.php/blog_c/registre",
                    data:{ email:email},
                    success:function(response)
                    {
                        console.log(response);
                        $("#message").html(response);
                        $('#cartmessage').show();
                    }
                    error: function() 
                    {
                        alert("Invalide!");
                    }
                }
            );
        });
    });
</script>


In Controller

在控制器中

public function registre()
{

    $email = $this->input->post('email'); # add this

    $this->form_validation->set_rules('email','EMAIL','trim|required|valid_email|is_unique[utilisateurs.email]');

    if($this->form_validation->run() == FALSE)
    {
        echo validation_errors();
    }
    else
    {               
        if(!$this->blog_m->registre($email))
        {
            echo "Something Went Wrong";
        }               
        else
        {
            echo "Inscription success";
        }

    }
}


In Model

在模型中

function registre($email)
{
    $data = array(
                'email'=>$this->input->post('email')
            );

    $this->db->insert('utilisateurs',$data);
} 

回答by Hikmat Sijapati

In your controller load model first.Like this..

首先在您的控制器加载模型中。像这样..

public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));//loads required heplers
        $this->load->model('blog_m');//loads your model 
    }

In view: you are using ajax so set form actionempty.Like this..

鉴于:您正在使用ajax,因此将表单设置为action空。像这样..

<?php echo form_open('',array('id'=>'myForm'));?>