Jump to content

Sending error from separate file


Erik_Fischer

Recommended Posts

I'm trying to create a signup page and am currently working on the verification of input (checking if they're empty, etc).

 

I'm handling the PHP in a separate file (the action file) when the form is submitted:

<form method="POST" action="submit.php">
  <fieldset>
    <label>Username</label>
    <div class="form-group">
      <input name="username" class="span-default" type="text" placeholder="Choose a username">
    </div>
  </fieldset>
</form>

What I need to do is add the following code under the input tag shown above if an error is found:

<div class="form-warning">ERROR TEXT HERE</div>

This is a portion of my PHP code. How would I do what I specified above from a separate file (or is there a better way)?

$username = $_POST['username'];

if(empty($username)) {
  // action here
}

Please help. :(

Link to comment
Share on other sites

If you have the validation in a different file then you could utilise sessions to set whatever data you wanted and redirect the user back to the form page.

session_start(); //this need to be at the top of each page you're wishing to use sessions on

$validationObject = (object) array(
   'validationError' => false;
);

$username = $_POST['username'];

//run your validation here, I've done one as an example
if (empty($username)) {
   $validationObject->validationError = true;
   $validationObject->username = (object) array(
      'message' => 'User name is required',
      'value' => $username
   );
   //I know setting the value here seems like a waste of time but if you were validating an email then you'd want to return the attempt to the user
}

//before the logic for processing the form, here we're just checking for errors
if ($validationObject->validationError) {
   $_SESSION['validation'] = $validationObject;
   header('Location: whateverPageYourFormIs.php');
   die();
}

//process the form

//html starts here

Then for your form page:

<?php
session_start();

if (isset($_SESSION['validation']) && $_SESSION['validation']->validationError) {
   $validationObject = $_SESSION['validation'];
   //unset the validation object so it's not used more than once
   unset($_SESSION['validation']);
}

?>
<!-- HTML header here -->
<form method="POST" action="submit.php">
  <fieldset>
    <label>Username</label>
    <div class="form-group">
      <input name="username" class="span-default" type="text" placeholder="Choose a username" value="<?php echo (isset($validationObject, $validationObject->username)) ? $validationObject->username->value : ''; ?>">
<?php
if (isset($validationObject, $validationObject->username)) {
?>
        <div class="form-warning"><?php echo $validationObject->username->message; ?></div>
<?php
}
?>
    </div>
  </fieldset>
</form>

I haven't tested this so it might have a few errors, but I'm sure it'll point you in the right direction. It's not complicated and uses stdClass objects. You could create a validation wrapper which may make things a little neater.

 

Hope it help anyway, any problems then give us a shout :)

Edited by exeTrix
Link to comment
Share on other sites

Just wondering, how come something like this isn't working for me:

 

HTML:

<form method="POST" action="submit.php">
  <fieldset>
    <label>Username</label>
    <div class="form-group">
      <input name="username" class="span-default" type="text" placeholder="Choose a username">
      <?php
        if(isset($message)) {
          echo '<div class="form-warning">$message</div>';
        }
      ?>
    </div>
  </fieldset>
</form>

PHP:

$username = $_POST['username'];
$message = "";

if(empty($username)) {
  $message = "Field is empty. Try again.";
}
Link to comment
Share on other sites

Because you have used  single quotes. There are two solution to this problem and here they are (the first is a little more efficient):

echo '<div class="form-warning">' . $message . '</div>';

//OR

echo "<div class=\"form-warning\">{$message}</div>";
Edited by exeTrix
Link to comment
Share on other sites

 

Because you have used  single quotes. There are two solution to this problem and here they are (the first is a little more efficient):

echo '<div class="form-warning">' . $message . '</div>';

//OR

echo "<div class=\"form-warning\">{$message}</div>";

 

Gotcha. However, when I test it out and submit, nothing happens, it doesn't show the message.

Link to comment
Share on other sites

You need to put the validation php on the same page as the form if you want it to work.  Put the validation at the top of the page before the form.  The only other way would be to set the error messages into the session and echo them on the form page if they exist.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.