Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Posts posted by KevinM1

  1. My first post -- yay! :)

    It sounds like you want a sticky form, a kind of form that will alert the user that they did something wrong (like not filling in a form element) upon submission, without bringing them to an error page.  I made a simple one in my own attempts at learning PHP that seems to work well.  Of course, you'll need to modify it for your specific purpose, but it's simple enough to modify easily.  I recommend using regular expressions in the test statements, though.  I was using JavaScript to do it as a test exercise, but that's not a secure method of validation.  The files I include in my code (header.inc and footer.inc) are just basic XHTML.  They only contain the header info (like page title, any CSS, that sort of thing) and the closing tags for the footer (</body> and </html>).  Hope this helps!

    [code]<?php

    $page_title = 'Sticky Form Test';
    include('header.inc');

    $message = NULL;

    if(isset($_POST['submit'])){
      if(empty($_POST['username'])){
          $u = FALSE;
          $message .= "Please input your username!<br />";
      }

      else {
          $u = $_POST['username'];
      }

      if(empty($_POST['password'])){
          $p = FALSE;
          $message .= "Please input your password!<br />";
      }

      else {
          $p = $_POST['password'];
      }

      if(empty($_POST['email'])){
          $e = FALSE;
          $message .= "Please input your e-mail address!";
      }

      else {
          $e = $_POST['email'];
      }

      if($u && $p && $e){
          echo "You correctly submitted everything!";
      }
    }

    ?>

    <form id="inputform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <fieldset class="narrow"><legend>Please input your information</legend>
          <p><label for="username">Name:</label><input name="username" id="username" type="text" class="txt" value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
          <p><label for="password">Password:</label><input name="password" id="password" type="password" class="txt" /></p>
          <p><label for="email">E-mail Address:</label><input name="email" id="email" type="text" class="txt" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/></p>
          <p><input type="submit" name="submit" id="submit" value="Submit" /></p>
      </fieldset>
    </form>

    <?php
    if(isset($message)){
      echo "<div style='color: #FF0000;'>$message</div>";
    }

    include('footer.inc');
    ?>[/code]
×
×
  • 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.