Results 1 to 6 of 6

Thread: Simple Question

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Simple Question

    Simple error...
    I just wanted to make a simple program that gets data from one form and sends it to the next..but i get a weird error.
    I can pass values if i type in a value, but not if i have a check box, or a drop down box.

    Entry form
    <form action="/php/handle_form.php" method="post">
    <fieldset>Enter your information:

    <p><b>Name:</b><input type="text" name="name" size="20" maxlenght="40" /></p>
    <p><b>Model:</b><input type="text" name="model" size="20" maxlenght="20" /></p>

    <p><b>Model Information:</b>
    <input type ="radio" name="status" value="New" /> New
    <input type ="radio" name="status" value="Old" /> Old</p>

    <p><b>Year:</b>
    <select name="year">
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
    <option value="2006">2006</option>
    <option value="2007">2007</option>
    </select></p>

    <p><b>Model Comments:</b> <textarea name="comments" rows="3" cols="40"></textarea></p>

    </fieldset>
    <div align="center"><input type="submit" name="submit" value="Enter" />

    </form></div></p>




    handler form:
    <?php
    $name = $_REQUEST['name'];
    $model = $_REQUEST['model'];
    $comments = $_REQUEST['comments'];
    $_REQUEST['status'];
    $_REQUEST['year'];
    $_REQUEST['submit'];

    echo "<b>Action:</b><br>";
    //Print the submitted information
    echo "<b>$name</b>, has entered $model $year for the following comments:<p>$comments";

    ?>


    I get this error...
    Action:

    Notice: Undefined variable: year in C:\Inetpub\jf1\php\handle_form.php on line 20
    Joe Fox, has entered MCV7745 for the following comments:
    Here is a test for Commetns

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Simple Question

    you didn't define $year, $submit or $status. Also, using $_REQUEST is a bad idea unless you don't know which method the form is coming from (get, post, session, cookie), and in this case you are POSTing your form, so use $_POST instead.

    PHP Code:
    <?
      $name     = $_POST['name'];
      $model    = $_POST['model'];
      $comments = $_POST['comments'];
      $status   = $_POST['status'];
      $year     = $_POST['year'];
      $submit   = $_POST['submit'];
    ?>
    One suggestion: you can combine these two PHP files you have into one, which will provide the same functionality, if you'd like. You can just do this:

    PHP Code:
    <?php
      
    if(!isset($_POST['submit'])){
    ?>
        <!-- put your HTML form here -->
        <!-- change your <form>'s action to thisscriptsname.php -->
    <?
      }else{
        //put your handler form here
      }
    ?>
    Also, it's useful to use the PHP tags around snippets of code ([php] to open and [/php] to close), making it somewhat easier to read.

    edit: I forgot you asked about radio/checkboxes/drop downs. They do work, you just have to make sure you do them right. Also, to prevent any empty values, whenever you have a radio button like you do, add checked="checked" to the default value only so that it will automatically be checked when you load the page. If you're having problems with other drop downs/checkboxes, post the HTML you're using and what you're doing in your handler form to capture it. Otherwise, your drop down year selector should work fine now.
    Last edited by kows; Sep 27th, 2006 at 05:17 PM.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Simple Question

    thanks for the reply
    where do i put the variables i declared?

    cant i put them in another file, and just use the "include" stament?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Simple Question

    ..? why would you do that? that's pretty pointless...

    just put them at the top of the handler file like they were before..
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Simple Question

    Its not pointless...
    You can have 1 file that carries all your variables, so if any changes are made down the road, you can change them in 1 spot, not in 15 different files

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Simple Question

    yes, but in your case it's pointless because your variables are not static or even defined in the script by the programmer, they're being submitted through a form by a user, and anything submitted through a form is USUALLY going to be saved to a database or textfile for later use. You are, assuming from seeing what you've done so far, making something that posts comments. You will want to save this stuff and later bring it up for other people to be able to see. Your include page would be useless because if you included it on another page, the variables would be blank unless a form was submitted to them, and chances are high that that form was not using the same input values (name, model, status, etc) as the first one you made.

    edit: hope that makes sense to you.
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

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



Click Here to Expand Forum to Full Width