问题
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;
}
}
回答1:
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 .....
回答2:
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();
});
来源:https://stackoverflow.com/questions/15053338/jquery-ajax-form-submit-not-working-with-codeigniter-but-working-otherwise