Jump to content

Processing Form with PHP and Using the Mail Function


msauer

Recommended Posts

Hi,

 

I am new to the W3School Forums. This past week I worked through the W3School “PHP Forms” section of the tutorial and all went well. I created a form, did the checks, then the validations and displayed the collected data on the screen.

Now I want to email the data instead of displaying it and I am now working though the W3School “The PHP mail() Function” section of the tutorial, I have successfully sent the email without doing all the checks and validation, but when I attempted to merge it with the email fuctions start to go wrong - I get a blank screen. Not sure what I am doing incorrectly.

I am already bald on one spot from pulling my hair. I have made so many adjustments that I now have confused myself. Can someone please offer a keen eye and some advice – pleeeeeeease ;)

What is the best way to post code so it doesn't post like a big lump of text and symbols. Any advice is greatly appreciated – Thanks in Advance!

 

I tried uploading the code in a word document but got message that I don't have permission to upload this kind of file. What kind of files are accepted?

 

Thanks a Bunch!

M

 

 

Link to comment
Share on other sites

A blank pages usually means you have errors, and your server is configured to not show error messages. One option is to configure your server to display all error messages, which would be ideal if you're trying to learn. You can change those options in the php.ini file, the display_errors and error_reporting options. You can also paste your code here to do a syntax check only:http://www.piliapp.com/php-syntax-check/

What is the best way to post code so it doesn't post like a big lump of text and symbols.

Use a code box, there's a button on the editor to insert the code brackets so that you can paste code between them.

I tried uploading the code in a word document

Yeah don't do that. If you want to upload a file then just upload the actual PHP file, there's no reason to put the code in Word. There's no reason to put code in a Word document ever, really.
Link to comment
Share on other sites

Hi Thank You for your response. I am in the process of setting up WAMP - Once I figure out why localhost doesn't want to run my page - I will get into the "ini' file but in the meanwhile I am uploading to a webserver and working that way. I don't have access to the "ini" file

With regards to the code - here is what I put together during the tutorial - any advice is greatly appreciated – Thanks in Advance!

<?php$name = $email = $gender = $comment = $website = "";$nameErr = $emailErr = $genderErr = $commentErr = $websiteErr = "";if ($_SERVER["REQUEST_METHOD"] == "POST"){if (empty($_POST["name"])){$nameErr = "Name is required";}else{$name = test_input($_POST["name"]);// check if name only contains letters and whitespaceif (!preg_match("/^[a-zA-Z ]*$/",$name)){$nameErr = "Only letters and white space allowed";}}if (empty($_POST["email"])){$emailErr = "Email is required";}else{$email = test_input($_POST["email"]);// check if e-mail address syntax is validif (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)){$emailErr = "Invalid email format";}}if (empty($_POST["website"])){$website = "";}else{$website = test_input($_POST["website"]);// check if URL address syntax is valid (this regular expression also allows dashes in the URL)if (!preg_match("/b(? :(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)){$websiteErr = "Invalid URL";}}if (empty($_POST["comment"])){$comment = "";}else{$comment = test_input($_POST["comment"]);}if (empty($_POST["gender"])){$genderErr = "Gender is required";}else{$gender = test_input($_POST["gender"]);}}function test_input($data){$data = trim($data);$data = stripslashes($data);$data = htmlspecialchars($data);return $data;}?><!--start main content area--><?phpif(isset($_POST["submit"])){?><form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">Name: <input type="text" name="name" value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span><br><br />E-mail: <input type="text" name="email" value="<?php echo $email;?>"><span class="error">* <?php echo $emailErr;?></span><br><br />Website: <input type="text" name="website" value="<?php echo $website;?>"><span class="error"><?php echo $websiteErr;?></span><br><br />Gender:<input type="radio" name="gender"<?php if (isset($gender) && $gender=="female") echo "checked";?>value="female">Female<input type="radio" name="gender"<?php if (isset($gender) && $gender=="male") echo "checked";?>value="male">Male<span class="error">* <?php echo $genderErr;?></span><br><br />Comment:<br /><textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br><br /><input type="submit" name="submit" value="Submit"></form><!--end of form area--><?php}else{$from = 'From: Loyalty Web Page Form <offers@domain.com>';$to = 'Offers@domain.com';$subject = 'Sign Me Up For Loyalty Offer ';$site = 'http://www.domain.com';$body = "Comments were sent via the Loyalty Offer on the domain Websiten";$body . ="<strong>Name:</strong> " . $name . "<br>" ."<strong>Email:</strong> " . $email . "<br>" ."<strong>Website:</strong> " . $website . "<br><br>" ."<strong>Gender:</strong> " . $gender . "<br><br>";mail($to,$subject,$body,$from);mail($email, "Thank you for Signing Up", "Dear $name,n Thank you for Signing Up. n I am sure you will enjoy the benefits domain has to offer. n Thank You,nDomain",$from);header('Location: http://www.domain.com/typage.php');}}?>

Link to comment
Share on other sites

Interesting - where did that worried icon face come from? hmmm

you should use code tags, per JSG's reply to your questions about how to post code on the forum.

Link to comment
Share on other sites

There's an option to disable emoticons when you post. I think you can set in your profile to never use them. If you put your code inside a code box it will also skip them. What happens when you run that code?

Thanks - I will go and disable them - Oh thank you for suggesting that tool - I put the code throught the checker and found a couple of syntax errors so now the page doesn't load blank. At the moment when I load the page - it goes straight to the page I am redirecting it to. The form is not coming up. I am receiviing an email from the submission Here is the updated code:<?php$name = $email = $gender = $comment = $website = "";$nameErr = $emailErr = $genderErr = $commentErr = $websiteErr = "";if ($_SERVER["REQUEST_METHOD"] == "POST"){if (empty($_POST["name"])){$nameErr = "Name is required";}else{$name = test_input($_POST["name"]);// check if name only contains letters and whitespaceif (!preg_match("/^[a-zA-Z ]*$/",$name)){$nameErr = "Only letters and white space allowed";}}if (empty($_POST["email"])){$emailErr = "Email is required";}else{$email = test_input($_POST["email"]);// check if e-mail address syntax is validif (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)){$emailErr = "Invalid email format";}}if (empty($_POST["website"])){$website = "";}else{$website = test_input($_POST["website"]);// check if URL address syntax is valid (this regular expression also allows dashes in the URL)if (!preg_match("/b(? :(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)){$websiteErr = "Invalid URL";}}if (empty($_POST["comment"])){$comment = "";}else{$comment = test_input($_POST["comment"]);}if (empty($_POST["gender"])){$genderErr = "Gender is required";}else{$gender = test_input($_POST["gender"]);}}function test_input($data){$data = trim($data);$data = stripslashes($data);$data = htmlspecialchars($data);return $data;}<?phpif(isset($_POST["submit"])){?><form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">Name: <input type="text" name="name" value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span><br><br />E-mail: <input type="text" name="email" value="<?php echo $email;?>"><span class="error">* <?php echo $emailErr;?></span><br><br />Website: <input type="text" name="website" value="<?php echo $website;?>"><span class="error"><?php echo $websiteErr;?></span><br><br />Gender:<input type="radio" name="gender"<?php if (isset($gender) && $gender=="female") echo "checked";?>value="female">Female<input type="radio" name="gender"<?php if (isset($gender) && $gender=="male") echo "checked";?>value="male">Male<span class="error">* <?php echo $genderErr;?></span><br><br />Comment:<br /><textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br><br /><input type="submit" name="submit" value="Submit"></form><!--end of form area--><?php}else{$from = 'From: domain Loyalty Web Page Form <offers@domain.com>';$to = 'Offers@domain.com';$subject = 'Sign Me Up ';$body = "Comments were sent via the Loyalty Offer Websiten";$body."<strong>Name:</strong> " . $name . "<br>" ."<strong>Email:</strong> " . $email . "<br>" ."<strong>Website:</strong> " . $website . "<br><br>" ."<strong>Gender:</strong> " . $gender . "<br><br>";mail($to,$subject,$body,$from);mail($email, "Thank you for Signing Up", "Dear $name,n Thank you for Signing Up for the Loyalty program. n I am sure you will enjoy the benefits we have to offer. n Thank You,nDomain ",$from);header('Location: http://www.domain.com/typage.php');}?> Edited by msauer
Link to comment
Share on other sites

The last if statement is backwards. If $_POST['submit'] is set then you display the form. If $_POST['submit'] is not set then you send the email and redirect. That's backwards.

Awesome - Thank you so Much! I know say if not submit - show form otherwise send email and redirect so the form is coming up but it submits with no data if I click submit. It is overlooking my entire validation section on top.
Link to comment
Share on other sites

is REQUEST_METHOD equal to POST? Is your <form> using POST? What does your HTML look like?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...