Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter & jquery Ajax

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

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.

like image 554
Nirina-aj Avatar asked Feb 02 '17 15:02

Nirina-aj


1 Answers

Try this.

In View (AJAX Part)

<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);
} 
like image 101
Abdulla Nilam Avatar answered Sep 30 '22 06:09

Abdulla Nilam