Okie dokes - I’ve got this problem. A PHP contact form that sends the information from the form to the E-Mail address in the PHP code. Problem is that when you hit the “Send” button it doesn’t do anything…And I don’t know how to solve the problem!
Help would be appreciated with this problem…Which I anticipate is a very rookie mistake.
<?php
// Pick up the form data and assign it to variables
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$phone = htmlspecialchars($_POST['phone']);
$contactpref = htmlspecialchars($_POST['contactpref']);
$subject = htmlspecialchars($_POST['subject']);
$jobtype = htmlspecialchars($_POST['jobtype']);
$message = htmlspecialchars($_POST['message']);
// Build the email (replace the address in the $to section with your own)
$to = 'info@example.co.uk';
$subject = "$subject";
$messageContainer = "You have a new <b>E-Mail from:</b> $name <br/>
<b>The job type(s) selected:</b> $jobtype <br/>
<b>Their message is:</b> $message <br/>
<b>E-Mail:</b> $email <br/>
<b>Phone:</b> $phone <br/>
<b>They would prefer to be contacted by:</b> $contactpref <br/>";
$headers = "From: $email";
// Send the mail using PHPs mail() function
mail($to, $subject, $messageContainer, $headers);
// Redirect
header("Location: http://www.example.co.uk");
?>
Any help or pointers in the right direction would be appreciated.
Ugh. I was hoping you wouldn’t read that or reply to this…I feel so stupid and silly But thanks, the help was appreciated. I’m kicking myself right now and cursing into the air because of that!
Thanks, I always seem to get it confused with the type=“button” for some reason, maybe it’s because the Submit button is a button!
Now another problem has came up! After I hit the Submit button it goes to the processForm.php page but doesn’t show anything. So obviously the code in the processForm.php document isn’t working as it should…Any ideas on that one? The code is the same as the one posted in the original post.
The home page wasn’t showing after submitting the contact form. And the reason why is because I had put processForm.php in the same directory as the contact form Web page, and the contact form Web page action link was sending the data to a processForm.php document in the root of the site that didn’t exist. I moved it to the root of the site and it works…For the most part!
NEW PROBLEM! Haha…
The E-Mail only displays one of the job types that is checked on a check box and not any others that are also checked. When an image file has been uploaded using the “Choose File” button it doesn’t send it through to the E-Mail address. I’m really not too sure on the first issue, but on the “Choose File” upload I think I need some extra PHP in there that I’ve yet to discover!
I’ve added the enctype attribute and values but I don’t know exactly where I should be positioning the second part of the code you posted. Can you put it in the correct place of the code for the processForm.php code snippet for me please?
So far nothing has changed. It still only posts the one check box item in the E-Mail rather than multiple checked check boxes and the file upload doesn’t work either. But all of the help is appreciated by everyone, especially you, venkat6134, you’ve helped me a lot so far
Righty,
there are a few problems to sort out here Andrew.
1/ If you have 4 checkboxes with the same name - only the last one checked will be sent as it overwrites all the others.
You need to create an array of values:
Note the creates an array.
You then iterate through that array to get the values and add them to your message.
$jobtype = ''; // blank variable to be filled in
// loop through the jobtype array
foreach($_POST['jobtype'] as $type) {
$jobtype .= htmlspecialchars($type);
}
and the processing page - with the headers and mail commented out for now so you can see whats happening.
// Pick up the form data and assign it to variables
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$phone = htmlspecialchars($_POST['phone']);
$contactpref = htmlspecialchars($_POST['contactpref']);
$subject = htmlspecialchars($_POST['subject']);
$jobtype = ''; // blank variable to be filled in
// loop through the jobtype array
foreach($_POST['jobtype'] as $type) {
$jobtype .= htmlspecialchars($type);
}
$message = htmlspecialchars($_POST['message']);
// Build the email (replace the address in the $to section with your own)
$to = 'info@example.co.uk';
$subject = "$subject";
$messageContainer = "You have a new <b>E-Mail from:</b> $name <br/>
<b>The job type(s) selected:</b> $jobtype <br/>
<b>Their message is:</b> $message <br/>
<b>E-Mail:</b> $email <br/>
<b>Phone:</b> $phone <br/>
<b>They would prefer to be contacted by:</b> $contactpref <br/>";
$headers = "From: $email";
echo $messageContainer;
// Send the mail using PHPs mail() function
//mail($to, $subject, $messageContainer, $headers);
// Redirect
//header("Location: http://www.example.co.uk");
when you run the form you will get s print out of the POST’ed array and the FILES array.