Php 如何进行客户端和服务器端验证?

Php 如何进行客户端和服务器端验证?,php,html,jquery,Php,Html,Jquery,我正在创建一个HTML表单,在该表单中,我同时执行客户端和服务器端验证 <form action='logic.php' method='post' id='login'> <input type='text' placeholder='Enter name' name='name'> <input type='submit'> </form> // Jquery $(document).ready(function(){

我正在创建一个HTML表单,在该表单中,我同时执行客户端和服务器端验证

<form action='logic.php' method='post' id='login'>
    <input type='text' placeholder='Enter name' name='name'>
    <input type='submit'>
</form>

// Jquery
$(document).ready(function(){

    function validate_name(input_id,error_id,msg="Name"){
        const x = document.getElementById(input_id).value;
        const y = document.getElementById(error_id);
        const pattern = /^[a-zA-Z]+$/
        if(x!==""){
            if(pattern.test(x) ){
                if(x.length<3 || x.length>10){
                    y.innerHTML = msg+' should be between 3 and 10 characters.'
                    return false;
                }
                else{
                    y.innerHTML = '';
                    return true;
                }
            }
            else{
                y.innerHTML = 'Should contain only alphabets.'
                return false;
            }
        }
        else{
            y.innerHTML = "Please Enter Your "+msg;
            return false;
        }
    }


    $('#login').submit(function(e){
        e.preventDefault();
        let name = validate_name('rollno','rerror');
        let subject_name = validate_select('subject_dropdown','serror');
        if(name === true & subject_name === true){
            window.location = 'controller/indexPage_logic.php';
        }
    })
});

//Jquery
$(文档).ready(函数(){
函数验证\名称(输入\标识,错误\标识,msg=“name”){
const x=document.getElementById(input_id).value;
const y=document.getElementById(错误id);
常量模式=/^[a-zA-Z]+$/
如果(x!==“”){
if(模式测试(x)){
如果(x.长度10){
y、 innerHTML=msg+'应介于3到10个字符之间。'
返回false;
}
否则{
y、 innerHTML='';
返回true;
}
}
否则{
y、 innerHTML='应仅包含字母表。'
返回false;
}
}
否则{
y、 innerHTML=“请输入您的”+msg;
返回false;
}
}
$('#login')。提交(函数(e){
e、 预防默认值();
让name=validate_name('rollno','rerror');
让subject_name=validate_select('subject_dropdown','serror');
if(name==true和subject_name==true){
window.location='controller/indexPage_logic.php';
}
})
});

我可能能够进行客户端验证,但如果表单得到正确验证,我就无法在服务器端PHP中使用$_POST['name']访问输入值。我认为这是由于window.location造成的。是否有更好的方法进行两侧验证?

当您需要POST时,您的位置更改会起作用

改为

$('#login').submit(function(e){
    e.preventDefault();
    let name = validate_name('rollno','rerror');
    let subject_name = validate_select('subject_dropdown','serror');
    if(name === true & subject_name === true){
        $.post('controller/indexPage_logic.php', {name:rollno}, function(response) { console.log(response)}); // do what you need to do when valid on the server too
    }
})

在表单中的操作参数中设置正确的文件

在if条件中,写入
e.currentTarget.submit()


您必须将数据发送到服务器,问题是Windows。location没有向服务器发送任何请求 能够共犯那次使用

$.post('url to the server', { data1 : "Your data", data2 : yourvar }, function(res) {
   console.log(res)
});

当存在AJAX时,为什么要使用window.location
?window.location不存在,您需要发布表单。使用$.post并读取响应这是可行的。但是如果我检查isset($\u post['login\u submit'])(我的提交按钮的名称),它将返回false。在$\u post数组中只有输入的值。将提交按钮设置为:
$.post('url to the server', { data1 : "Your data", data2 : yourvar }, function(res) {
   console.log(res)
});