Skip to content Skip to sidebar Skip to footer

Php Contact Form Emails To The Email Address, But 'name' Field Is Empty?

No, I am new to this form! I have successfully set up a contact form page. It emails me the 'email address' and 'message' from the form fields, but does not send the name filed val

Solution 1:

if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))

$name = $_POST['name']; 

In above code if post of name,email and message empty, then it set name = $_POST['name'];

SO $name variable not set.

So code like these

if(empty($_POST['name'])  || 
           empty($_POST['email']) || 
           empty($_POST['message'])) {
        $errors = 'Please enter all required fields';
    }
    else
    {
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message'];
   } 

Solution 2:

Okay here i debugged , here is the corrected working code . you have to use isset instead of empty there

<?php$errors = '';
$myemail = 'myemail';//<-----Put Your email address here.if(isset($_POST['name'])  || 
   isset($_POST['email']) || 
   isset($_POST['message'])) { 


$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 


}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Website Query: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email \n Message \n $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 


?>

Also I covered the if loop you can put mail sending code also inside the if loop there

Hope this helps

Solution 3:

if your submiting, try to remove value='' just

<inputtype="text"id="edit_1" name="name" style="position:absolute; left:103px; top:8px; width:50px; /*Tag Style*/" __AddCode="here">

See if it works !

EDITED & ADDED

change the sumbit's value into name to be: name='submit'

<inputtype="submit" style="position:absolute; left:8px; top:191px; width:81px; height:22px; /*Tag Style*/" name="submit" __AddCode="here">  

and you may wanna change php part to look like this:

<?php$errors = '';
$myemail = 'myemail';//<-----Put Your email address here.$name    = $_POST['name'];
$email   = $_POST['email'];
$message = $_POST['message'];

if(isset($_POST['sumbit']) && (
    empty($_POST['name'])  ||
    empty($_POST['email']) ||
    empty($_POST['message'])))
    {
        echo"Please fill up the fields";
    }
    elseif(isset($_POST['sumbit']) && empty($errors)){
    $to = $myemail;
    $email_subject = "Website Query: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email \n Message \n $message";

    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
}
?>

Solution 4:

if(!isset($_POST['name'])  && !isset($_POST['email']) && !isset($_POST['message'])) {
   $name = $_POST['name']; 
   $email = $_POST['email']; 
   $message = $_POST['message']; 
}else{
   $errors = ''; //process
}

Add your curly braces. you are missing your curly braces. so the name variable is not being set if every POST variable has a value. The other two are because without the braces the if statement ends after the first statement.

Edit, I think your logic is wrong also. You probably want to say if all of these values are not empty then process.

Post a Comment for "Php Contact Form Emails To The Email Address, But 'name' Field Is Empty?"