我的 View 页面中有一个表单,用于在 Controller 中提交一个名为 function Login()
的函数
这是我的登录功能
function Login()
{
$EmailId = $this->input->post('mailId');
$Password = $this->input->post('password');
$res=$this->friendsmodel->CheckLogin($EmailId,$Password);
if($res==true)
{
$_SESSION['Authenticaton_user']="auth_user";
$this->session->set_flashdata('item', 'Thanks for logging in');
//I want to call javascript function from here
redirect('friends/Display_News');
}
else
{
$this->session->set_flashdata('item', 'Username Or Password is invalid');
redirect('friends');
}
}
现在想从我的 if 和 else 语句中调用名为 topBar()
的 JavaScript 函数
这是我的脚本
function topbar(message)
{
var alert = $('<div id="alert">'+message+'</div>');
$(document.body).append(alert);
var $alert = $('#alert');
if ($alert.length) {
var alerttimer = window.setTimeout(function() {
$alert.trigger('click');
}, 5000);
$alert.animate({ height: $alert.css('line-height') || '50px' }, 200).click(function() {
window.clearTimeout(alerttimer);
$alert.animate({ height: '0' }, 200);
});
}
}
如何从此处调用 JavaScript
最佳答案
您无法从 PHP Controller 调用 JavaScript 函数,但是您可以将某些变量设置为 View 的标志来调用该函数:
function Login()
{
// I don't know how you interact with your views, so I will just
// assume that it happens something like this:
if($res==true)
{
// call the function
$this->view->assign('callTopBar', true);
}
else
{
// do not call the function
$this->view->assign('callTopBar', false);
}
}
然后,在 View 内:
<?php if ($this->callTopBar): ?>
<script type="text/javascript">
topbar();
</script>
<?php endif; ?>
关于php - 如何从我的 Controller 调用 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3155517/