0

I tried to submit a form using jquery ajax without codeigniter just to understand how ajax works. It worked fine but now I want to do it in codeigniter (since I am developing my application in CI). It's not inserting the values in the database. I do not know how to check where the problem is

This is my form in view :-

<form name="article_form" method="POST" action=""> <input type="text" name="title" placeholder="Title for your article" /> <br> <textarea rows="12" name="body" placeholder="Tell your story"></textarea> <br> <input type="submit" name="submit_article" id="submit_article" value="Post" /> </form> 

This is my jquery ajax script :-

<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script> <script> $('#submit_article').click(function(){ var article_title = document.getElementById("title").value; var article_body = document.getElementById("body").value; $.ajax({ url: '<?php echo base_url()."main/submit_article";?>', type: 'POST', dataType: 'json', data: 'title='+article_title + '&body='+article_body, success: function(output_string){ $('#result_table').append(output_string); } }); }); </script> 

Main is the controller and this the function submit_article :-

public function submit_article() { $article_title = $this->input->post('title'); $article_body = $this->input->post('body'); $this->load->model("model_articles"); if($this->model_articles->article_submit($article_title, $article_body)) { return true; } else { return false; } } 

This loads the model - model_articles and passes two value namely $article_title and $article_body to the function article_submit(). This is the function in the model that has to insert the values into the database :-

public function article_submit($article_title, $article_body) { $article_data = array( 'title' => $article_title, 'body' => $article_body ); $query_insert_article = $this->db->insert('articles', $article_data); if($query_insert_article) { return true; } else { return false; } } 
3
  • Are you sure no records are being entered in your DB? What happens if you try var_dump($article_title, $article_body); in your controller? Commented Feb 24, 2013 at 15:41
  • I get this bool(false) bool(false) Commented Feb 24, 2013 at 15:44
  • hi, 0, 0 are being stored in database. This means that the insert function ios working. I think the value is not reaching the controller from the view. Is there any way to the check this? Commented Feb 24, 2013 at 17:06

2 Answers 2

2

You need to change your controller function and ajax to try this one

 <script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script> <script> $('#submit_article').click(function(){ var article_title = $('input[name="title"]').val(); var article_body = $('textarea[name="body"]').val(); $.ajax({ url: '<?php echo base_url()."main/submit_article";?>/'+article_title+'/'+article_body , type: 'POST', dataType: 'json', data: $('#form_id').serialize(),/*edit form_id*/ success: function(output_string){ $('#result_table').append(output_string); } }); }); </script> 

and this one is yours controller

 public function submit_article($article_title,$article_body ) { $this->load->model("model_articles"); if($this->model_articles->article_submit($article_title, $article_body)) { return true; } else { return false; } 

}

hope it will sovle .....

Sign up to request clarification or add additional context in comments.

Comments

0

You need to change your JavaScript to this:

$('#submit_article').click(function(e) { var article_title = $('input[name="title"]').val(); var article_body = $('textarea[name="body"]').val(); $.ajax({ url: '<?php echo base_url(); ?>main/submit_article', type: 'POST', dataType: 'json', data: 'title=' + article_title + '&body=' + article_body, success: function(output_string) { $('#result_table').append(output_string); } }); // Add this if you want to prevent the normal form from sending: e.preventDefault(); }); 

1 Comment

i added this but no luck, I also made a change to input tag by adding id="title" and id="body". It does'nt work. when I run the controller with pre-assigned values for $article_title and $article_body it stores the value in the database. But when I submit the form with values nothing happens. I guess there is something weong with the veiw and jquery ajax code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.