Page 1 of 1

php forms validations

Posted: Sun Oct 19, 2008 5:24 pm
by fantomel
hello i have some problems with a php code i don't know how to do it :(

this is my actual code for the moment testing

Code: Select all

<form method="POST" action="form.php">
   Username: <input type="text" name="username" maxlength="10" />
   <? echo (isset($errors['username'])) ? $errors['username'] : '' ?><br />
   <input type="submit" name="submit" value="true" />
</form>
php code

Code: Select all

if($_POST['submit'] == 'true') {
   $errors = array();
   if(empty($_POST['username']) || strlen($_POST['username']) > 9) {
      $errors['username'] = 'Your username is not the right length';
   }
}
if i put

Code: Select all

echo (isset($errors['username'])) ? $errors['username'] : ''
in form.php it will work

but if i want to echo on the page with the form it doesn't work :( and i don't know how should i do the function to make the echo please can someone provide me with an explanation or a function ? thank you very much

Re: php forms validations

Posted: Sun Oct 19, 2008 5:41 pm
by aceconcepts

Code: Select all

 
if(isset($_POST['submit'])) {
   $errors = array();
   if(empty($_POST['username']) || strlen($_POST['username']) > 9) {
      $errors['username'] = 'Your username is not the right length';
   }
}
 

Re: php forms validations

Posted: Sun Oct 19, 2008 7:18 pm
by califdon
Are you perhaps expecting that PHP can detect what the user does, without returning the form variables to a new script? Remember, once a web page is sent to the browser, there is no more PHP code in it. You can do one or more of the following:
  • Validate your form data in the browser with Javascript.
  • Validate your form data after the form has been submitted, using PHP (but now you must reload a new page).
  • Use Ajax (combination of Javascript and PHP scripts working together) to validate form data and optionally modify elements in the page that's already loaded.
I don't know of any other alternatives.