Problem generating name in submitted email

Apologies if this is a simple answer. Completely new to php, and trying to learn via the web…

On line #96…If I change $name to $phone…The submitted email shows:-

You have received an email from 07123456789 Phone Number 07123456789

The problem is when I leave it at $name…The submitted email shows:-

You have received an email from Phone Number 07123456789

All the other fields submit to the email correctly.

…and misses out the name field completely…Any help would be appreciated. Cheers

	THIS MAY BE A BETTER WAY OF SHOWING THE CODE
		<div class="container">
			<form class="contactForm" method="post">

	<div class="form-group">
		<input type="text" name="name" placeholder="Name">
	</div>

	<div class="form-group">
		<input type="text" name="subject" placeholder="Subject">
	</div>

	<div class="form-group">
		<input type="email" name="email" placeholder="Email">
	</div>

	<div class="form-group">
		<input type="tel" name="phone" placeholder="Phone number">
	</div>

	<div class="form-group">
		<textarea type="text" name="message" placeholder="Your Message" rows="5"></textarea>
	</div>


	<!--Submit button-->


	<div class="text-center">


		<button class="btn bg-dark text-white" btn-lg type="submit">Send Message
		
			
		</button>
	</div>

			</form>
		</div>


	<?php

	if ( isset( $_POST[ 'submit' ] ) )
		$name = $_POST[ 'name' ];
	$subject = $_POST[ 'subject' ];
	$mailFrom = $_POST[ 'email' ];
	$phone = $_POST[ 'phone' ];
	$message = $_POST[ 'message' ];

	$mailTo = "[email protected]";
	$headers = "From: " . $mailFrom;
	$txt = "You have received an email from " . $name . "  Phone Number  " . $phone . ".\n\n" . $message;


	mail( $mailTo, $subject, $txt, $headers );

	?>

There are two mistakes causing this, but first two things you need to do to get php to help you find problems…

  1. Find the php.ini that php is using and set error_reporting to E_ALL, set display_errors to ON, and set output_buffering to OFF. Restart your web server to get any changes made to the php.ini to take affect and then confirm that these settings were changed by adding a phpinfo() statement to your code and checking the values for these settings. These settings will cause php to report and display all errors it detects and to not hide output and errors if your code is doing any header() redirects. If you are doing this on a live/public server, you would instead set display_errors to OFF and set log_errors to ON so that all php errors will get logged so as to not give hackers useful information about your hosting account.
  1. For debugging purposes, use either print_r() or var_dump() in your code on the external data, $_POST in this case, so that you can see exactly what it is. var_dump() contains additional information about the values (type and length), so ti is useful for finding things like white-space/non-printing characters as part of the data.

Using item #2 above, you will see that there is no $_POST[‘submit’], since the submit button doesn’t have a name=‘submit’ attribute. This is the first problem.

The second problem is your if ( isset( $_POST[ ‘submit’ ] ) ) conditional statement doesn’t have any { } around the conditional block of code, so only the first statement after it is part of the conditional code, which is the line for the name value, which is why it doesn’t exist in combination with problem number one.

BTW - these emails are NOT being sent from the email address that someone enters in your form. They are being sent from the mail server at your web hosting and the From: mail header needs to be an email address that corresponds to the sending mail server. You can put the user entered email address as a Reply-to: mail header, after validating that it is exactly and only one properly formatted email address (to prevent mail header injection.)

You should also apply htmlentities() to all values being put into the email body, since the web interface/email client that gets used to read the emails can treat them as html even if you are not sending them as html.

You should also not put any user submitted data directly into the subject field. Put your own subject in that field, and put the user submitted subject into the email body.

Sponsor our Newsletter | Privacy Policy | Terms of Service