jquery ajax表单提交不工作与codeigniter但工作否则 [英] jquery ajax form submit not working with codeigniter but working otherwise
问题描述
我试图提交一个表单使用jquery ajax没有codeigniter只是为了解ajax如何工作。它工作正常,但现在我想在codeigniter(因为我正在开发我的应用程序在CI)。它不是在数据库中插入值。我不知道如何检查问题在哪里
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>
这是我的jquery ajax脚本: -
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>
主控制器和这个函数submit_article: -
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;
}
}
这会加载模型 - model_articles并传递两个值$ article_title和$ article_body到函数article_submit()。这是模型中必须将值插入数据库的函数: -
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;
}
}
推荐答案
需要改变你的控制器函数和ajax到
尝试这一个
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>
这是您的控制器
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;
}
}
希望它会sovle .....
hope it will sovle .....
这篇关于jquery ajax表单提交不工作与codeigniter但工作否则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!