Javascript 使用CodeIgniter通过AJAX提交表单
我正在构建一个web应用程序,它可以让用户以问答的形式关注讨论主题。为此,在显示问题时,我在每个问题旁边都有一个“跟随”按钮。我希望用户能够在不重新加载页面的情况下遵循这些线程,从而使用AJAX。因此,我希望AJAX调用: 1) 提交一份表格,更新数据库,记录以下关系;和 2) 将“跟随”提交按钮替换为“跟随” 以下是我的JavaScript代码:Javascript 使用CodeIgniter通过AJAX提交表单,javascript,ajax,forms,codeigniter,Javascript,Ajax,Forms,Codeigniter,我正在构建一个web应用程序,它可以让用户以问答的形式关注讨论主题。为此,在显示问题时,我在每个问题旁边都有一个“跟随”按钮。我希望用户能够在不重新加载页面的情况下遵循这些线程,从而使用AJAX。因此,我希望AJAX调用: 1) 提交一份表格,更新数据库,记录以下关系;和 2) 将“跟随”提交按钮替换为“跟随” 以下是我的JavaScript代码: $("#submitFollow").click(function() { var type = $("input#type").val()
$("#submitFollow").click(function() {
var type = $("input#type").val();
var u_id = $("input#u_id").val();
var e_id = $("input#e_id").val();
var dataString = 'type='+ type + '&u_id=' + u_id + '&e_id=' + e_id;
$.ajax({
type: "POST",
url: "<?=site_url()?>/follow/ajaxFollow",
data: dataString,
success: function() {
$('#following').html("Following");
.hide()
.fadeIn(1500)
}
});
return false;
});
});
$(“#submitFollow”)。单击(函数(){
变量类型=$(“输入类型”).val();
var u#u id=$(“输入#u#u id”).val();
var e#u id=$(“输入e#u id”).val();
var dataString='type='+type+'&u_id='+u_id+'&e_id='+e_id;
$.ajax({
类型:“POST”,
url:“/follow/ajaxFollow”,
数据:dataString,
成功:函数(){
$('following').html(“following”);
.hide()
法丹先生(1500)
}
});
返回false;
});
});
下面是我的表单代码。我删除了阻止表单定期提交的操作和方法。我尝试使用JS防止默认,但没有成功。(数值由模型填写):
现在,控制器非常简单——它只需要获取post变量数组并将它们直接提交到数据库
我对JavaScript不太在行,因此如果我在一个简单的问题上占用您的时间,我深表歉意。我在这里只是简单介绍一下,但希望能有所帮助。如果是我,我会这样做: 下面是一些由codeigniter生成的html:
<div>
questions 1
</div>
<div>
<a class="follow" href="http://example.com/follow/question/1">Follow Question 1</a>
</div>
<div>
questions 2
</div>
<div>
<a class="follow" href="http://example.com/follow/question/2">Follow Question 2</a>
</div>
我非常喜欢这个解决方案,但我无法让event.preventDefault()为我的站点做任何事情。记录被添加到数据库中很好,但是链接可以工作,没有Ajax。我已经安装了jquery,并将其用于其他方面,但我无法停止任何表单或链接的默认活动。填报虚假报税表;也什么都不做。有什么建议吗?嗯,我不确定到底是什么做的,但我终于让它起作用了。我将event.preventDefault()替换为return false,并将其移动到函数末尾。到目前为止,一切顺利。
<div>
questions 1
</div>
<div>
<a class="follow" href="http://example.com/follow/question/1">Follow Question 1</a>
</div>
<div>
questions 2
</div>
<div>
<a class="follow" href="http://example.com/follow/question/2">Follow Question 2</a>
</div>
<?php
class Follow extends Controller {
function Follow()
{
parent::Controller();
$this->auth->restrict_to_admin();
}
function question($id = NULL)
{
if($id)
{
//get question from database using the id
//i assume somehow the user is logged in? use their id where applicable
echo '<span>You are now following this question</span>';
}
}
}
/* End of file follow.php */
$(document).ready(function(){
$('follow').click(function(event){
event.preventDefault();
var followUrl = $(this).attr('href');
//do ajax call here.
$.post(
followUrl,
{
//any paramaters you need to add can
//go here in this format:
//param_key: param_value
},
function(responseText){
//here you can update the clicked link to say something
//like "you are now following this question"
//the responseText var will have whatever text the server responds with.
//just responsd with html
},
'html'
);
});
});