PDA

View Full Version : PHP problems....


BlindPilot
06-17-05, 06:20 AM
Hi,

Im trying to teach myseld PHP, but think I may have hit a snag..

Ive made a form which passes data onto a PHP page.

I know the code is working on the form, as if I set its method to get I can see the correct data being passed along in the URL.

However, my php page is telling me that the variables are undefined.

THe tutorial ive used doesnt mention anything about this and seems to assume that the variables, in this case:

UserName

and

UserChoice

will be picked up automatically by my variables in the PHP:

$UserName
$UserChoice

Ive checked to make sure all cases / spellings are correct.

My HTML page looks like this:


<html>
<head>
<title>My Form</title>
</head>
<body>

<form action="formerphp.php" method=post>

User name:
<br> <input type="text" name="UserName">

<p> Desired nickname:
<br> <input type="text" name="UserChoice">
<p>

<input type="submit" name="submit" value="Enter My Data!">
</form>

</body>
</html>


The php is this:


<html>
<head>
<title>Choices.</title>

</head>

<body>

<p>
Hi <?php print $UserName; ?>

<p>
Nickname: <b> <?php print $UserChoice; ?> !?! </b>

</body>
</html>



any ideas where im going wrong?

Could it be something to do with the set-up of my PHP-IIS jobby??

cyberskye
06-17-05, 10:55 AM
I use echo instead of print - I won't go into details, but print is a construct and eho is a php function. *May* be related to that, but I can't say for sure.

Let's verify your php is set up correctly. create a file called info.php. Only content should be:
<?php
phpinfo();
?>

Then open in your browser - this will give you the details of you php/http configuration.

Skye

EDIT- when you pass form data to a php script, it does automatically create a variable using the form name - as long as a certain config value (register_globals = On) is set. This has security implications and is off by default.

Instead of $UserName, try $_POST['UserName'] and see if that works.

Skye

TonyT
06-17-05, 02:50 PM
// get posted data into local variables
$UserName = Trim($_POST[UserName]);

cyberskye
06-17-05, 04:37 PM
That'll work too -

You can also use extract($_POST); to assign each element to it's own variable. Just make sure you call extract() before you try working with the indovidual variables.

EDIT - pretty sure you need ticks inside the brackets to indicate an array element:

$UserName = trim($_POST['UserName']);

Skye

TonyT
06-18-05, 05:17 AM
it works w/out the single quotes inside brackets, this is my generic contact script:

<?php

// get posted data into local variables
$IP = Trim($_POST[IP]);
$SenderEmail = Trim($_POST[SenderEmail]);
$EmailTo = "john_doe@domain.net";
$Subject = Trim($_POST[Subject]);
$First = Trim($_POST[First]);
$Last = Trim($_POST[Last]);
$Areacode = Trim($_POST[Areacode]);
$Phone = Trim($_POST[Phone]);
$Street = Trim($_POST[Street]);
$City = Trim($_POST[City]);
$State = Trim($_POST[State]);
$Zip = Trim($_POST[Zip]);
$Message = Trim($_POST[Message]);

// validation
$validationOK=true;
if (Trim($SenderEmail)=="") $validationOK=false;
if (Trim($Subject)=="") $validationOK=false;
if (Trim($First)=="") $validationOK=false;
if (Trim($Last)=="") $validationOK=false;
if (Trim($Areacode)=="") $validationOK=false;
if (Trim($Phone)=="") $validationOK=false;
if (Trim($Street)=="") $validationOK=false;
if (Trim($City)=="") $validationOK=false;
if (Trim($State)=="") $validationOK=false;
if (Trim($Zip)=="") $validationOK=false;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=mail_error.html\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "IP Address: ";
$Body .= "$IP";
$Body .= "\n";
$Body .= "First: ";
$Body .= "$First";
$Body .= "\n";
$Body .= "Last: ";
$Body .= "$Last";
$Body .= "\n";
$Body .= "Areacode: ";
$Body .= "$Areacode";
$Body .= "\n";
$Body .= "Phone: ";
$Body .= "$Phone";
$Body .= "\n";
$Body .= "Street: ";
$Body .= "$Street";
$Body .= "\n";
$Body .= "City: ";
$Body .= "$City";
$Body .= "\n";
$Body .= "State: ";
$Body .= "$State";
$Body .= "\n";
$Body .= "Zip: ";
$Body .= "$Zip";
$Body .= "\n";
$Body .= "Message: ";
$Body .= "$Message";
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$SenderEmail>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=mail_sent.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=mail_error.html\">";
}
?>

cyberskye
06-18-05, 12:35 PM
Gotcha - since you are using the key name as your variable name, you could use extract() to replace the assignment list. If you were changing names like this:

$User = trim($_POST['UserName'];

...then you would need to write it out as you did. Since BlindPilot only has two variables, either way is about the same effort and size.

Skye

BlindPilot
06-20-05, 03:27 AM
Cool, thanks for your help guys. I'll give what youve shown me here a go today when I get chance and let you know how I get on. :thumb: