2014-02-28 96 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」] 最後檢查每個元素是否設置(有效),這會讓你的驗證非常簡單