Erik_Fischer Posted August 21, 2013 Share Posted August 21, 2013 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. Quote Link to comment Share on other sites More sharing options...
exeTrix Posted August 21, 2013 Share Posted August 21, 2013 (edited) 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 August 21, 2013 by exeTrix Quote Link to comment Share on other sites More sharing options...
Erik_Fischer Posted August 21, 2013 Author Share Posted August 21, 2013 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."; } Quote Link to comment Share on other sites More sharing options...
exeTrix Posted August 21, 2013 Share Posted August 21, 2013 (edited) 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 August 21, 2013 by exeTrix Quote Link to comment Share on other sites More sharing options...
Erik_Fischer Posted August 21, 2013 Author Share Posted August 21, 2013 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. Quote Link to comment Share on other sites More sharing options...
fastsol Posted August 21, 2013 Share Posted August 21, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.