odading Posted September 5, 2016 Share Posted September 5, 2016 I found a php code for contact/email form in W3 School : http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_complete the code is : <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; 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 whitespace if (!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 is well-formed if (!filter_var($email, FILTER_VALIDATE_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; } ?> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 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> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <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> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html> My question is : How do I put my email address so after the user click "submit" button, the information he/she has filled in the form is sent to my email address ? I'm sorry if this is a stupid question, but this thing is very new for me . Thank you. Link to comment Share on other sites More sharing options...
Ingolme Posted September 5, 2016 Share Posted September 5, 2016 Here's the mail tutorial from W3Schools: http://www.w3schools.com/php/func_mail_mail.asp It uses PHP's mail() function. Link to comment Share on other sites More sharing options...
odading Posted September 5, 2016 Author Share Posted September 5, 2016 Thank you, Ingolme. I go to see the link now. Link to comment Share on other sites More sharing options...
odading Posted September 5, 2016 Author Share Posted September 5, 2016 (edited) Hi Ingolme, I've just read the link and I'm sorry I still don't understand it.I'm thinking like this : The link tell that to send email can use the code : <?php $to = "somebody@example.com"; $subject = "My subject"; $txt = "Hello world!"; $headers = "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com"; mail($to,$subject,$txt,$headers); ?> Based from the code I found in my first post ---> http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_complete Should I insert the send email code after the "if if" condition and after the function test_input($data) as followed : <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; 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 whitespace if (!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 is well-formed if (!filter_var($email, FILTER_VALIDATE_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; $to = "somebody@example.com"; mail($to,$name,$email,$gender,$comment,$website); } ?> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 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> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <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> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html> I insert this code : $to = "somebody@example.com"; mail($to,$name,$email,$gender,$comment,$website); right after the function test_input($data) and before the <h2>PHP Form Validation Example</h2> Am I correct or not, Ingolme ?Thank you. Edited September 5, 2016 by odading Link to comment Share on other sites More sharing options...
dsonesuk Posted September 5, 2016 Share Posted September 5, 2016 The mail function has three required parameters ' to', 'subject', 'message', and two optional, you have only the required to fill, 'to' the recipient ($email), 'subject' which you decide, and 'message' in which you gather $gender, $comment, $website content and concatenate together to assign to $message variable usually into html table layout to be sent with email. Link to comment Share on other sites More sharing options...
odading Posted September 5, 2016 Author Share Posted September 5, 2016 The mail function has three required parameters ' to', 'subject', 'message', and two optional, you have only the required to fill, 'to' the recipient ($email), 'subject' which you decide, and 'message' in which you gather $gender, $comment, $website content and concatenate together to assign to $message variable usually into html table layout to be sent with email. Hi dsonesuk... I'm sorry I don't understand your explanation. Anyway I try hard to catch what you mean. Here is the code : $to = "my-email-address"; $message = $gender + $comment mail($to,$name,$email,$message); $to is my email address $name is the sender's name $email is the sender's email address $message contains information about sender's gender, sender's comment/message Then that code I should insert it after the function test_input($data) and before the <h2>PHP Form Validation Example</h2> Am I correct or not ? 'subject' which you decide Sorry, i don't understand ... why do I decide the sender's subject of his email message for me ? For exampe, when I receive an email from a friend - I have his subject something like this "I'm coming to your country". Then inside the message is more detail about his coming. Thank you. Link to comment Share on other sites More sharing options...
dsonesuk Posted September 5, 2016 Share Posted September 5, 2016 The subject has to be decided by YOU so you know what email relates to, example 'example.co.uk contact form' or 'example.co.uk Enquiry', the user usually does not fill this out, as the sender can then filter the email to type of email, you can use variables within the subject, for example: $name.' Has sent an enquiry from example.co.uk' where $name will equal 'John Smith', so subject will be 'John Smith Has sent an enquiry from example.co.uk'. mail($to,$name,$email,$message); will return with subject 'John Smith'? What is this a relation to? Contact, enquiry, more likly spam, delete. Meassage example: $message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</p> <table> <tr> <th>Name</th> <th>Email</th> <th>Gender</th> <th>Comment</th> <th>Website</th> </tr> <tr> <td>".$name."</td> <td>".$email."</td> <td>".$gender."</td> <td>".$comment."</td> <td>".$website."</td> </tr> </table> </body> </html> "; Link to comment Share on other sites More sharing options...
thescientist Posted September 16, 2016 Share Posted September 16, 2016 Anyway I try hard to catch what you mean. Here is the code : $message = $gender + $comment You're doing addition here, fyi. You probably mean to use . which is the operator for concatenation http://www.w3schools.com/php/php_operators.asp (string operators) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now