Hi, As I'm just getting started with PHP I need a simple solution to a simple problem. I know this will probably be a easy question, but because I'm just getting started my PHP is very weak. Basically i need a php email form fully functional. At the moment I've just taken a simple email form and am using it on a website. Now my question is how to make the name and last name funtion work as well. As you can see in my HTML I have Name and Last Name, how do I get that to function with the PHP file. Or if you have a better code then the one below please let me know. HTML: “<form method='post' action='Mailform.php'> <p>Name: <input type='text' name='email'></p> <p>Last Name: <input type='text' name='email'></p> <p>Email: <input type='text' name='email'></p> <p>Subject: <input type='text' name='subject'></p> <p>Message:<br/> <textarea name='message' rows='10' cols='30'></textarea></p> <input type='submit' value='Submit Form'> </form>†PHP: “<?php $email_address = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; mail( "someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; ?>†Thanks,
First change these lines in your HTML so that the 'name' attribute in the Name/Last Name input tags is unique: <p>Name: <input type='text' name='fname'></p> <p>Last Name: <input type='text' name='lname'></p> PHP: Then to use them in your .php you would reference their values with these variables: $_POST['fname'] $_POST['lname'] If you have a specific place you want to place them and are unsure, just post back with where you want them to show up in your .php.
<?php $email_address = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; mail( "someone@example.com", "Subject: $subject", $message, "From:" . $email . " ); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; ?> Code (markup): That should work
Where do you want the names to go ? in the sent message or in the message getting sent As above example use say name='fname' & name='lname' then post those. <form method='post' action='Mailform.php'> <p>First Name: <input type='text' name='fname'></p> <p>Last Name: <input type='text' name='lname'></p> <p>Email: <input type='text' name='email'></p> <p>Subject: <input type='text' name='subject'></p> <p>Message:<br/> <textarea name='message' rows='10' cols='30'></textarea></p> <input type='submit' value='Submit Form'> </form> Code (markup): PHP: <?php $fname = $_POST['fname']; $lname = $_POST['lname']; $email_address = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; mail( "someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you ".$fname." ".$lname." for using our mail form.<br/>"; echo "Your email has been sent."; ?> PHP: EDIT or just use the codes below
Since you have opted to use the post method on the form, you'll need to retrieve information using the $_POST variable. So, to draw your first/last name out you'd first change your name and last name's "name" to something different than email or it will always pull the last piece out (In this case the users email). Once you do that, to pull out first and last name just do something like $firstname=$_POST['firstname'] and $lastname=$_POST['lastname'], or you can concatenate them as in my example. __________________________________________________ HTML: form method='post' action='Mailform.php'> <p>Name: <input type='text' name='firstname'></p> <p>Last Name: <input type='text' name='lastname'></p> <p>Email: <input type='text' name='email'></p> <p>Subject: <input type='text' name='subject'></p> <p>Message:<br/> <textarea name='message' rows='10' cols='30'></textarea></p> <input type='submit' value='Submit Form'> </form> Code (markup): PHP: <?php $email_address = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; $name = $_POST['firstname'] . " " . $_POST['lastname']; //We need to add the name to the message $message = "Contact Name: $name\r\n Contact Email: $email_address\r\n\r\n" . $message; mail( "someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; ?> Code (markup):
Try this Add a HTML Box <p>Name: <input type='text' name='name'></p> And PHP goes like this <?php $name = $_POST['name']; $email_address = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; //We need to add the name to the message $message = "Contact Name: $name\r\n Contact Email: $email_address\r\n\r\n" . $message; mail( "someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; ?> PHP: