Javascript 我可以在表单提交时调用php文件,并从中调用或回显JS函数吗?
所以我基本上是想创建一个HTML表单Javascript 我可以在表单提交时调用php文件,并从中调用或回显JS函数吗?,javascript,php,html,forms,Javascript,Php,Html,Forms,所以我基本上是想创建一个HTML表单 通过php POST方法处理我的登录 使用javascript关闭我的登录lighbox,或者显示一个错误(现在我正在使用JS更改登录表单下的一个不可见表单值) HTML if(isset($\u GET['error'])){ echo“登录时出错!”; }?> 电子邮件: 密码: 以下是php文件: if (isset($_POST['email'], $_POST['p'])) { $email = $_POST['email
if(isset($\u GET['error'])){
echo“登录时出错!
”;
}?>
电子邮件:
密码:
以下是php文件:
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p'];
if (login($email, $password, $mysqli) == true) {
// Login success
echo "<script>document.getElementById('light').style.display=
'none';document.getElementById('fade').style.display= 'none'</script>";
} else {
// Login failed
echo "<script>document.getElementById('errorbox').value='Error'</script>";
}
} else {
// The correct POST variables were not sent to this page.
echo 'Invalid Request';
}
if(设置($\u POST['email'],$\u POST['p'])){
$email=$_POST['email'];
$password=$_POST['p'];
if(登录($email,$password,$mysqli)==true){
//登录成功
echo“document.getElementById('light').style.display=
'none';document.getElementById('fade').style.display='none';
}否则{
//登录失败
echo“document.getElementById('errorbox')。value='Error';
}
}否则{
//未将正确的POST变量发送到此页面。
回显“无效请求”;
}
我知道我做得不对,我只是不知道怎么做。。。
将onsubmit事件属性添加到表单:
其中validate()是一个js函数,它关闭lightbox并在表单正常时返回true,或者在表单不好时显示错误并返回false。好的,是的,从技术上讲,您可以这样做,但这是一个非常糟糕的做法,而且非常不干净 您更愿意使用Ajax,并在其成功回调中执行这些JS操作: 注意:您需要在脚本中包含lib。 另一个无:如果您的服务器上没有PHP5.4,只需删除第二个回调函数,并处理成功回调中的所有场景
!(function($){
$(function() {
$('#submitBtn').on('submit', function(){
$.post('process_login.php', $('form[name="login_form"]').serialize(), function(data){
//data is a json object which contans the reponse
data = $.parseJSON(data);
$("fade").fadeOut();
$("light").fadeOut();
},
function(data){//error callback
data = $.parseJSON(data);
if(data.forbidden){
$("#errorBox").html("Error!");
}
else if(data.error){
$("#errorBox").html("Invalid request!");
}
});
});
});
})(window.jQuery);
HTML:
使用AJAX提交表单。AJAX是否会降低登录过程的安全性?不,不会,AJAX是一个HTTP请求,就像重新加载整个页面一样。这似乎是我需要做的。我尝试过逐字复制您的代码,(嗯,在我的表单HTML下面添加和关闭标记)。除了“找不到对象”之外,我仍然没有得到任何其他信息错误404。我添加了jquery 1.9库,我不确定我的php是什么版本。我使用的是xampp的更新版本。由于它是一个事件处理程序,它应该在jquery ready函数中…我将编辑代码。我现在在最后一行遇到一个snytax错误。另外“'from[name=“login_form”]”应该是form[name…]对吗?(我不是在挖苦人,我只是对JS方面的事情有点不知所措)关于语法错误,我刚刚通过JS linter运行了代码,有一些警告,但没有错误,现在就试试。呃,仍然得到了object not found错误。不确定我的问题在哪里。注意:我没有使用JS lightbox,这是css。
!(function($){
$(function() {
$('#submitBtn').on('submit', function(){
$.post('process_login.php', $('form[name="login_form"]').serialize(), function(data){
//data is a json object which contans the reponse
data = $.parseJSON(data);
$("fade").fadeOut();
$("light").fadeOut();
},
function(data){//error callback
data = $.parseJSON(data);
if(data.forbidden){
$("#errorBox").html("Error!");
}
else if(data.error){
$("#errorBox").html("Invalid request!");
}
});
});
});
})(window.jQuery);
<form name="login_form">
<div>
<input type="text" name="email" placeholder="email">
</div>
<div>
<input type="password" name="p" placeholder="password">
</div>
<div>
<input type="button" id="submitBtn" value="Login">
</div>
</from>
$response = array();
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p'];
if (login($email, $password, $mysqli) == true) {
http_response_code(200);//HTTP OK, requires php 5.4
$response['success'] = true;
} else {
// Login failed
$response['forbidden'] = true;
http_response_code(401);//HTTP forbidden
}
} else {
// The correct POST variables were not sent to this page.
$response['error'] = true;
http_response_code(400);//HTTP bad request
}
echo json_encode($response);