2014-04-13 22 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))