2014-04-13 50 views
1

我想驗證我的表單使用服務器端驗證。PHP如果其他條件基於驗證

現在我在做什麼,如果任何錯誤可從服務器端的變化form action這個http://localhost/sitename/其他http://localhost/sitename/addRegRecord

我與波紋管代碼,但如果沒有錯誤檢測到我的其他條件沒有工作嘗試。

<form name="main" method="post" action=" 
    <?php 
     if(isset($errName) == "" and isset($errAddress) == "" and isset($errEmail) == "" and isset($errPhone) == ""){ 
      echo 'http://localhost/sitename/addRegRecord'; 
     }else{ 
      echo 'http://localhost/sitename/'; 
     } 
    ?> 
"> 

想我要的是:

當用戶的文件了表格,然後點擊提交,並沒有錯誤檢測形式的行動補充一點:http://localhost/sitename/addRegRecord否則,如果一個錯誤是行動這檢測使用http://localhost/sitename/

我的代碼工作:

<?php 
    $errName  = ""; 
    $errAddress = ""; 
    $errEmail = ""; 
    $errPhone = ""; 

    if(isset($_POST["Submit"])){ 
     // Full Name must be letters, dash and spaces only 
     if(preg_match("/^[A-Za-z ]+$/", trim($_POST["name"])) == "") 
      $errName = '<p class="errText">Name must be from letters, dashes, spaces and must not start with dash</p>'; 
     // Address must be word characters only 
     if(preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/", trim($_POST["address"])) == "") 
      $errAddress = '<p class="errText">Address must be only letters, numbers or one of the following ". , : /"</p>'; 
     // Email mask 
     if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", trim($_POST["email"])) == "") 
      $errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)</p>'; 
     // Phone mask 1-800-998-7087 
     if(preg_match("/^\d{1}-\d{3}-\d{3}-\d{4}$/", trim($_POST["phone"])) == 0) 
      $errPhone = '<p class="errText">Phone must comply with this mask: 1-333-333-4444</p>'; 
    } 
?> 

<form name="main" method="post" action=" 
    <?php 
     if(isset($errName) == "" and isset($errAddress) == "" and isset($errEmail) == "" and isset($errPhone) == ""){ 
      echo 'http://localhost/sitename/addRegRecord'; 
     }else{ 
      echo 'http://localhost/sitename/'; 
     } 
    ?> 
"> 
<table width="500" border="0" cellpadding="4" cellspacing="0" bordercolor="#000000" bgcolor="#EDEFF1"> 
    </tr> 
    <tr align="center" bgcolor="#FD9003"> 
    <td colspan="2" bgcolor="#A6B39D">Registration Form</td> 
    </tr> 
    <tr> 
    <td>Name:</td> 
    <td> 
     <input name="name" type="text" size="50" maxlength="100" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>"> 
     <?php if(isset($errName)) echo $errName; ?> 
    </td> 
    </tr> 
    <tr> 
    <td>Address:</td> 
    <td> 
     <input name="address" type="text" size="50" maxlength="250" value="<?php if(isset($_POST['address'])){ echo $_POST['address']; } ?>"> 
     <?php if(isset($errAddress)) echo $errAddress; ?> 
    </td> 
    </tr> 
    <tr> 
    <td>Email:</td> 
    <td> 
     <input name="email" type="text" size="50" value="<?php if(isset($_POST['email'])){ echo $_POST['email']; } ?>"> 
     <?php if(isset($errEmail)) echo $errEmail; ?> 
    </td> 
    </tr> 
    <tr> 
    <td>Phone:</td> 
    <td> 
     <input name="phone" type="text" size="16" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>"> 
     <?php if(isset($errPhone)) echo $errPhone; ?> 
    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
</table> 
</form> 
+1

如果$ ERRNAME = 「」 會比較合適 –

+0

!你應該閱讀['isset'](http://www.php.net/manual/en/function.isset.php)手冊頁 –

回答

2

您也可以嘗試這樣的:

<form name="main" method="post" action=" 
    <?php 
     if(!empty($_POST['name']) and !empty($_POST['address']) and !empty($_POST['email']) and !empty($_POST['phone'])){ 
      echo 'http://localhost/sitename/addRegRecord'; 
     }else{ 
      echo 'http://localhost/sitename/'; 
     } 
    ?> 
"> 
+1

非常感謝你對我的作品:) – lumos

1

isset()

Returns TRUE if var exists and has value other than NULL, FALSE otherwise. 

所以你可以使用簡單的if(isset($errName)沒有==""或使用if(empty($errName))