Javascript 使用Jquery和Php验证前端和后端的表单数据
我正在尝试在前端和后端验证我的表单数据,还使用jquery和php在jquery上使用post方法Javascript 使用Jquery和Php验证前端和后端的表单数据,javascript,php,jquery,laravel,validation,Javascript,Php,Jquery,Laravel,Validation,我正在尝试在前端和后端验证我的表单数据,还使用jquery和php在jquery上使用post方法 <form> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" id="exampleInputEmail1" ar
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
电子邮件地址
我们永远不会与其他人共享您的电子邮件。
暗语
提交
这是我的表单如何在前端和后端验证它而无需页面刷新
<script>
$(document).ready(function(){
$("button").on("click", function(){
let email = $.trim($("#exampleInputEmail1").val());
let password = $.trim($("#exampleInputPassword1").val());
if(email === ""){
alert("Email is required!");
return false;
}
if(password === ""){
alert("Password is required!");
return false;
}
$.post("login.php",{email : email , password : password}, function(response){
response = JSON.parse(response);
if(response.success){
// redirect to homepage
}else{
alert(response.message);
}
});
});
});
</script>
login.php
<?php
$email = $_POST["email"];
$password = $_POST["password"];
if($email === ""){
echo json_encode(array("success" => false,"message" => "Email is required!"));
exit;
}
if($password === ""){
echo json_encode(array("success" => false,"message" => "Password is required!")); exit;
}
//check the record in db and send back the response
?>
$(文档).ready(函数(){
$(“按钮”)。在(“单击”,函数(){
让电子邮件=$.trim($(“#示例输入电子邮件1”).val());
设密码=$.trim($(“#示例输入密码1”).val());
如果(电子邮件==“”){
警报(“需要电子邮件!”);
返回false;
}
如果(密码==“”){
警报(“需要密码!”);
返回false;
}
$.post(“login.php”,{email:email,password:password},函数(响应){
response=JSON.parse(response);
if(response.success){
//重定向到主页
}否则{
警报(response.message);
}
});
});
});
login.php
您可以通过编写解决方案进行验证,然后在此处询问我正在尝试进行验证。。尝试此操作的代码在哪里?