我已成功使用ajax post保存输入值,但未加载下一页。
客户端代码:
<script type="text/javascript">
function notifyEmail() {
var form_data = {
'email':$('#inviteEmail').val()
};
alert($('#inviteEmail').val());
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});
}
</script>
<input type='text' id='inviteEmail' placeholder='Enter email'/>
<a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a>
控制器代码:
function email_invites()
{
$this->load->model('emailInvites');
if($query = $this->emailInvites->saveEmailInvite())
{
$this->load->view('emailInvites');
}
}
答案 0 :(得分:2)
我所做的是以下可以帮助您
<script type="text/javascript">
function notifyEmail() {
var form_data = {
'email':$('#inviteEmail').val()
};
alert($('#inviteEmail').val());
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
dataType:'json',
success: function(msg) {
if(msg.response)
//load the content in the body or wherever you want
jQuery('body').load("<?php echo site_url('main/email_invites/show'); ?>");
else
console.log("Opps, Something wrong happend");
return true;
}
});
}
</script>
<input type='text' id='inviteEmail' placeholder='Enter email'/>
<a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a>
在控制器中
function email_invites($action = 'process')
{
if($action == 'process')
{
$this->load->model('emailInvites');
$query['response'] = $this->emailInvites->saveEmailInvite();
echo json_encode($query);
}
else
{
$this->load->view('emailInvites');
}
}
答案 1 :(得分:0)
你不需要回显网址,只需在像这样的ajax调用中使用它,
$.ajax({
url: 'main/email_invites',
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});
如果site_url
方法返回所需的url,则需要调用ajax,
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});