2014-02-28 121 views
-4

我想在包含另一个PHP之前验证输入,以便用户可以在同一页面上获取错误(如果有的话).. 4个输入是必需的,但其他是可选的..我想要做的是,如果用户只填写4个所需的输入,则验证输入比包含另一个PHP文件(而不是包含PHP,而不是包含PHP,而是使用JavaScript来提醒我代码正常运行..),如果用户还填写其他可选输入以验证它们并包含PHP文件我遇到的问题是即使用户插入无效字符,它仍然会提醒我代码很好。 我只希望它来处理最后的JavaScript,如果用户在允许的字符输入罢了..如何使用php验证html表单

这里的PHP:

<?php 
// define variables and set to empty values 
$titleErr = $MdescripErr = $posterErr = $vcodeErr = $vcode2Err = $vcode3Err = $mlink1Err = $mlink2Err = $mlink3Err = ""; 
$title = $Mdescrip = $poster = $comment = $vcode = $vcode2 = $vcode3 = $mlink1 = $comment = $mlink2 = $mlink3 = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    if (empty($_POST["title"])) 
    {$titleErr = "title is required";} 
    else 
    { 
    $title = test_input($_POST["title"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[A-Za-z0-9 ]*$/",$title)) 
     { 
     $titleErr = "Only letters and white space allowed"; 
     } 
    } 

    if (empty($_POST["Mdescrip"])) 
    {$MdescripErr = "Movie Description is required";} 
    else 
    { 
    $Mdescrip = test_input($_POST["Mdescrip"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[A-Za-z0-9 ]*$/",$Mdescrip)) 
     { 
     $MdescripErr = "Only letters and white space allowed"; 
     } 
    } 


    if (empty($_POST["poster"])) 
    {$posterErr = "Poster Link is required";} 
    else 
    { 
    $poster = test_input($_POST["poster"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$poster)) 
     { 
     $posterErr = "Invalid URL"; 
     } 
    } 

    if (empty($_POST["vcode"])) 
    {$vcodeErr = "Embed Link is required";} 
    else 
    { 
    $vcode = test_input($_POST["vcode"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode)) 
     { 
     $vcodeErr = "Invalid URL"; 
     } 
    } 


    if (empty($_POST["vcode2"])) 
    {$vcode2 = "";} 
    else 
    { 
    $vcode2 = test_input($_POST["vcode2"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode2)) 
     { 
     $vcode2Err = "Invalid URL"; 
     } 
    } 

    if (empty($_POST["vcode3"])) 
    {$vcode3 = "";} 
    else 
    { 
    $vcode3 = test_input($_POST["vcode3"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode3)) 
     { 
     $vcode3Err = "Invalid URL"; 
     } 
    } 

    if (empty($_POST["mlink1"])) 
    {$mlink1 = "";} 
    else 
    { 
    $mlink1 = test_input($_POST["mlink1"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink1)) 
     { 
     $mlink1Err = "Invalid URL"; 
     } 
    } 

    if (empty($_POST["mlink2"])) 
    {$mlink2 = "";} 
    else 
    { 
    $mlink2= test_input($_POST["mlink2"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink2)) 
     { 
     $mlink2Err = "Invalid URL"; 
     } 
    } 

    if (empty($_POST["mlink3"])) 
    {$mlink3 = "";} 
    else 
    { 
    $mlink3 = test_input($_POST["mlink3"]); 
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL) 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink3)) 
     { 
     $mlink3Err = "Invalid URL"; 
     } 
    } 


} 

function test_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

if ($title == NULL || $Mdescrip == NULL || $poster == NULL || $vcode == NULL) 
{ 
} 
else if (!preg_match("/^[A-Za-z0-9 ]*$/",$title) ||!preg_match("/^[A-Za-z0-9 ]*$/",$Mdescrip) || !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$poster) || !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode)) 
{ 
} 
else 
    { 
?> 
<script> 

alert("it went through"); 

</script> 
<?php 
} 


?> 

回答

0

你应该尝试拆分URL并取出unnecesary项目像“https://”,所以你可以采取一个像“http://www.stackoverflow.com/bla/bla/bla”的网址,并将其分成[“http”,“”,“www.stackoverflow.com”,“bla”,“bla”,“bla”] 最后检查每个元素是否设置(有效),这会让你的验证非常简单