PHP和javascript之间正确的交互方式? [英] Right way of interaction between PHP and javascript?

查看:109
本文介绍了PHP和javascript之间正确的交互方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有一个登录表单,在提交后,由PHP验证(检查数据库等)。现在,我需要使用Javascript向用户发送反馈(登录失败或成功)。这样做的正确方法是什么?



我的尝试:



到目前为止,我知道我可以通过简单地回显调用javascript函数(< script> jsFuntion(message);< / script>)或使用AJAX的脚本来实现这一点。但是我问自己什么是最安全最正确的方法?

Hi, everybody. I have a login form which is, after submitting, being validated by PHP(checking the database etc.). Now, I need to send feedback to user (login fail or succes) by using Javascript. What is the right way of doing this?

What I have tried:

So far, I know I can do this by simply echoing the script that calles javascript function (<script> jsFuntion(message) ;</script>) or by using AJAX. But I'm asking myself what is the safest and the most correct way?

推荐答案

我认为,解决这个问题的最好方法是使用'SESSION'。将结果(无论成功还是失败)存储在会话中作为闪存消息的种类。



1.在流程页面操作后设置会话消息。

2.然后在重定向页面检查会话消息设置与否。

3.如果已设置,则只需回显该消息。



以下代码可能对您有所帮助。



I think, the best way to solve this problem is to use 'SESSION'. Store the result (whether success or failure) in the session as sort of a flash message.

1. Set a session message after your operation in the process page.
2. Then in the redirected page check whether the session message is set or not.
3. If it is set then simply echo that message.

The below code may help you.


_SESSION ['message'] ='';

//在流程页面中,您要检查身份验证
if(logged_in){//您的用户身份验证用户
_SESSION['message'] = ''; // In the process page, where you are checking for the authentication if (logged_in) { // your logic for authenticating user


_SESSION ['message'] ='成功';
}其他{
_SESSION['message'] = 'Success'; } else {


这篇关于PHP和javascript之间正确的交互方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆