View Full Version : PHP form Help.
andyg113
02-08-2011, 08:10 PM
hello there,
I'm very new new to this so forgive me. I have a very simple form for a mailing list here. (http://www.onlyjoe.co.uk)
I need to know exactly what should go in the php file and how to link it with the form. I'm completely new so a step by step type thing would be amazing.
I'd like the forms contents to be emailed to mailinglist@onlyjoe.co.uk with the subject ADD TO ONLYJOE MAILING LIST and i'd also like the option for a success page.
Here's the html for the form incase you need it.
<center><form action="php file goes here" method="post">
<font face="courier new" color="Black"><b>Email:</b></font> <input type="text" name="email" /><input type="submit" name="submit" value="Join Mailing List" />
</form></center>
Please help,
Thanks for your time.
Andy
Schmoopy
02-08-2011, 08:33 PM
This is how I'd do it:
The code below is the file that you specify in the "action='php file goes here'" bit.
<?php
/* Step 1 - Check to see whether the user has submitted the form */
if(isset($_POST['submit'])) {
/* Step 2 - Check that the user has input something for their email */
// Using $email so it's easier to write
// The trim() function takes off any whitespace from the string
$email = trim($_POST['email']);
if(!empty($email)) {
/* Step 3 - Email isn't empty, send email */
// The email address to send to
$to = 'mailinglist@onlyjoe.co.uk';
// Message subject
$subj = 'ADD TO ONLYJOE MAILING LIST';
// Message content
$msg = $email . ' would like to be added to your mailing list.';
$headers = "From: Mailing List <mailing@onljoe.com>";
/* Step 4 - Send the email */
if(mail($to, $subj, $msg, $headers)) {
/* Redirect to success page if mail sent */
header('Location: success.html');
} else {
/* Redirect to a different page or do something else if it fails */
header('Location: error.html');
}
}
}
?>
Please not: This does not do any kind of email validation, this is more of an example of the process of sending the email. You should be able to find an email validation function with google, or I can post one if you don't have any luck.
Hope this helps you out.
andyg113
02-08-2011, 10:23 PM
At the moment, using your exact php code with my error and success urls it keeps directing me to the error page.
Do you kno why this might be?
djr33
02-08-2011, 10:32 PM
Did you change the code at all? That error means there is a typo-- a missing ( or { or something like that. But I don't see one in the original code. What does line 5 look like for you?
andyg113
02-08-2011, 10:45 PM
Did you change the code at all? That error means there is a typo-- a missing ( or { or something like that. But I don't see one in the original code. What does line 5 look like for you?
At the moment, i'm using the exact php code with my error and success urls it now keeps directing me to the error page.
Do you kno why this might be?
feel free to test here (http://onlyjoe.co.uk).
Apologies for the weird edit above by the way.
djr33
02-08-2011, 10:51 PM
You cannot view PHP code on a live site. Is there a possibility that you changed something while changing the URLs? Again, I don't see an error in the code above.
andyg113
02-08-2011, 10:51 PM
This is the exact code i'm using.
<?php
/* Step 1 - Check to see whether the user has submitted the form */
if(isset($_POST['submit'])) {
/* Step 2 - Check that the user has input something for their email */
// Using $email so it's easier to write
// The trim() function takes off any whitespace from the string
$email = trim($_POST['email']);
if(!empty($email)) {
/* Step 3 - Email isn't empty, send email */
// The email address to send to
$to = 'mailinglist@onlyjoe.co.uk';
// Message subject
$subj = 'ADD TO ONLYJOE MAILING LIST';
// Message content
$msg = $email . ' would like to be added to your mailing list.';
$headers = "From: Mailing List <mailinglistonlyjoe@gail.com>";
/* Step 4 - Send the email */
if(mail($to, $subj, $msg, $headers)) {
/* Redirect to success page if mail sent */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
} else {
/* Redirect to a different page or do something else if it fails */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
}
}
}
?>
djr33
02-08-2011, 10:53 PM
That's very strange. I still don't see an error. What happens if you add a space after the ifs?
andyg113
02-08-2011, 11:04 PM
That's very strange. I still don't see an error. What happens if you add a space after the ifs?
I added a space after the ifs heres the code:
<?php
/* Step 1 - Check to see whether the user has submitted the form */
if (isset($_POST['submit'])) {
/* Step 2 - Check that the user has input something for their email */
// Using $email so it's easier to write
// The trim() function takes off any whitespace from the string
$email = trim($_POST['email']);
if (!empty($email)) {
/* Step 3 - Email isn't empty, send email */
// The email address to send to
$to = 'mailinglist@onlyjoe.co.uk';
// Message subject
$subj = 'ADD TO ONLYJOE MAILING LIST';
// Message content
$msg = $email . ' would like to be added to your mailing list.';
$headers = "From: Mailing List <mailinglistonlyjoe@gmail.com>";
/* Step 4 - Send the email */
if (mail($to, $subj, $msg, $headers)) {
/* Redirect to success page if mail sent */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
} else {
/* Redirect to a different page or do something else if it fails */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
}
}
}
?>
Still no joy.
(sorry)
djr33
02-09-2011, 01:19 AM
Your website says "an error has occurred" but does not let us know any details.
Maybe Schmoopy will have an idea about how to fix it.
Schmoopy
02-09-2011, 07:32 AM
I put this on my server to check for any parse errors before posting it, so I know that's not the problem.
I'm almost 90% sure the problem will be with the mail() function. You can try turning error reporting on, and then putting a die statement before the redirect:
<?php
error_reporting(E_ALL);
/* Step 1 - Check to see whether the user has submitted the form */
if (isset($_POST['submit'])) {
/* Step 2 - Check that the user has input something for their email */
// Using $email so it's easier to write
// The trim() function takes off any whitespace from the string
$email = trim($_POST['email']);
if (!empty($email)) {
/* Step 3 - Email isn't empty, send email */
// The email address to send to
$to = 'mailinglist@onlyjoe.co.uk';
// Message subject
$subj = 'ADD TO ONLYJOE MAILING LIST';
// Message content
$msg = $email . ' would like to be added to your mailing list.';
$headers = "From: Mailing List <mailinglistonlyjoe@gmail.com>";
/* Step 4 - Send the email */
if (mail($to, $subj, $msg, $headers)) {
/* Redirect to success page if mail sent */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
} else {
die;
/* Redirect to a different page or do something else if it fails */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
}
}
}
?>
You might not want to do this on the live site, as other users will be able to see the error.
Let me know what you get from this.
andyg113
02-09-2011, 10:48 AM
Hi, now i get this:
Warning: mail() [function.mail]: SMTP server response: 501 5.5.4 Invalid Address in \\nas34ent\Domains\s\showstoppersincabaretcornwall.co.uk\user\htdocs\onlyjoecontact\onlyjoe\FormHandler.php on line 31
using this code:
<?php
error_reporting(E_ALL);
/* Step 1 - Check to see whether the user has submitted the form */
if (isset($_POST['submit'])) {
/* Step 2 - Check that the user has input something for their email */
// Using $email so it's easier to write
// The trim() function takes off any whitespace from the string
$email = trim($_POST['email']);
if (!empty($email)) {
/* Step 3 - Email isn't empty, send email */
// The email address to send to
$to = 'mailinglistonlyjoe@gmail.com';
// Message subject
$subj = 'ADD TO ONLYJOE MAILING LIST';
// Message content
$msg = $email . ' would like to be added to your mailing list.';
$headers = "From: Mailing List <mailinglistonlyjoe@gmail.com>";
/* Step 4 - Send the email */
if (mail($to, $subj, $msg, $headers)) {
/* Redirect to success page if mail sent */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
} else {
die;
/* Redirect to a different page or do something else if it fails */
header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
}
}
}
?>
Thanks
djr33
02-09-2011, 04:15 PM
It sounds like your mail() function is configured incorrectly. Can you contact your host for assistance?
On a server with a properly configured mail() function, this code should work.
andyg113
02-12-2011, 05:22 PM
It sounds like your mail() function is configured incorrectly. Can you contact your host for assistance?
On a server with a properly configured mail() function, this code should work.
Hi, I contacted my host and they gave me the below code which did work for about five minutes then stopped again. How could that have happened?
<?php
// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = "mailinglist@onlyjoe.co.uk"; // Specify the email address you want to send the mail to.
$email_subject = "ADD TO ONLYJOE MAILING LIST"; // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html";
// Get the details the user entered into the form
$email_from = $_POST["email"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: $email_from . \r\n";
$headers .= "Reply-To: $email_from . \r\n"; // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("sendmail_from", $email_from);
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
header("Location: " . $thankyou_url); // Redirect customer to thankyou page
} else {
// The mail didn't send, display an error.
echo "There has been an error sending your message. Please try later.";
}
?>
djr33
02-12-2011, 05:34 PM
1. The mail() function in that is very strange: you aren't sending a body (should be the third parameter) and the fourth is a second set of headers.
2. If it did work and doesn't work now and you did not change the code, then something on the server probably changed.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.