Stopping empty form submission PHP

前端 未结 4 1284
一整个雨季
一整个雨季 2021-01-28 05:00

I have been trying to implement server validation to prevent blank emails in my contact us page, but I am not sure on how to do it in PHP, here is my code:



        
4条回答
  •  有刺的猬
    2021-01-28 05:13

    just test the variable for "emptiness" and exit early. Something like this:

    if(empty($field_email)) {
        // maybe show the user a reason why this was rejected...
        return;
    }
    

    You probably want to do this for just about all the input fields.

    In addition, you can use JavaScript (jQuery has some nice plugins) to prevent the user from submitting invalid data in the first place. This won't remove the need to do it server side (since they can just disable JS, or someone malicious might intentionally bypass this measure), but it can make it a more user friendly experience.

提交回复
热议问题