• home
  • forum
  • my
  • kt
  • download
  • PHP forms

    Author: 2007-08-11 17:41:13 From:

    Good morning all Base here!

    Coming to you with another tut in PHP smile.gif

    Today we are going to be learning about PHP Forms.

    We will build the form first...


    CODE
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <div align="center">
        Enter somthing:
        <input name="input" type="text" size="50">
        <br />
        <div align="right"><input name="submit" type="submit" value="GO!">
        </div>
      </div>
    </form>


    So lets give that a little explanation.

    I'm not going to teach you about the HTML form itself if you want to know more about those. look in the HTML tutorials section of the forums.

    There is only 1 bit of code here worth getting into:

    CODE
    <?php echo $_SERVER['PHP_SELF']; ?>


    So!

    If you were browsing the page index.php then it would echo:

    CODE
    <form action="index.php" method="post">


    Remember there are limitations to PHP_SELF such as if your browsing index.php?page=downloads

    Then it will still only echo index.php rather than ?page=downloads

    But i'm sure you can work around that tongue.gif

    Ok

    So onto our PHP

    CODE
    <?php
    // Start of our script


    if($_POST['submit']){
    // IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.

    $users_input = $_POST['input'];
    // Making a variable called users_input with the contents of the text box in it.

    // Echo'ing a small line of text including what you wrote.
    echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";

    // Else - saying that if you haven't clicked the submit button it won't do the PHP.
    } else {
    ?>


    So i think my comments are generally quite a good explanation but i will explain each line in more detail.

    What i like to do is a literal explanation i.e. if they click submit then create a variable

    That kind of stuff.

    So yeah! Here we go...

    "If they click the "submit" button then {
    Create variable called users_input with the contents of the form they just filled in then put on the screen:

    Many thanks for submitting the form. You typed <b>(What they typed)</b> into the text box.

    Otherwise do } { this:"

    So yeah...

    if($_POST['submit']){ = if the button called submit has been submitted.

    $users_input = $_POST['input']; = Make a variable called users_input with their input init.

    ***NOTE, make sure you understand the correlation between $_POST['input']; & <input name="input" ***

    echo "some words"; = Just shows the user what they typed in.

    } else { = Otherwise:

    Ok so i will now give you the entire PHP page..

    CODE
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Testing Forms</title>
    </head>

    <body>
    <?php
    // Start of our script


    if($_POST['submit']){
    // IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.

    $users_input = $_POST['input'];
    // Making a variable called users_input with the contents of the text box in it.

    // Echo'ing a small line of text including what you wrote.
    echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";

    // Else - saying that if you haven't clicked the submit button it won't do the PHP.
    } else {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <div align="center">
        Enter somthing:
        <input name="input" type="text" size="50">
        <br />
        <div align="right"><input name="submit" type="submit" value="GO!">
        </div>
      </div>
    </form>
    <?php
    }
    // End the script completely
    ?>
    </body>
    </html>


    Cheers people, hope you have learned somthing

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot