Jump to content

Recommended Posts

After entering the form data correctly, and when I click the 'Go Back To Main Page' link, how can I make it so that it doesn't display the error(s) again?

 

formvalidation.php:

 

<?php
      session_start();
?>

      <?php

      function VerifyForm(&$values, &$errors)
      {
      // Do all necessary form verification

      if (strlen($values['name']) < 3)
      $errors['name'] = '* Name too short';

      elseif (strlen($values['name']) > 50)
      $errors['name'] = '* Name too long';

      // Needs better checking 

      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></head>
      <body>
      <?php
      if (count($errors) > 0)
      $fontstart = "<font color=\"#FF0000\">";
      $fontend = "</font>";
      echo $fontstart."<p>There were some errors in your submitted form, please correct them and try again.</p>".$fontend;
      ?>
      <tr>
      <td class="error"><?= $fontstart.$errors['name'].$fontend ?></td><br>
      <td class="error"><?= $fontstart.$errors['email'].$fontend ?></td><br>
      <td class="error"><?= $fontstart.$errors['text'].$fontend ?></td><br>
      </tr>
      <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
      <table>
      <tr>
      <td>Name:</td>
      <td><input type="text" size="30" name="name" value="<?= htmlentities($values['name']) ?>"/>
      </tr>
      <tr>
      <td>Email:</td>
      <td><input type="text" size="30" name="email" value="<?= htmlentities($values['email']) ?>"/>
      </tr>
      <tr>
      <td valign="top">Text:</td>
      <td>
      <textarea name="text" cols="30" rows="6"><?= htmlentities($values['text']) ?></textarea>
      </td>
      </tr>
      <tr><td colspan="2" align="center"><input type="submit" value="Submit"></tr>
      </table>
      </form>
      </body>
      </html><?php
      }
      function ProcessForm($values)
      {
      //mail('$adminemail', 'Form test', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>");
      // Replace with actual page or redirect 
      echo "<html>\n<head><title>Thank you!</title></head>\n<body>\nThank you!\n<br>\nYour data would have been submitted if this were a real form! =D\n<br>\n<a href=\"./formvalidation.php\">Go Back To Main Page</a>\n<br>\n</body>\n</html>";
      }
      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
      $formValues = $_POST;
      $formErrors = array();
      if (!VerifyForm($formValues, $formErrors))
      DisplayForm($formValues, $formErrors);
      else
      ProcessForm($formValues);
      }
      else
      DisplayForm(null, null);
      ?>

Link to comment
https://forums.phpfreaks.com/topic/188935-form-validation-error-glitch-oo/
Share on other sites

Didnt work..

 

Here's what I have:

 

      function ProcessForm($values)
      {
      //mail('$adminemail', 'Form test', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>");
      // Replace with actual page or redirect <!-- s:P --><img src=\"{SMILIES_PATH}/icon_razz.gif\" alt=\"\" title=\"Razz\" /><!-- s:P -->
      echo "<html>\n<head><title>Thank you!</title></head>\n<body>\nThank you!\n<br>\nYour data would have been submitted if this were a real form! =D\n<br>\n<a href=\"./formvalidation.php\">Go Back To Main Page</a>\n<br>\n</body>\n</html>";
      }
      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
      $formValues = $_POST;
      $formErrors = array();
//      if (!VerifyForm($formValues, $formErrors))
      if (!(VerifyForm($formValues, $formErrors)) || !(array_key_exists('name', $formValues))){
      DisplayForm($formValues, $formErrors);
      }else{
      ProcessForm($formValues);
      }
      }
//      else{
//      DisplayForm(null, null);
//      }

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.