Results 1 to 10 of 10

Thread: PHP Mail form

  1. #1
    Join Date
    Jun 2013

    PHP Mail form

    Hi, I am new to using PHP (in fact its my very first time) and I have to finish a contact form for class. We have to make a form that includes name, email, birthday, and a message box. When it submits, the information has to be sent to both me and the user, and the page displays a thank you message.

    Here is what I have so far:
    <form method="post" action="contact.php"> 
    Name: <input name="name" type="text"><br>
    Birthday: <input name="birthday" type="text"><br>
     Email: <input name="email" type="text"><br>
     <textarea name="message" rows="15" cols="40"></textarea><br> 
     <input type="submit"> 
     $to = "myemail@provider.com"; 
     $subject = "Contact Us"; 
    $birthday = $_REQUEST ['birthday'] ;
    $name = $_REQUEST['name'] ;
     $email = $_REQUEST['email'] ; 
     $message = $_REQUEST['message'] ; 
     $headers = "From: $email"; 
     $sent = mail($to, $subject, $message, $headers) ; 
     {print "Your mail was sent successfully"; }
     {print "We encountered an error sending your mail"; }
    I am wondering how to get the code to also email to the recipient, myself, and display the thank you message at the end. Every time I hit submit I get a 404 error. What am I doing wrong and how can I fix this?

  2. #2
    Join Date
    Jun 2013
    look in to your form action
    <form method="post" action="contact.php">
    you are submitting your form to contact.php
    is your file named contact.php?

  3. #3
    Join Date
    Aug 2012
    since you have the form submitting to its self. meaning the same page that the php is on remove the contact.php in action and just leave it blank like this action=""> start with that see if that helps

  4. #4
    Join Date
    Jun 2013
    Quote Originally Posted by Nicholas Diaz View Post
    since you have the form submitting to its self. meaning the same page that the php is on remove the contact.php in action and just leave it blank like this action=""> start with that see if that helps
    Ok, that got rid of the 404 error. So then I tested out the form several times but it only sent one notification to me. Sadly it just sent the notification, subject, and time, but not any of the information I need such as birthdate, sender's name and sender's email address.

  5. #5
    Join Date
    Aug 2012
    ok good. that means we are narrowing down the problems. so we need to look at the code again now. I like to keep my php separate from my forms personally but. let me add it to my text editor and see if i can make some changes.

  6. #6
    Join Date
    Aug 2012
    ok your all fixed up two files. try to always keep your php separate from your forms.

    the only thing you could change is the variable called $to change it to the email address this form is supposed to send to. as far as for the 2nd email it will just send to what ever someone types in to the form.

    index.php is your form file
     <form method="POST" action="send.php"> 
    Name: <input name="name" type="text"><br>
    Birthday: <input name="birthday" type="text"><br>
     Email: <input name="email" type="text"><br>
     <textarea name="message" rows="15" cols="40"></textarea><br> 
     <input type="submit"> 
    send.php is your php script
    //Sanitize incoming data and store in variable
    $name = trim(stripslashes(htmlspecialchars($_POST['name'])));
    $birthday = trim(stripslashes(htmlspecialchars($_POST['birthday'])));
    $email = trim(stripslashes(htmlspecialchars($_POST['email'])));
    $message = trim(stripslashes(htmlspecialchars($_POST['message'])));
    //Prepare information from form to be sent
    $to = 'CHANGETHISEMAIL@test.com';
    $from = 'admin@test.com';
    $headers = 'MIME-VERSION: 1.0' . '\n';
    $headers .= 'From: $from' . '\n';
    $subject = 'Contact Form Submission';
    $body = 'Name: ' .$name . PHP_EOL;
    $body .= 'Birthday: ' .$birthday . PHP_EOL;
    $body .= 'Email: ' .$email . PHP_EOL;
    $body .= 'Message: ' .$message . PHP_EOL;
    // Form data was successfull so we will now send admin email and return message to the user
    $success = (mail($to, $subject, $body, $headers, '-f admin@test.com.com')); 
    	echo 'Thank you!';				  
    	if ($success) (mail($email, $subject, $body, $headers, '-f admin@test.com.com'));	
    	echo 'Woops! Something went wrong please contact us directly';

  7. #7
    Join Date
    Jun 2013

    In Single File


    Here is a more elegant display with processing and display in a single file:
    	define ('SHOW_ME',0);				// Declare Debug value
    	if (SHOW_ME==1) {
    		foreach ($_POST as $k => $v) {
    			echo "K=> $k V=> $v <br>";
    		}	// end foreach $_POST
    	}	// end if SHOW_ME
    	$name				=	"";
    	$birthday		=	"";
    	//$email			=	"";
    	$message			=	"";
    	$to				=	"";
    	$from				=	"";
    	$subject			=	"";
    	$headers			=	"";
    	$body				=	"";
    	if (isset($_POST['submit'])) {
    		$name			=	trim(stripslashes(htmlspecialchars($_POST['name'])));
    		$birthday	=	trim(stripslashes(htmlspecialchars($_POST['birthday'])));
    		//$email		=	trim(stripslashes(htmlspecialchars($_POST['email'])));
    		$message		=	trim(stripslashes(htmlspecialchars($_POST['message'])));
    		$to			=	trim(stripslashes(htmlspecialchars($_POST['to'])));
    		$from			=	trim(stripslashes(htmlspecialchars($_POST['from'])));
    		$subject		=	trim(stripslashes(htmlspecialchars($_POST['subject'])));
    		$headers		=	'MIME-VERSION: 1.0' . '\n';
    		$headers	  .=	'From: $from' . '\n';
    		$body			=	'Name: ' .$name . PHP_EOL;
    		$body		  .=	'Birthday: ' .$birthday . PHP_EOL;
    		$body		  .=	'Email: ' .$to . PHP_EOL;
    		$body		  .=	'Message: ' .$message . PHP_EOL;
    		if (SHOW_ME==1) {
    			echo			"T=> $to F=> $from S=> $subject H=> $headers <br>".
    							"M=> $message <p>";
    		}	// end if SHOW_ME
    		$checked		=	find_blanks();
    		if ($checked<>'') { echo $checked; exit; }
    		echo "Attempting to Mail your message.  There may be a delay. <br>";
    		if (!(mail($to, $subject, $body, $headers, "-f $from"))) {
    			echo 'Woops! Something went wrong please contact us directly';
    			//(mail($to, $subject, $body, $headers, "-f $from"));
    		} else {
    			echo 'Message Sent! Thank you! <br>';	  
    		}	// end if $success
    	}	// end if isset $_POST['submit']
    	function find_blanks () {
    		foreach ($_POST as $k => $v) {
    			if ($k=='submit') { continue; }
    			if ($v=='') {
    				return 	"All fields must have values! Your =>$k<= field is blank! <br>".
    							"Fill in all fields and try again! <br>";
    			}	// end $v
    		}	// end foreach $_POST
    	}	// end function find_blanks
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    	<table border='1' cellspacing='0' cellpadding='5' width='50%' 
    			align='center' bgcolor='grey'>
    		<tr><td align='center' width='100%'>
    		<table border='0' cellspacing='0' cellpadding='0' width='100%' 
    				bgcolor='#dddddd' align='center'>
    				<td align='center' colspan='2'>
    					<h1>User Email Form</h1>
    				<td align='right'>To:&nbsp;&nbsp;</td>
    				<td align='left'><input name="to" type="text"></td>
    				<td align='right'>From:&nbsp;&nbsp;</td>
    				<td align='left'><input name="from" type="text"></td>
    				<td align='right'>Subject:&nbsp;&nbsp;</td>
    				<td align='left'><input name="subject" type="text"></td>
    				<td align='right'>Name:&nbsp;&nbsp;</td>
    				<td align='left'><input name="name" type="text"></td>
    				<td align='right'>Birthday:&nbsp;&nbsp;</td>
    				<td align='left'><input name="birthday" type="text"></td>
    				<td align='right'>Email:&nbsp;&nbsp;</td>
    				<td align='left'><input name="email" type="text"></td>
    				<td align='right' valign='top'>Message:&nbsp;&nbsp;</td>
    				<td align='left'><textarea name="message" rows="15" cols="40"></textarea></td>
    				<td align='center' colspan='2'>
    					<input type="submit" name='submit' value='Submit'>
    Was not able to get your "mail" command to run, as must be from a plugin you installed, but the rest works fine.



  8. #8
    Join Date
    Aug 2012
    my code was working fine what problem did you have with it>
    Last edited by Nicholas Diaz; 06-21-2013 at 09:47 PM.

  9. #9
    Join Date
    Jun 2013

    1. You were hard coding many of your vars that should be entered on the form,
    2. You were using 2 files to get this done when only one is needed.

    That's all



  10. #10
    Join Date
    Aug 2012
    I appreciate your feed back.
    But I am not the one asking a question on this thread. I am providing help to a person who had a question that has been answered for days now. as for putting php and html in the same file it should be avoided in most cases due to security risks. a few examples being when connecting to a database, dealing with a page controlled by a session, or when your passing personal information that should be secure through the server. At the end of the day to each his own and I think its important to see many different examples on how to accomplish any task. The more examples people are given when asking a question on a forum the better, because it allows them to have a better idea of what they are doing. its hard to learn for some people because there are so many ways to accomplish the same task.

    I make a point to answer as many questions as I can because I to have questions and i appreciate when people answer my questions promptly.

    I personally like my files to be organized easy to read and secure. So I make a point to avoid mixing php and html unless there is a reason for it.

    I think for the sake of being helpful to the person who started this thread, it would be more productive to give them an example of the way you would accomplish
    this task instead of telling me you don't like the way I structure my code. unfortunately all that does is leave confusion for someone who comes along looking for
    an answer on this thread and the last post they see is your post telling me what you feel I did wrong with out giving an example of how you feel the correct way of doing this was.

    I am sure you have your own style as I have mine, and I am sure it will be helpful for people to see because in some cases some people might find my code was easier for them to understand
    and in other cases people may relate better to the way you do things.

    Here is a reference to some common mistakes done with php.

    I realize this is an email form but just recently I was working on a form with a person who wanted to keep the email address private. That means it was necessary to keep the php in a separate file. So i try to keep good habits instead of picking and choosing when to make a simple script less secure just for the sake of not feeling like its necessary to be more secure on this one.

    Hope to see an example from you so we can add some more content to the site. The More examples more questions get answered.

    Also if you are seeing something in my script that is wrong please elaborate. I am here to learn and appreciate any chance to learn something new.
    Last edited by Nicholas Diaz; 06-27-2013 at 11:39 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13821 seconds
  • Memory Usage 2,964KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates