Page 1 of 1

PHP form validation & returning output on same page

Posted: Thu Mar 18, 2010 3:43 pm
by khuzema
Hi folks
I had written below php script

Code: Select all

 
<?php
       function VerifyForm(&$values, &$errors)
       {
        if(strlen($values['name']) < 3)
        $errors['name'] = "Name too short";
        elseif(strlen($values['name']) > 50)
     $errors['name'] = 'Name too long';
     if(!ereg('.*@.*\..{2,4}', $values['email']))
     $errors['email'] = 'Email address invalid';
        if(strlen($values['text']) == 0)
     $errors['text'] = 'Text required';
      return(count($errors) == 0);
       }
  
      function DisplayForm($values, $errors)
      {
  ?>
  
      <html>
      <head>
      <title>Yadda yadda</title>
      <style>
      TD.error
      {
      color: red;
      font-weight: bold;
      }
      </style>
      </head>
      <body>
      <?php
  
      if (count($errors) > 0)
  
      echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
  
      ?>
      <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
      <table>
      <tr>
      <td>Name:</td>
      <td><input type="text" size="30" name="name" value="" />
      <td class="error"><?= $errors['name'] ?></td>
      </tr>
      <tr>
      <td>Email:</td>
      <td><input type="text" size="30" name="email" value="" />
      <td class="error"><?= $errors['email'] ?></td>
      </tr>
      <tr>
      <td valign="top">Text:</td>
      <td>
      <textarea name="text" cols="30" rows="6"> </textarea>
      </td>
      <td class="error"><?= $errors['text'] ?></td>
      </tr>
      <tr><td colspan="2" align="center"><input type="submit" value="Submit"></tr>
      </table>
      </form>
      </body>
      </html>
      <?php
      }
      function ProcessForm($values)
      {
      mail('foo@bar.com', 'Form test', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>");
      // Replace with actual page or redirect :P
      echo "<html><head><title>Thank you!</title></head><body>Thank you!</body></html>";
      }
      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
      $formValues = $_POST;
      $formErrors = array();
      if (!VerifyForm($formValues, $formErrors))
      DisplayForm($formValues, $formErrors);
      else
      ProcessForm($formValues);
      }
      else
      DisplayForm(null, null);
      ?>
 
When i am submitting the form i am getting following error :

Code: Select all

 
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server. 
 
If you think this is a server error, please contact the webmaster. 
 
Error 403
localhost
3/19/2010 1:44:53 AM
Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 
 
I am returning output on the same page using $_SERVER['PHP_SELF'] in the action attribute of the form & i think that this is creating some problem .
I will greatly appreciate any kind of help .

Re: PHP form validation & returning output on same page

Posted: Thu Mar 18, 2010 4:57 pm
by Reviresco
Try this:

Code: Select all

<form action="" method="POST">
or just:

Code: Select all

<form method="POST">

Re: PHP form validation & returning output on same page

Posted: Sat Mar 20, 2010 12:42 am
by khuzema
Thanks Reviresco
Now it is working . Do you know the reason whay it is not working with $_SERVER['PHP_SELF'] ?

Re: PHP form validation & returning output on same page

Posted: Sat Mar 20, 2010 9:34 pm
by ell0bo
Not knowing the structure of your site, I am going to guess you're doing some sort of URL rewrite to make clean URLs? That's the only thing I can think of that would have PHP_SELF different from what's on the browser's address.

For instance if you do index.php/foo/bar, I'm pretty sure PHP_SELF will simply be index.php, and thus you won't end up where you think you are.

Generally, if a form is posting back to itself, just leave the location blank.

Re: PHP form validation & returning output on same page

Posted: Sat Mar 20, 2010 11:05 pm
by khuzema
Thanks ell0bo