Hello,

I am having problem with contact form. Can anyone help me?

contact.php

<div id="contactform">

    <?php
    require_once('recaptchalib.php');
    $name = strip_tags(@$_POST['name']);
    $email = str_replace(" ", "", strip_tags(@$_POST['email']));
    $phone = str_replace(" ", "", strip_tags(@$_POST['phone']));
    $message = strip_tags(@$_POST['message']);
    if (@$_POST['submit']) {
    if ($name&&$email&&$phone&&$message) {
    if (is_numeric($phone)) {
    $privatekey = "6LdRStUSAAAAABctjZq_iX249auG_kjaNvn_4vfN";
    $resp = recaptcha_check_answer($privatekey,
    $_SERVER["REMOTE_ADDR"],
    $_POST["recaptcha_challenge_field"],
    $_POST["recaptcha_response_field"]);
    if (!$resp->is_valid) {
    //What happens when the CAPTCHA was entered incorrectly
    echo "The reCAPTCHA wasn't entered correctly.";
    } else {
    //Your code here to handle a successful verification
    ini_set("SMTP", "smtp.mail.yahoo.com");
    $body = "
    Name: ".$name."
    Phone: ".$phone."
    Email: ".$email."\n\n
    ".$message;
    mail("davy_yg@yahoo.com", "Contact Me", $body, "From: ".$email);
    echo "Your message has been sent, we will get back to your shortly.";
    $name = trim("");
    $email = trim("");
    $phone = trim("");
    $message = trim("");
    }
    }
    else
    echo "There should only be numbers in your phone number.";
    }
    else
    echo "Please fill in <b>all</b> the fields!";
    }
    ?>
    <div id="form">
    <form name="contact" action="contact.php" method="POST">
    <table>
    <tr>
    <td>
    <font color="red">*</font> = Required
    </td>
    </tr>
    <tr>
    <td>
    Nama <font color="red">*</font>:
    </td>
    <td>
    <input type="text" name="name" size="30" value="<?php echo $name; ?>">
    </td>
    </tr>
    <tr>
    <td>
    Email <font color="red">*</font>:
    </td>
    <td>
    <input type="text" name="email" size="30" value="<?php echo $email; ?>">
    </td>
    </tr>
    <tr>
    <td>
    No Telp. <font color="red">*</font>:
    </td>
    <td>
    <input type="text" name="phone" size="30" value="<?php echo $phone; ?>">
    </td>
    </tr>
    <tr>
    <td>
    Pesan <font color="red">*</font>:
    </td>
    <td>&nbsp;

    </td>
    </tr>
    <tr>
    <td>&nbsp;

    </td>
    <td>
    <textarea name="message" cols="30" rows="5" style="resize:none" wrap="off"><?php echo $message; ?></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;

    </td>
    </tr>
    <tr>
    <td>&nbsp;

    </td>

If someone fill in the contact form and make mistakes - no error message appears, if they fill in the contact form correctly, the message is not sent to the intended email.

You cannot use Yahoo to send email like that, because it requires authentication.

Strange, it works on: link

I am copy and paste it for another web.

if (@$_POST['submit']) {

Is it okay if I do type this:

<input type="image" name="submit" src="images/button-kirim.jpg" height="30px" width="70px" alt="Submit details" />

instead of

<input type="submit" name="submit" value=" Send ">

Will the if statement able to read the image

 <?php
    //require_once('recaptchalib.php');
  $error = array();
    $name = strip_tags(@$_POST['name']);
    $email = str_replace(" ", "", strip_tags(@$_POST['email']));
    $phone = str_replace(" ", "", strip_tags(@$_POST['phone']));
    $message = strip_tags(@$_POST['message']);

    if (isset($_POST['submit'])) 
    {
        if( ($name !='') && ($email !='') && ($phone!='') && is_numeric($phone) && ($message !='' ) )
        {

                 $privatekey = "6LdRStUSAAAAABctjZq_iX249auG_kjaNvn_4vfN";
                $resp = recaptcha_check_answer($privatekey,
                $_SERVER["REMOTE_ADDR"],
                $_POST["recaptcha_challenge_field"],
                $_POST["recaptcha_response_field"]);

               if (!$resp->is_valid) 
                {
                    //What happens when the CAPTCHA was entered incorrectly
                    echo "The reCAPTCHA wasn't entered correctly.";
                } 
                else 
                {
                    //Your code here to handle a successful verification
                    ini_set("SMTP", "smtp.mail.yahoo.com");             //smtp require's validations
                    ....
                }
        }
        else
        {
            //validation's
           foreach( $_POST as $key => $val)
           {

            if($key == "phone" && $val !="" )
            {
                if(!is_numeric($val)){
                    $error[$key] = $key ;   
                }

            }

            if($val == "" || !isset($_POST[$key]) )
            {
                $error[$key] = $key;
            }
           }
        }

    }
    else {

        echo "Please fill in <b>all</b> the fields!";
    }

    ?>
    <div id="form">
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <table>

    <tr>
    <td>
   <?php if(in_array('name', $error)) echo '<font color="red">Name*</font> = Required'; ?>
    </td>
    <td>
    <input type="text" name="name" size="30" value="<?php echo $name; ?>">
    </td>
    </tr>
    <tr>
    <td>
   <?php if(in_array('email', $error)) echo '<font color="red">Email*</font> = Required'; ?>
    </td>
    <td>
    <input type="text" name="email" size="30" value="<?php echo $email; ?>">
    </td>
    </tr>
    <tr>
    <td>
    <?php if(in_array('phone', $error)) echo '<font color="red">Phone*</font> = Required'; ?>
    </td>
    <td>
    <input type="text" name="phone" size="30" value="<?php echo $phone; ?>">
    </td>
    </tr>
    <tr>
    <td>
    <?php if(in_array('message', $error)) echo '<font color="red">Message*</font> = Required'; ?>
    </td>
    <td>&nbsp;
    </td>
    </tr>
    <tr>
    <td>&nbsp;
    </td>
    <td>
    <textarea name="message" cols="30" rows="5" style="resize:none" wrap="off"><?php echo $message; ?></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;
    </td>
    </tr>
    <tr>
    <td>&nbsp;
    </td>
    <input type="submit" name="submit" value=" Send ">
</form>
</div>

well u can try that not perfect though.

I figure something, all the code remains the same, except for:

if (@$_POST['image']) {

Just one problem, no matter what recaptcha I entered I keep receiving this message:

The reCAPTCHA wasn't entered correctly.

The last time I try, the page goes to the next page (contact2.php) with no conformation at all nor email receive.

Therefore, I try the new codes posted by hakeemtunde, and receive this error:

Fatal error: Call to undefined function recaptcha_get_html() in C:\xampp\htdocs\Innovation\contact.php on line 213

dont forget i commented //require_once('recaptchalib.php'); out u have to uncomment that and the code focus more on input validation not much on CAPTCHA

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.