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; } }
var_dump($article_title, $article_body);in your controller?