简体   繁体   English

PHP处理表单输入

[英]PHP handling form inputs

looking to find out the best way to handle my form inputs. 寻找找出处理我的表格输入的最佳方法。 Firstly, the form is completely validated using Javascript. 首先,该表格使用Javascript进行了完全验证。 I presume validation is then done in PHP for users who disable Javascript. 我认为对于禁用Javascript的用户,然后在PHP中完成验证。 At the moment I am getting inputs like so 此刻我正在得到这样的输入

<?php

$title = $_POST['inputTitle'];
$name = $_POST["inputName"];
$surname = $_POST["inputSurname"];
$email = $_POST["inputEmail"];
$link = $_POST["inputLinks"];

if (isset($title) && isset($name) && isset($surname) && isset($email) && isset($link)) {
    //do something
}

Is it enough to simply do that? 仅仅这样做就足够了吗? Or should I be doing proper validation on the email even though it was done in Javascript? 或者即使我使用Javascript进行验证,还是应该对电子邮件进行适当的验证?

Also, would it be best to add these inputs to an array? 另外,最好将这些输入添加到数组中吗? I cant do it via html because I needed to give each input their own unique name. 我无法通过html进行操作,因为我需要为每个输入提供自己的唯一名称。

Thanks 谢谢

You should still validate the variables, don't rely on the JavaScript. 您仍然应该验证变量,不要依赖JavaScript。 Your PHP code could receive inputs from elsewhere, the user could have JavaScript disabled, or deliberately manipulate the form in their browser to bypass any validation. 您的PHP代码可能会收到其他地方的输入,用户可能已禁用JavaScript,或故意在浏览器中操纵表单以绕过任何验证。

Moving the data to an array instead of individual variables depends on what you are intending to do with it. 将数据移至数组而不是单个变量取决于您打算如何处理。

You should treat Javascript validation as a "bonus" where PHP validation is a must. 您应该将Javascript验证视为“奖励”,其中必须进行PHP验证。

Is better that to use empty() instead of isset() on input validation, since will return TRUE even user does not key in any value. 最好在输入验证时使用empty()而不是isset(),因为即使用户不键入任何值也将返回TRUE。

if( !( 
        empty($_POST['inputTitle']) || 
        empty($_POST['inputName']) || 
        empty($_POST['inputSurname']) || 
        empty($_POST['inputEmail']) || 
        empty($_POST['inputLinks']) 
    ) ) {
        // continue validate inputEmail by using regular expression
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM