简体   繁体   English

php if else基于验证的条件

[英]php if else condition based on validation

I am trying to validate my form using server side validation. 我正在尝试使用服务器端验证来验证我的表单。

Now what I am doing is, If any error is available from server side change form action to this http://localhost/sitename/ else http://localhost/sitename/addRegRecord 现在我正在做的是,如果从服务器端更改form action到此http://localhost/sitename/ else http://localhost/sitename/addRegRecord任何错误可用

I have tried with bellow code but when no error detect my else condition is not working. 我试过波纹管代码,但没有错误检测我的其他条件不起作用。

<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/';
        }
    ?>
">

Want I want is: 我想要的是:

When user file-up the form and click submit and no error detect add this in form action: http://localhost/sitename/addRegRecord else if single error is detect use this in from action http://localhost/sitename/ 当用户提交表单并单击提交并且没有错误检测时,请在表单操作中添加: http://localhost/sitename/addRegRecord else如果检测到单个错误,请在操作中使用此操作http://localhost/sitename/

My Code Work: 我的代码工作:

<?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>

You can also try with this: 你也可以尝试这个:

<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/';
        }
    ?>
">

isset() isset()函数

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

so you can use simply if(isset($errName) without =="" or use if(empty($errName)) 所以你可以简单地使用if(isset($errName) without ==""或者使用if(empty($errName))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM