5

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.

3
  • Have you inspected the request to check if the correct form data is being sent, and what headers are being received? Commented Feb 2, 2017 at 15:29
  • You're probably getting a PHP error into the error log, and Codeigniter or php.ini might be configured to not display php errors, since otherwise the echoed html, whether it's a validation error or a success, would be picked by jQuery and displayed in the message div Commented Feb 2, 2017 at 15:36
  • samiles, I received ok 200ms Commented Feb 3, 2017 at 16:36

2 Answers 2

5

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);
} 
Sign up to request clarification or add additional context in comments.

Comments

0

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 action empty.Like this..

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

1 Comment

and there is no problem about encode json,in you opinion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.