Forum Moderators: coopster

Message Too Old, No Replies

HTML and PHP form not working right

         

gazzafm

1:40 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Hi

I have a html form that uses php to send the particulars to our email. When we click on submit the form states its transfering to our website, thinks for a while then instead of redirecting to the thank you page it shows our web address and the name of the php file in the address bar.

ie www.example.com/output.php I cannot see an error! Any ideas please?

Here is the form

<form name="form1" method="post" action="output.php" enctype="multipart/form-data">
<input name="Name" id="Name" value="" size="20" class="fieldstyle" type="text"><input name="email" id="email" size="20" maxlength="100" class="fieldstyle" type="text">
<input name="Home_Phone" id="Home_Phone" value="" size="20" class="fieldstyle" type="text">
<Moderator's NOTE: more form fields here ...>
<input name="Submit" value="Submit" type="submit">
</form>


and here is the output.php file

?php

// Receiving variables
$Name = addslashes($_POST['Name']);
$email = addslashes($_POST['email']);
$Home_Phone = addslashes($_POST['Home_Phone']);
$Mobile_Phone = addslashes($_POST['Mobile_Phone']);
$Mailing_Address = addslashes($_POST['Mailing_Address']);
$City = addslashes($_POST['City']);
$State = addslashes($_POST['State']);
$Post_Code = addslashes($_POST['Post_Code']);
$Bath_Option = addslashes($_POST['Bath_Option']);
$Type_of_Listing = addslashes($_POST['Type_of_Listing']);
$Rooms = addslashes($_POST['Rooms']);
$Covered_Parking = addslashes($_POST['Covered_Parking']);
$What_Type = addslashes($_POST['What_Type']);
$Features = addslashes($_POST['Features']);
$Price_Selling = addslashes($_POST['Price_Selling']);
$Price_Negotiable = addslashes($_POST['Price_Negotiable']);
$Pet_Friendly= addslashes($_POST['Pet_Friendly']);
$Comments = addslashes($_POST['Comments']);
if(isset($_FILES['picture1']['tmp_name']))
{
$tmp_name1 = $_FILES["picture1"]["tmp_name"];
$name1 = str_replace(' ', '_', $Name.'_'.$_FILES["picture1"]["name"]);

if(!move_uploaded_file($tmp_name1, "homesales/$name1"))
{
$error1 = 'Could not move file 1';
}
}

if(isset($_FILES['picture2']['tmp_name']))
{
$tmp_name2 = $_FILES["picture2"]["tmp_name"];
$name2 = str_replace(' ', '_', $Name.'_'.$_FILES["picture2"]["name"]);
if(!move_uploaded_file($tmp_name2, "homesales/$name2"))
{
$error2 = 'Could not move file 2';
}
}

if(isset($_FILES['picture3']['tmp_name']))
{
$tmp_name3 = $_FILES["picture3"]["tmp_name"];
$name3 = str_replace(' ', '_', $Name.'_'.$_FILES["picture3"]["name"]);

if(!move_uploaded_file($tmp_name3, "homesales/$name3"))
{
$error3 = 'Could not move file 3';
}

if(isset($_FILES['picture4']['tmp_name']))
{
$tmp_name4 = $_FILES["picture4"]["tmp_name"];
$name3 = str_replace(' ', '_', $Name.'_'.$_FILES["picture4"]["name"]);

if(!move_uploaded_file($tmp_name4, "homesales/$name4"))
{
$error4 = 'Could not move file 4';
}

if(isset($_FILES['picture5']['tmp_name']))
{
$tmp_name5 = $_FILES["picture5"]["tmp_name"];
$name3 = str_replace(' ', '_', $Name.'_'.$_FILES["picture5"]["name"]);

if(!move_uploaded_file($tmp_name5, "homesales/$name5"))
{
$error5 = 'Could not move file 5';
}

if(isset($_FILES['picture6']['tmp_name']))
{
$tmp_name6 = $_FILES["picture6"]["tmp_name"];
$name6 = str_replace(' ', '_', $Name.'_'.$_FILES["picture6"]["name"]);

if(!move_uploaded_file($tmp_name6, "homesales/$name6"))
{
$error6 = 'Could not move file 6';
}
}
// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
header("Location: error.html");
exit;
}

//Sending Email to form owner
# Email to Owner
$pfw_header = "From: $email";
$pfw_subject = "Home Sale Advert Form";
// Change to your email address.........
$pfw_email_to = "webmaster@example.com";
//......................................
$pfw_message = "Name: $Name\n"
. "email: $email\n"
. "Home_Phone: $Home_Phone\n"
. "Mobile_Phone: $Mobile_Phone\n"
. "Mailing_Address: $Mailing_Address\n"
. "City: $City\n"
. "State: $State\n"
. "Post_Code: $Post_Code\n"
. "Bath_Option: $Bath_Option\n"
. "Type_of_Listing: $Type_of_Listing\n"
. "Rooms: $Rooms\n"
. "Covered_Parking: $Covered_Parking\n"
. "What_Type: $What_Type\n"
. "Features: $Features\n"
. "Price_Selling: $Price_Selling\n"
. "Price_Negotiable: $Price_Negotiable\n"
. "Pet_Friendly: $Pet_Friendly\n"
. "Comments: $Comments\n"
. "Image 1: $name1\n"
. "Image 2: $name2\n"
. "Image 3: $name3\n"
. "Image 4: $name4\n"
. "Image 5: $name5\n"
. "Image 6: $name6\n"
. "\n";
mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//Sending auto respond Email to user
# Email to Owner
// Change to your email address.........
$pfw_header = "From: webmaster@arpra.org.au";
//......................................
$pfw_subject = "Confirmation Home Sale Advert Auto - Response";
$pfw_message = "Name: $Name\n"
. "email: $email\n"
. "Home_Phone: $Home_Phone\n"
. "Mobile_Phone: $Mobile_Phone\n"
. "Mailing_Address: $Mailing_Address\n"
. "City: $City\n"
. "State: $State\n"
. "Post_Code: $Post_Code\n"
. "Bath_Option: $Bath_Option\n"
. "Type_of_Listing: $Type_of_Listing\n"
. "Rooms: $Rooms\n"
. "Covered_Parking: $Covered_Parking\n"
. "What_Type: $What_Type\n"
. "Features: $Features\n"
. "Price_Selling: $Price_Selling\n"
. "Price_Negotiable: $Price_Negotiable\n"
. "Pet_Friendly: $Pet_Friendly\n"
. "Comments: $Comments\n"
. "\n";


mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

header("Location: thank_you.html");


?>

[edited by: coopster at 3:01 pm (utc) on Sep 8, 2011]
[edit reason] showing relevant portion of form [/edit]

penders

2:30 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's quite a code dump!

I assume you do receive the form submission server-side and the emails are received OK?

header("Location: thank_you.html");


You should specify an absolute URL in the Location HTTP header. Also try including exit() immediately following your header() statement.

rocknbil

4:26 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's because, although you're creating error variables, you're not doing anything with them. It just continues on. Second, it's not very efficient or usable to create one variable for each error. Concatenate them into a string. I've shortened the code for example.

Also, all this
condition
{
//bla
}

does is waste lines. :-)

$errors = null;

if(!move_uploaded_file($tmp_name1, "homesales/$name1")) {
$errors .= '<li>Could not move file 1</li>';
}
// cut
if(!move_uploaded_file($tmp_name2, "homesales/$name2")) {
$errors .= '<li>Could not move file 2</li>';
}
//cut
if(!move_uploaded_file($tmp_name3, "homesales/$name3")) {
$errors .= '<li>Could not move file 3</li>';
}
//cut
if(!move_uploaded_file($tmp_name4, "homesales/$name4")){
$errors .= '<li>Could not move file 4</li>';
}
//cut
if(!move_uploaded_file($tmp_name5, "homesales/$name5")){
$errors .= '<li>Could not move file 5</li>';
}
// cut
if(!move_uploaded_file($tmp_name6, "homesales/$name6")){
$errors .= '<li>Could not move file 6</li>';
}
//cut
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email)){
$errors .= <li>Please enter a single valid email.</li>";
}
if ($errors) {
// DO NOT REDIRECT. Otherwise you have to pass error variables
// via $_SESSION or query string. Output your form HERE with user values intact.
$form = '
<p style="color:#ffff;font-weight:bold;">It appears there are some errors in your input:</p>
<ul>' . $errors . '</ul>
<p>Please make the appropriate corrections and try again.
<!-- do not use SELF, Goggle for PHP self vulerabilities -->
<form method="post" action="yourscript.php">
<p><label for="email">Email</label> <input type=""text" name="email" id="email" value="' . $email . '"></p>
<!-- etc -->
</form>
";
echo $form;
exit;
}

With that, it won't go on to the rest of the program. Error trapping is your friend. :-)