0

Hello,

I have this contact form website

Try to fill in the whole information then after you press submit, the next page start to hang. Why is it?

<?php

// FORM VALIDATION AND RECAPTCHA SETTING

// Include ReCaptcha and define keys
require_once('recaptchalib.php');
define('RECAPTCHA_PUBLIC', '6LdRStUSAAAAAH4f_NMg-Fmdp2bDo4LYNhN36ScB');
define('RECAPTCHA_PRIVATE', '6LdRStUSAAAAABctjZq_iX249auG_kjaNvn_4vfN');

// Define notification recipient
define('NOTIFICATION_RECIPIENT', 'davy_yg@yahoo.com');

// Define data and error arrays
$data = array('name' => '', 'email' => '', 'phone' => '', 'message' => '', 'captcha' => '');
$errors = array();
$error = '';

// Check if form submitted
if($_POST) {
    // Prepare form data and captcha 
    $data = array_merge($data, $_POST['form']);
    $captcha = recaptcha_check_answer(RECAPTCHA_PRIVATE, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

    // Validate name
    if(strlen(trim($data['name'])) == 0)
        $errors['name'] = 'Name is mandatory';

    // Validate email
    if(strlen(trim($data['email'])) == 0)
        $errors['email'] = 'Email is mandatory';
    elseif(! filter_var($data['email'], FILTER_VALIDATE_EMAIL))
        $errors['email'] = 'Email is not valid';

    // Validate phone
    if(strlen(trim($data['phone'])) == 0)
        $errors['phone'] = 'Phone is mandatory';

    // Validate message
    if(strlen(trim($data['message'])) == 0)
        $errors['message'] = 'Message is mandatory';

    // Validate captcha
    if(! $captcha->is_valid)
        $errors['captcha'] = 'Captcha is incorrect';

    // Send email and redirect user if no form errors
    if(! $errors) {
        if(send_notification($data))
            header('Location: thank-you.php');
        else
            $error = 'An unexpected error occurred. Please try again.';
    }
}
?>

<div id="form">
        <form method="post" action="konsultasi-konsumen.php">
            <?php if($error): ?>
                <h3><?php echo $error; ?></h3>
            <?php endif; ?>

            <table border="0">
                <tr align="left">
                    <th><div class="form-row">
                        <label for="name">Name</label><br /></th>
                    <th><input id="name" type="text" name="form[name]" value="<?php echo $data['name']; ?>" />

                        <?php if(isset($errors['name'])): ?>
                            <span class="form-error"><?php echo $errors['name']; ?></span>
                            <?php endif; ?>
                    </div></th>
                </tr>                        

                <tr align="left">
                    <th><div class="form-row">            
                            <label for="email">Email</label><br /></th>
                    <th><input id="email" type="text" name="form[email]" value="<?php echo $data['email']; ?>" />

                        <?php if(isset($errors['email'])): ?>
                            <span class="form-error"><?php echo $errors['email']; ?></span>
                            <?php endif; ?>
                    </div></th>
                </tr>

                <tr align="left">
                    <th><div class="form-row">
                            <label for="name">Phone</label><br /></th>
                    <th><input id="name" type="text" name="form[phone]" value="<?php echo $data['phone']; ?>" />

                        <?php if(isset($errors['phone'])): ?>
                            <span class="form-error"><?php echo $errors['phone']; ?></span>
                            <?php endif; ?>
                    </div></th>
                </tr>

                <tr align="left">
                    <th><div class="form-row">
                            <label for="message">Message</label><br /></th>
                    <th><textarea id="message" name="form[message]"><?php echo $data['message']; ?></textarea>

                <?php if(isset($errors['message'])): ?>
                    <span class="form-error"><?php echo $errors['message']; ?></span>
                <?php endif; ?>
                    </div></th>
                </tr>

                <tr><th><br></th></tr>

                <tr align="left">
                    <th colspan="2"><div class="form-row">
                <?php echo recaptcha_get_html(RECAPTCHA_PUBLIC); ?>

                <?php if(isset($errors['captcha'])): ?>
                    <span class="form-error"><?php echo $errors['captcha']; ?></span>
                <?php endif; ?>
                </div></tr>

                <tr align="left">
                    <th colspan="2"><div class="form-row">
                <input type="submit" value="Submit" />
                </div></th></tr>

                </table>
        </form>
</div>

Edited by davy_yg

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by joshl_1995
0

Hello davy_yg,
I have put this script together, it does what your script does. So here it is hope it works for you.

<?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!";
    }
?>
<form name="contact" action="konsultasi-konsumen.php" method="POST">
<table>
    <tr>
        <td>
        <font color="red">*</font> = Required
        </td>
    </tr>
    <tr>
        <td>
        Your name <font color="red">*</font>:
        </td>
        <td>
        <input type="text" name="name" size="64" value="<?php echo $name; ?>">
        </td>
    </tr>
    <tr>
        <td>
        Your email <font color="red">*</font>:
        </td>
        <td>
        <input type="text" name="email" size="64" value="<?php echo $email; ?>">
        </td>
    </tr>
    <tr>
        <td>
        Your phone number <font color="red">*</font>:
        </td>
        <td>
        <input type="text" name="phone" size="64" value="<?php echo $phone; ?>">
        </td>
    </tr>
    <tr>
        <td>
        &nbsp;
        </td>
    <tr>
    <tr>
        <td>
        Message <font color="red">*</font>:
        </td>
        <td>
        &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        &nbsp;
        </td>
        <td>
        <textarea name="message" cols="50" rows="5" style="resize:none" wrap="off"><?php echo $message; ?></textarea>
        </td>
    </tr>
    <tr>
        <td>
        &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        &nbsp;
        </td>
        <td>
        <?php
        $publickey = "6LdRStUSAAAAAH4f_NMg-Fmdp2bDo4LYNhN36ScB";
        echo recaptcha_get_html($publickey);
        ?>
        </td>
    </tr>
    <tr>
        <td>
        &nbsp;
        </td>
        <td>
        <input type="submit" name="submit" value="     Send     ">
        </td>
    </tr>
</table>
</form>
0

Notice: Undefined index: name in C:\xampp\htdocs\Rustoleum\konsultasi-konsumen2.php on line 60

Notice: Undefined index: email in C:\xampp\htdocs\Rustoleum\konsultasi-konsumen2.php on line 61

Notice: Undefined index: phone in C:\xampp\htdocs\Rustoleum\konsultasi-konsumen2.php on line 62

Notice: Undefined index: message in C:\xampp\htdocs\Rustoleum\konsultasi-konsumen2.php on line 63

Notice: Undefined index: submit in C:\xampp\htdocs\Rustoleum\konsultasi-konsumen2.php on line 64

Line 60 - 64:

$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']) {
0

Try this:

$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']) {
0

Oh i know why you renamed the page adding the number "2".
Replace <form name="contact" action="konsultasi-konsumen.php" method="POST">
to <form name="contact" action="konsultasi-konsumen2.php" method="POST">

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.