Vishal
Vishal

Reputation: 1

Simple HTML/PHP Contact Form Not Working

I am trying to create simple HTML/PHP Contact form, But this is not working correctly. Pl check this below given code that's I'm actually trying.

My simple form process

HTML form,

<form action="send_mail.php" method="post" onsubmit="return validateForm();" name="myform" >

        <input type="hidden" name="recipient" value="email">
        <input type="hidden" name="subject" value="FormMail E-Mail">`
        <table cellspacing="2" cellpadding="2" border="0">
        <tr>
        <td align="left">Nome:
        <input type="text" name="name" id="name" size="50"></td>
        </tr>
        <tr>
        <td align="left">Email:
        <input type="text" name="email" id="email" size="50"></td>
        </tr>
        <tr>
        <td align="left">Mensagem</td></tr>
        <tr>
        <td><textarea style="resize:none;" cols="44" rows="8" name="message" id="message"></textarea></td>
        </tr>
        <tr>
        <td align="left">
        <input name="submit" type="submit" value="Aceder"><input type="reset" value="Recompor">
        <input type="hidden" name="redirect" value="thankyou.html"></td>
        </tr>
        </table>
        </form>

PHP Code

if(isset($_POST['submit']))
{
$to = "[email protected]";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>

I am using this code, plz suggest me if there is any error in my code and any changes required..

Thank you.

Upvotes: 0

Views: 2190

Answers (4)

A. Zalonis
A. Zalonis

Reputation: 1609

Try this

<?php
if(isset($_POST['submit']))
{
$to = "[email protected]";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf8' . "\r\n";

 if( !mail($to, $subject, $body, $headers) )
 {
   echo "Failure!";
 }
}
?>

Upvotes: 0

Imran Omer
Imran Omer

Reputation: 701

Change above PHP code to this;

<?php
if(isset($_POST['submit']))
{
$to = "[email protected]";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>

Upvotes: 0

teo
teo

Reputation: 801

In your PHP Code on the line 4 the quotation marks are missing at the end of the line before ;. Correct line 4:

$subject = "Email from Xyz company";

Upvotes: 0

avalkab
avalkab

Reputation: 436

quotation marks end on subject?

  if(isset($_POST['submit']))
    {
    $to = "[email protected]";
    $subject = "Email from Xyz company; // quotation marks end?
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];
    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

    mail($to, $subject, $body);
    }
    else
    {
    echo "Failure!";
    }
    ?>

Upvotes: 2

Related Questions