php if else基于验证的条件

时间:2014-04-13 12:32:10

标签: php validation

我正在尝试使用服务器端验证来验证我的表单。

现在我正在做的是,如果服务器端有任何错误,请将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>

2 个答案:

答案 0 :(得分: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 :(得分:1)

isset()

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

因此您只需使用if(isset($errName)而不使用==""或使用if(empty($errName))