# Please Help with PHP script



## rev1976 (Aug 11, 2004)

Hey there,

Still a newbie to web development. Having problems with my PHP script. It's suppose to validate the form and if any required fields are missing a error html page will open and notify the user. If all the fields are filled in the script will check the email and then send a confirmation to the user an email as well to myself and a thank you html page will launch. Now when i press the submit button it called the PHP script but nothing happens. Please help...Thank you in advance. I've pasted a copy of the code

rev1976
________________________________

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<?php
$firstName=$_REQUEST['firstName'];
$lastName=$_REQUEST['lastName'];
$email=$_REQUEST['emailAddy'];
$dayPhone=$_REQUEST['dayPhone'];
$evenPhone=$_REQUEST['evenPhone'];

$allinfo=nl2br($allinfo);

if (!isset($_REQUEST['emailAddy'])) {
header("Location: http://www.gettingyouhired.com/purchase.htm");
}
elseif (empty($email) || empty($firstName) || empty($lastName) || empty($dayPhone) || empty($evenPhone)){
header(("Location: http://www.gettingyouhired.com/error.html");
}
else{

// Build message parts
$recipient = "[email protected]";
$subject = "Request From Website";
$message = "Name: $name\nEmail: $email"; //\nAltSubject 
$from = "$email";

//this part is sending the email to the client
mail($email, "Thank You for Your Request", "Dear ".$name."\n
you contacted us by using the form....", "From: [email protected]\nReturn-path: <[email protected]>\n");

// Send mail
mail($recipient,$subject,$message,$from);

header("Location: http://www.gettingyouhired.com/thankYou.html");

} 
?>


----------



## brendandonhu (Jul 8, 2002)

Quite a few problems with that script.
First of all, get rid of the DOCTYPE at the beginning. You can't send output to the browser then later send a header. Thats what making the script not do anything. You are also using the $_REQUEST superglobal, which is pretty ancienct PHP. You should probably be using $_POST instead. You are also using lots of double-quotes where single quotes would be more appropriate.


----------



## rev1976 (Aug 11, 2004)

Thanks for the suggestions brandon. How would I write the script to perform my desire results?


----------



## brendandonhu (Jul 8, 2002)

I didn't test this but it ought to work. I see in your script $allinfo=nl2br($allinfo). Where is this $allinfo supposed to be coming from.	
<?php
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$email=$_POST['emailAddy'];
$dayPhone=$_POST['dayPhone'];
$evenPhone=$_POST['evenPhone'];

if (!isset($_POST['emailAddy'])) {
header('Location: http://www.gettingyouhired.com/purchase.htm');
}
elseif (empty($email) || empty($firstName) || empty($lastName) || empty($dayPhone) || empty($evenPhone)){
header(('Location: http://www.gettingyouhired.com/error.html');
}
else{
// Build message parts
$recipient = '[email protected]';
$subject = 'Request From Website';
$message = "Name: $name\nEmail: $email";
$from = "$email";
//this part is sending the email to the client
mail($email, "Thank You for Your Request", "Dear ".$name."\n
you contacted us by using the form....", "From: [email protected]\nReturn-path: <[email protected]>\n");
// Send mail
mail($recipient,$subject,$message,$from);
header('Location: http://www.gettingyouhired.com/thankYou.html');
} 
?>


----------



## rev1976 (Aug 11, 2004)

Thanks again Brendan. So, basically you took out all the double quotes and changed them to single quotes? so the header function i coded is correct?


----------



## brendandonhu (Jul 8, 2002)

Yep, pretty much,


----------



## rev1976 (Aug 11, 2004)

Thanks for your help Brendan...BIG TIME.


----------



## rev1976 (Aug 11, 2004)

Hey Brendan,

Still doesn't work. hmmmm


----------



## brendandonhu (Jul 8, 2002)

Is there an error message? Are the emails being sent? And are there blank spaces or lines at the beginning or end of the PHP File?


----------



## rev1976 (Aug 11, 2004)

The php file is being called but a blank screen comes up. I try to run some test cases ie: leave out a required field.


----------



## deuce868 (Nov 2, 2000)

You have an extra ( in this line in your elseif section

header(('Location: http://www.gettingyouhired.com/error.html');


----------



## rev1976 (Aug 11, 2004)

Ok sorry to bother your guys again.......i changed the code around....when i first tested the code everything was fine then i added some more test condition to check if certain fields were empty but for some reason when i implemented the changes it doesn't work anymore...i'm so lost. I think there's something from with my IF condition statement. Here's the updated code:

<?php
ob_start();
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$email=$_POST['emailAddy'];
$dayPhone=$_POST['dayPhone'];
$evenPhone=$_POST['evenPhone'];
$address1=$_POST['addy1'];
$address2=$_POST[['addy2'];
$interview=$_POST['interviewChoices'];
$resume=$_POST['resumeChoices'];
$school=$_POST['schoolName'];
$program=$_POST['program'];
$gradYr=$_POST['gradYr'];
$city=$_POST['city'];
$postal=$_POST['postal'];

if (!isset($_POST['emailAddy'])) {
echo '';
}
elseif (empty($email) || empty($firstName) || empty($lastName) || empty($dayPhone) || empty($evenPhone)) || empty($school) || empty($program) || empty($gradYr)) {

echo '';
exit();
}
else{
// Build message parts
$recipient = '[email protected]';
$subject = 'Request From Website';
$message = "Name: $firstName $lastName\nAddress: $addy1 $addy2\nCity: $city\nPostal Code: $postal\nDay Phone: $dayPhone\nEvening Phone: $evenPhone\nEmail: $email\nSchool: $school\nYear: $gradYr\nProgram: $program\nCourse Selection - Interview Help: $interview Resume Help: $resume";
$from = "$email";

//this part is sending the email to the client

mail($email, "gettingyouhired registration", "Dear $firstName $lastName\nThank you for registering with us. Here is purchase info:\nName: $firstName $lastName\nAddress: $addy1 $addy2\nCity: $city\nPostal Code: $postal\nDay Phone: $dayPhone\nEvening Phone: $evenPhone\nEmail: $email\nSchool: $school\nYear: $gradYr\nProgram: $program\nCourse Selection - Interview Help: $interview Resume Help: $resume", "From: [email protected]\nReturn-path: <[email protected]>\n");

// Send mail

mail($recipient,$subject,$message,$from);

echo '';
exit();
} 
?>


----------



## brendandonhu (Jul 8, 2002)

You are forwarding to purchase.html if the variable emailAddy is empty. Is this what you wanted? Then you are sending them to error.html if the other fields are empty. The other problem would be that you called ob_start(), but you never called ob_end_flush(). I don't see any reason to use output buffering, or meta refresh. The meta refresh will not work for some people, header() will work for just about everyone. And you went back to using double quotes where single quotes will be faster for PHP to process.


----------



## rev1976 (Aug 11, 2004)

Thanks....but for some reason header() doesn't work for me that's why i'm using the meta refresh. meta refresh doesn't work on all browsers? Ya it was a habit to use double quotes....i'll change it to single quotes thanks for the tip. You wouldn't be able to test the code with the header() function would you? Thanks again.


----------



## brendandonhu (Jul 8, 2002)

Meta refresh won't work for some people. Header() should work, can you post the exact code you were trying to use with it? And again any particular reason for the ob_Start()


----------



## rev1976 (Aug 11, 2004)

You can replace the echo '';

with header("Location: http://www.gettingyouhired.com/thankYou.html");

I don't know why i put the ob_start() in there. I just starting learning PHP 3 days ago. If you can get the header() to work please let me know. THanks.


----------



## brendandonhu (Jul 8, 2002)

Pretty good for 3 days 

<?php
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$email=$_POST['emailAddy'];
$dayPhone=$_POST['dayPhone'];
$evenPhone=$_POST['evenPhone'];
$address1=$_POST['addy1'];
$address2=$_POST[['addy2'];
$interview=$_POST['interviewChoices'];
$resume=$_POST['resumeChoices'];
$school=$_POST['schoolName'];
$program=$_POST['program'];
$gradYr=$_POST['gradYr'];
$city=$_POST['city'];
$postal=$_POST['postal'];
if (!isset($_POST['emailAddy'])) {
header('http://www.gettingyouhired.com/purchase.htm');
}
elseif (empty($email) || empty($firstName) || empty($lastName) || empty($dayPhone) || empty($evenPhone)) || empty($school) || empty($program) || empty($gradYr)) {
header('http://www.gettingyouhired.com/error.html');
}
else{
// Build message parts
$recipient = '[email protected]';
$subject = 'Request From Website';
$message = "Name: $firstName $lastName\nAddress: $addy1 $addy2\nCity: $city\nPostal Code: $postal\nDay Phone: $dayPhone\nEvening Phone: $evenPhone\nEmail: $email\nSchool: $school\nYear: $gradYr\nProgram: $program\nCourse Selection - Interview Help: $interview Resume Help: $resume";
$from = "$email";

//this part is sending the email to the client

mail($email, 'gettingyouhired registration', 'Dear $firstName $lastName\nThank you for registering with us. Here is purchase info:\nName: $firstName $lastName\nAddress: $addy1 $addy2\nCity: $city\nPostal Code: $postal\nDay Phone: $dayPhone\nEvening Phone: $evenPhone\nEmail: $email\nSchool: $school\nYear: $gradYr\nProgram: $program\nCourse Selection - Interview Help: $interview Resume Help: $resume", "From: [email protected]\nReturn-path: <[email protected]>\n");

// Send mail
mail($recipient,$subject,$message,$from);
header('http://www.gettingyouhired.com/thankYou.html');
} 
?>

The important part about using header() is to make sure that you don't have spaces or blank lines at the beginning or end of your PHP file.


----------



## rev1976 (Aug 11, 2004)

Great!! Thanks a lot. I'll give the new code a try. Oh ya, is there a validation program or location where i can validate my code so that it'll work in most of the browers?

Thanks


----------



## brendandonhu (Jul 8, 2002)

PHP Code doesn't run on browsers.


----------



## rev1976 (Aug 11, 2004)

ummm i guess i wasn't clear. I just want to make sure that all my code will work on most of the browers
i know that it works for IE5 not sure about netscape or other browsers.


----------



## rev1976 (Aug 11, 2004)

Man i hate to be a bother but i can;'t get the header() to work. I made sure there were no spaces and that all the parameters were there. Still doesn't work. It opens the PHP page but it does redirect it to any of the location i specified.


----------



## brendandonhu (Jul 8, 2002)

I made a mistake in my code, the header() line should read:
header('Location: http://www.gettingyouhired.com/purchase.htm');


----------



## EtHeO (Jul 19, 2005)

geez, i tried everything, i just cant post the solution directly. I got the solution in the attached text file. Hope it helps.

Good luck

EtHeO
The new expert in town


----------



## brendandonhu (Jul 8, 2002)

Your attachment isn't working.


----------



## EtHeO (Jul 19, 2005)

You were right, I've fixed it


----------



## brendandonhu (Jul 8, 2002)

There Location: header doesn't require a newline, as shown at http://php.net/header

The problem in the earlier script was that
header('http://www.gettingyouhired.com/thankYou.html');
The "Location: " part was forgotten.


----------

