PDA

Click to See Complete Forum and Search --> : Simple Question


joefox
Sep 27th, 2006, 04:47 PM
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

kows
Sep 27th, 2006, 05:06 PM
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.

<?
$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
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 ( to open and 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.

joefox
Sep 28th, 2006, 09:01 AM
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?

kows
Sep 28th, 2006, 01:28 PM
..? why would you do that? that's pretty pointless...

just put them at the top of the handler file like they were before..

joefox
Sep 28th, 2006, 02:20 PM
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 :)

kows
Sep 28th, 2006, 03:21 PM
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.