SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying Errors and Redirecting a Form

    Can the following be done entirely with PHP and not using Javascript...

    There is a form that has a required field.

    When the user clicks the "Submit" button...

    1.) If the required field was left blank, then the form should re-appear with a warning message next to the omitted field stating, ERROR: Field must contain a value.

    2.) If the form was filled out correctly, then the form should re-direct to another page (which is the next step in whatever process).

    If so, what would the PHP code look like?

    Thanks,


    Amy

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that should be easier with PHP!
    Sample code(not tested):
    PHP Code:
    <?php
    $errors
    = array();
    if(isset(
    $_POST['submit']))
    {
      if(empty(
    $_POST['name']))
      {
       
    $errors['name']='Name is required';
      }
      
      if(empty(
    $_POST['email']))
      {
       
    $errors['email']='email is required';
      }  
      
      if(empty(
    $errors))
      {
    //validation success
         //send email, save to db ...
        
    header('Location: thankyou.html');
      }
    }
    ?>
    <html>
    <head>
    </head>
    <body>
    <form method='post'>
    Name: <input name='name' type='text'><?php if(isset($errors['name'])) echo $errors['name']; ?><br>
    Email: <input name='email' type='text'><?php if(isset($errors['email'])) echo $errors['email']; ?>
    </form>


    </body>
    </html>
    This PHP form validation script can be used for easier validation.
    Javascript form validation gives quicker response.

  3. #3
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code snippet.

    Does anyone else have a different approach?

    This seems to work, other than it needs to be fleshed out like making the form sticky.


    Amy

  4. #4
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    prasanthmj,

    I adapted your code but it doesn't seem to be working?!

    I stepped through my code in NetBeans and the array is receiving a value...

    Code:
    <?php
      // Start output buffering
      //ob_start();
    
      // Initialize a session
      session_start();
    
      // Initialize $errors array
      $errors= array();
    
      if (isset($_POST['submitted'])) {
        // Handle the form.
        echo "For was submitted!";
    
        if(empty($_POST['email'])) {
          $errors['name']='E-mail is required';
        }
        if(empty($errors)) {
          //validation success
          //send email, save to db ...
          header('Location: thankyou.html');
        }
      }
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
    <head>
      <title>Sign-In/Create Account</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <!--<link rel="stylesheet" type="text/css" href="tabs.css" />-->
      <style type="text/css">
      </style>
    </head>
    <body>
          <h2>Registering is quick and easy...</h2>
    
    <!--      <form action="104_CreateUser.xhtml" method="post">  -->
          <form action="103_SignIn.php" method="post">
            <ol>
              <!-- a.) E-MAIL ADDRESS -->
              <li class="step">
                <label for="email">Enter your e-mail address:</label>
                <input id="email" type="text" name="email" size="30" maxlength="80" />
                <?php if(isset($errors['email'])) echo $errors['email']; ?>
              </li>
            </ol>
    
            <!-- HIDDEN INPUT -->
            <input type="hidden" name="submitted" value="TRUE" />
    
            <!-- PROCEED BUTTON -->
            <input type="image" src="images/Proceed.jpg"
                   alt="Proceed" class="proceed" border="0" />
          </form>
        </div>
      </div>
    </body>
    </html>
    What am I doing wrong?


    Amy

  5. #5
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Saving a value here...

    if(empty($_POST['email'])) {
    $errors['name']='E-mail is required';
    }

    But then referencing it here...

    <label for="email">Enter your e-mail address/label>
    <input id="email" type="text" name="email" size="30" maxlength="80" />
    <?php if(isset($errors['email'])) echo $errors['email']; ?>


    Gets ya everytime?!


    Thanks,


    Amy

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the core logic.

    Make sure to call exit; after sending a redirect header
    Make sure to use htmlspecialchars() if you output those variables back into the form.
    Don't use empty() if a falsy value might be acceptable. For example, 0. strlen(trim(...)) is usually reasonable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •