...

View Full Version : Newbie to PHP trouble with passing form variables



StupidRalph
09-20-2003, 01:39 AM
I am wondering what is wrong with my script. I keep getting an error saying my variables are undefined. But I thought the variables take the value of the form elements. Is there additional code that I need to add? I am using php version 4.3.3. I have the default php.ini configurations considering i do not know how or what to change. Anywho my script is as follows:

<?PHP

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($favoriteColor == "--Select--")
$message = "Please choose a favorite color.";


if ($message) // If we've set a message with a warning about their form entry...
echo ($message);
else {
mail("MyEmail@MyDomain.com",
"Form Results",
"$customerName filled out the form and
selected a favorite color of $favoriteColor",
"From: $customerName <$customerEmail>");

echo ("Thank you. A representative will contact you shortly.");
die(); // We can stop if the form's been submitted and we've thanked them.
} // end if we're sending the email

} // end if they hit "Submit"

?>


<body>
<FORM METHOD = "post" action="formEMAILtest.php">

<B>Your Name:</B> <INPUT TYPE = "text" NAME = "customerName" /> <BR />
<B>Your Email:</B> <INPUT TYPE = "text" NAME = "customerEmail" /> <BR />

<B>Your Favorite Color:</B>
<SELECT SIZE = "1" NAME = "favoriteColor">
<OPTION selected>--Select--</OPTION>
<OPTION>Red</OPTION>
<OPTION>Green</OPTION>
<OPTION>Blue</OPTION>
</SELECT> <BR />

<INPUT TYPE = "submit" NAME = "submit" VALUE = "Submit Form"/> <BR />

</FORM>
</body>


This is just a test form I don't care about ppl fav. color LoL.

Acecool
09-20-2003, 02:58 AM
Why die(); ?

Should put if ( $submit ) {
// Check the submission
}
else
{
// Show the form
}


EDIT:

For each of the vars try using
$oldvar = $_POST['oldvar'];

StupidRalph
09-20-2003, 04:09 AM
thanx I'll try that now :)

DigitaLink
09-20-2003, 05:55 AM
Yeah, in the old days we could get variables passed by forms just by using $variable_name ... but in the great NEW days here, we use $_POST[variable_name], $_GET[variable_name], $_SESSION[variable_name], etc.

It seems like a bit of a pain compared to the old way, but TRUST me, it's MUCH better this way! Because at some point if you start mixing $_POST[username] with $_SESSION[username] with $username all in the same script, you can not only be fairly sure you end up using the right variable, you can be fairly sure the wrong one doesn't get overwritten with the wrong data. :D

Acecool
09-20-2003, 06:08 AM
And then forging login etc, and instead of a SQL query $blah people can use the GET without it being a GET

so it is btter and a bit more secure

StupidRalph
09-20-2003, 06:38 PM
Is this correct? It keeps telling me that message has and undefined index. I lost some of the formatting due to adding color but here is the code.

<?PHP

$submit=$_POST['submit'];
$customerName=$_POST['customerName'];
$customerEmail=$_POST['customerEmail'];
$message= $_POST['message']; $favoriteColor=$_POST['favoriteColor'];

if ($submit) { // If they've submitted the form...


if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($favoriteColor == "--Select--")
$message = "Please choose a favorite color.";


if ($message) // If we've set a message with a warning about their form entry...
echo ($message);
else {
mail("RThomas [at] Safrico.com",
"Form Results",
"$customerName filled out the form and
selected a favorite color of $favoriteColor",
"From: $customerName <$customerEmail>");

echo ("Thank you. A representative will contact you shortly.");
} // end if we're sending the email

} // end if they hit "Submit"


?>

Acecool
09-20-2003, 06:53 PM
<?PHP

$submit=$_POST['submit'];
$customerName=$_POST['customerNam
e'];
$customerEmail=$_POST['customerEm
ail'];
$message= $_POST['message']; $favoriteColor=$_POST['favoriteCo
lor'];

if ($submit) { // If they've submitted the form...


if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($favoriteColor == "--Select--")
$message = "Please choose a favorite color.";


if ($message) // If we've set a message with a warning about their form entry...
echo ($message);
else {
mail("RThomas [at] Safrico.com",
"Form Results",
"$customerName filled out the form and
selected a favorite color of $favoriteColor",
"From: $customerName <$customerEmail>");

echo ("Thank you. A representative will contact you shortly.");
} // end if we're sending the email

} // end if they hit "Submit"


?>

You can use the PHP tags, lemme read through

Try making each var global, EX:
global $submit,$customerName,$customerEmail,$message;


Also checking if they entered a name etc use

if (trim($thenamevar) == "") {

trim removes spaces where they arent needed, beginning and end of text

StupidRalph
09-25-2003, 02:29 AM
I tweaked it a lil and got it to work out thanx.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum