2013-05-17 150 views
0

只是一個快速的,我似乎無法發現,並希望其他人可以發現我的錯誤,我有一些PHP編寫的字符串驗證代碼,我遇到的問題是表單似乎在打開提交按鈕後立即驗證「customerfname」字段,只要表單打開,即使在按下提交按鈕之前也會顯示錯誤「請輸入您的名字」。有誰能發現我的錯誤?提交之前表單驗證

<?php 

$flag = false; 
$badchar = ""; 
$string = $_POST["customerfname"]; 
$string = trim($string); 
$length = strlen($string); 
$strmsg = ""; 

if (isset($_POST["submit"])) { 
    if ($length == 0) { 
     $strmsg = '<span class="error"> Please enter your first name</span>'; 
     $flag = true; 
     $valid = false;} 
    else if ($length > 30) { 
     $strmsg = '<span class="error"> Can not enter more than 30 characters</span>'; 
     $flag = true; 
     $valid = false; 
     } 
else{ 
    for ($i=0; $i<$length;$i++){ 
     $c = strtolower(substr($string, $i, 1)); 
     if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false){ 
      $badchar .=$c; 
      $flag = true; 
      $valid = false; 
     } 
    } 
    if ($flag) { 
     $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';} 
    } 
    if (!$flag) { 
     $valid = true; 
    } 
} 

?> 

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo"> 

<td><label for="customerfname">Customer First Name: </label></td> 
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td> 

<p><input type="submit" name="submit" value="Save Data"/>&nbsp;<input type="reset" value="Clear Form" /> 

</form> 
+0

學習正則表達式,你不會後悔的。 – elclanrs

+0

嘗試過,沒有問題...我注意到沒有發送$ _POST ['submit'],但是甚至沒有輸入驗證碼。 –

+3

你沒有提交按鈕? – Kuzgun

回答

0
<?php 
$flag = false; 
$badchar = ""; 
$strmsg = ""; 
if(isset($_POST['customerfname'])) 
{ 
$string = $_POST["customerfname"]; 
$string = trim($string); 
$length = strlen($string); 
} 
if (isset($_POST["submit"])) { 
if ($length == 0) { 
$strmsg = '<span class="error"> Please enter your first name</span>'; 
$flag = true; 
$valid = false;} 
else if ($length > 30) { 
$strmsg = '<span class="error"> Can not enter more than 30 characters</span>'; 
$flag = true; 
$valid = false;} 
else { 
for ($i=0; $i<$length;$i++){ 
    $c = strtolower(substr($string, $i, 1)); 
    if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false){ 
     $badchar .=$c; 
     $flag = true; 
     $valid = false; 
    } 
} 
if ($flag) { 
    $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';} 
} 
if (!$flag) { 
    $valid = true;} 
    var_dump($valid); 
} 

?> 

<form method="POST" action="" id="custinfo"> 

<td><label for="customerfname">Customer First Name: </label></td> 
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td> 
<td><input type="submit" name="submit" /></td> 

</form> 
+0

那我該怎麼寫呢? – AJJ

+0

如果$ _POST滿足您的要求 – user2117650

+0

,並且在您不應該寫這樣的內容時顯示您的評論,則我vardumped $有效: <?php if(isset($ strmsg)){echo $ strmsg; }?> – user2117650

0

您應該添加一個提交按鈕與「提交」作爲名稱,並將所有關於POST請求的線下您的if (isset($_POST["submit"]))

這裏是工作(還有很多東西要改善,但這是一個開始)你的代碼版本:

<?php 

$flag = false; 
$badchar = ""; 
$strmsg = ""; 

if (isset($_POST["submit"])) { 

    $string = (isset($_POST["customerfname"])) ? $_POST["customerfname"] : ''; // if customerfname wasn't sent, $string will be empty 
    $string = trim($string); 
    $length = strlen($string); 

    if ($length == 0) { 
     $strmsg = '<span class="error"> Please enter your first name</span>'; 
     $flag = true; 
     $valid = false; 
    } 
    else if ($length > 30) { 
     $strmsg = '<span class="error"> Can not enter more than 30 characters</span>'; 
     $flag = true; 
     $valid = false; 
    } 
    else { 
     for ($i=0; $i<$length;$i++) { 
       $c = strtolower(substr($string, $i, 1)); 
       if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false) { 
        $badchar .=$c; 
        $flag = true; 
        $valid = false; 
       } 
     } 
     if ($flag) { 
      $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>'; 
     } 
    } 
    if (!$flag) { 
     $valid = true; 
    } 
} 

?> 

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo"> 

<td><label for="customerfname">Customer First Name: </label></td> 
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td> 
<td><input type="submit" name="submit"></td> 

</form> 
+0

我確實有一個名爲「submit」的提交按鈕,目前PHP代碼的整個部分在(isset($ _ POST [「submit」]))中,但是我仍然嘗試了你的方法,但仍然沒有運氣。 – AJJ

+0

嘗試「if(empty($ string))」而不是「if($ length == 0)」。順便說一句你的PHP版本是什麼? – evuez

相關問題