1

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

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/

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>
2
  • 1
    if $errName != "" would be more appropriate Commented Apr 13, 2014 at 12:37
  • you should read isset manual page Commented Apr 13, 2014 at 12:40

2 Answers 2

2

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/';
        }
    ?>
">
Sign up to request clarification or add additional context in comments.

Comments

1

[isset()][1]

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)) [1]: https://www.php.net/isset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.