Issue with email form and php script

107 Views Asked by At

I am new to website development and wanted to get a quick answer about a php script that I am writing. I want this script to take all of the form fields and email them to me as well as let the person filling out the form know that their information has been sent as well as take them back to my site after submitting the form. Right now the form will email incomplete fields to my email address and it will let the person know that the form has been submitted, but it the filled in information is blank. Below is my form code and below that is my php script. Can someone look at both and tell me what I am doing wrong? Thank you in advance.


<form action="contact.php" method="post"  id="form">
<input type="text" input name="cf_name" value="Name(Required)"    onfocus="this.value = '';" onblur="if (this.value == '')    {this.value =    'Name';}">
<input type="text" input name="cf_email" value="Email (Required)"   onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';>
<input type="text" input name="cf_subject" value="Subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}">
<textarea onfocus="if(this.value == 'Your Message here....') this.value='';" onblur="if(this.value == '') this.value='Your Message here....;" >Your Message here....</textarea>
<span class="pull-right"><input type="submit" value="Submit">                   </span>
</form>

<?php

$mail_to = '[email protected]';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message,$headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to          
[email protected]');
window.location = 'index.html';
</script>
<?php
}
?>
3

There are 3 best solutions below

0
On

the name of your fields are not populated in your php script.

cf_name cf_email cf_subject

You should use something like this in your script:

$body_message = 'From: '.$_POST['cf_name']."\n";
$body_message .= 'Email: '.$_POST['cf_email']."\n";
3
On

Error in HTML FORM

in text are assign name field

<textarea name="cf_message"......
<input type="submit" name="submit" value="submit">

Check data with

if(isset($_POST['submit'])
{
//mail code here
}

in your code all variables are empty because you are not assign any value to them

Method 01

$field_name = $_POST['cf_name'];//this will assign your HTML data
$field_email= $_POST['cf_email'];
$field_subject= $_POST['cf_subject'];
$field_message= $_POST['cf_message'];

then you can use above code

Method 02

Change this

$subject = 'Message from a site visitor '.$_POST['cf_message'];

$body_message = 'From: '.$_POST['cf_name']."\n";
$body_message .= 'Email: '.$_POST['cf_email']."\n";
$body_message .= 'Subject: '.$_POST['cf_subject']."\n";
$body_message .= 'Message: '.$_POST['cf_message'];

$headers = 'From: '.$_POST['cf_email']."\r\n";
$headers .= 'Reply-To: '.$_POST['cf_email']."\r\n";
0
On

Your form has invalid mark up, extra JS and default values you won't want.

Here's how I'd make your form

<form action="contact.php" method="post"  id="form">
    <input type="text" name="cf_name" placeholder='Name (Required)' />
    <input type="text" name="cf_email" placeholder="Email (Required)" />
    <input type="text" name="cf_subject" placeholder="Subject" />
    <textarea placeholder="Your Message here...."></textarea>
    <span class="pull-right">
    <input type="submit" name="submit" value="Submit" />
    </span>
</form>

This is how I'd write your PHP.

<?php
if(!empty($_POST['submit'])) {
    $mail_to = '[email protected]';
    $subject = 'Message from a site visitor '.$field_name;
    $body_message = (!empty($_POST['cf_name'])) ? 'From: '. $_POST['cf_name'] ."\n" : '';
    $body_message .=  (!empty($_POST['cf_email'])) ? 'Email: '. $_POST['cf_email'] ."\n" : '';
    $body_message .=  (!empty($_POST['cf_subject'])) ? 'Subject: '. $_POST['cf_subject'] ."\n" : '';
    $body_message .= 'Message: '.$field_message;
    $fromemail = (!empty($_POST['cf_email'])) ? $_POST['cf_email'] : 'DEFAULT EMAIL IF EMPTY';
    $headers = 'From: '. $fromemail . "\r\n";
    $headers .= 'Reply-To: ' . $fromemail . "\r\n";
    if (mail($mail_to, $subject, $body_message, $headers)) { ?>
<script language="javascript" type="text/javascript">
            alert('Thank you for the message.');
            window.location = 'index.html';
        </script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to [email protected]');
            window.location = 'index.html';
        </script>
<?php } else {
} else {
     echo 'Form not submitted';
}?>

The () ? : is the ternary operator, a short hand way of writing conditionals, http://php.net/manual/en/language.operators.comparison.php.

You also may want to add in JS validation to the HTML form for a more friendly response to the user that they missed a required field. Currently if all fields are empty your $body_message will just be empty (or should be I didn't test this).

Also be sure to update the DEFAULT EMAIL IF EMPTY to your actual default email.

It also might have been a bit cleaner to use a standard if instead of the ternary for the !empty($_POST['cf_email'] since 2 values need to be set but you can modify that if you want.