Announcement

Collapse
No announcement yet.

Processing forms in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Processing forms in PHP

    Can anyone point me to a good resource as to how to process the input from html forms in PHP.

    Usual sort of thing really I need for someone to be able to fill in the form submit it, have it verified and any errors being reported back to the user and then stored somewhere or other.

    I asume this can be done easily in php right?
    Motorsport Forums

  • #2
    This is actually pretty simple because any form fields that you submit to a php script are automitically avaiable in a variable that mathces their name.

    Example: (yourform.html)
    Code:
    <form method="post" action="processform.php" name="">
    	<input type="text" name="name">
    	<br>
    	<input type="text" name="address">
    	<br>
    	<input type="text" name="age">
    	<br>
    	<input type="submit" name="Submit" value="Submit">
    </form>
    Now in your "processform.php" page you will automitically have a few variables to play with:
    $name, $address and $age

    These variables will contain the values that the user put into the form field in yourform.html

    Heres an example of something you could put in processform.php
    Code:
    <?
    if ((!name) || (!$address) || (!$age)){
    echo "Im sorry you forgot to fill in one or more of the required fields. Please go back and try again";
    exit;
    } else {
    echo " Your name is: $name 
              Your address is: $address
              You are $age years old";
      }
    ?>

    Or you could do something like:

    Code:
    <?
    if ((!name) || (!$address) || (!$age)){
    header("Location: http://www.yoursite.com/yourform.html");
    exit;
    } else {
    echo " Your name is: $name 
              Your address is: $address
              You are $age years old";
      }
    ?>
    (this example will simply take them back to the form rather than displaying an error)

    Hope This helps
    ~Chris


    Comment


    • #3
      Heh, looks nice!

      Can you do that sort of thing e.g. check that an email has an @ sign in it.

      I'm probably going to want to put it into a database (I haven't been told what database yet!), does PHP integrate well with databases other than mySQL such as Oracle perhaps? (Not that I know anything about Oracle!)



      Motorsport Forums

      Comment


      • #4
        ooh!, this stuff is nice

        one thing I've noticed with error checking as you know 80% of computing is error checking .

        I can send the form back to the user with error messages by just 'include'ing the form and then put e.g. value=$name in one of the inputs to retain the original values which the user entered (if you see what I mean).

        However I also have some radio buttons and checkboxes and would like form returned with an error message to maintain the input which the user had previously done. i.e. All inputs should be the same as they previously did - the only thing they should have to do is correct the error.

        Any clues?
        Motorsport Forums

        Comment


        • #5
          Here's the function to check email:

          You could have, in your processform.php file, have the lines:

          function check_email ($str) {
          //returns 1 if valid email, 0 if not

          if (ereg ("^[email protected]+\\..+$", $str)) {
          return 1;
          } else {
          return 0;
          }
          }

          Basically, the string: "^[email protected]+\\..+$" checks for [email protected] which is close enough to match [email protected]

          Comment


          • #6
            I love regular expressions!!

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X