Help with PHP/MySQL contact form.

Soldato
Joined
28 Sep 2008
Posts
14,176
Location
Britain
Hi all,

I have a fairly basic enquiry form which a user will fill in from the website. This does two things:

1) emails the site owner with an address with the name, email, telephone, enquiry of the user
2) writes the name, email and phone number to a MySQL database

All this works, except, Name and Email are "required" fields, designated with an * and the "class=required" attribute. This is not the case for telehone, yet, if you leave the telephone field blank, upon submitting, the form returns an error saying that all fields must be completed.

I'm not sure this is a PHP problem, so here's the HTML

Code:
<form id="enquiryform" action="form-process.php" method="post">
<p><label for="name">Name:</label><input type="text" class="required" id="name" name="name" maxlength="60" /><span class="warning">*</span></p>

<p><label for="telephone">Telephone:</label><input type="int" id="telephone" name="telephone" maxlength="30"  /></p>

<p><label for="email">Email:</label><input type="text" class="email required" id="email" name="email" maxlength="100" /><span class="warning">*</span></p>

<p><label for="enquiry">Your enquiry:</label><textarea class="required" rows="4" cols="40" id="enquiry" name="enquiry" ></textarea><span class="warning">*</span></p>

<p><label for="callback">Please call me back:</label>
<select id="callback" name="callback" >
<option value="Anytime">Anytime</option>
<option value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Evening">Evening</option>
<option value="No">No thanks</option>
</select></p>

<p><label for="hearabout">Where did you hear about us?</label>
<select id="hearabout" name="hearabout" tabindex="109">
<option value="Other">Other</option>
<option value="Already a customer">Already a customer</option>
<option value="Search engine">Search engine</option>
<option value="Referred by a friend">Referred by a friend</option>
</select></p>
<p><input type="submit" class="button" name="submit" value="Submit Enquiry"  /></p>
</form>

Can anyone help me work out why it's not working?
 
Hi all,

I have a fairly basic enquiry form which a user will fill in from the website. This does two things:

1) emails the site owner with an address with the name, email, telephone, enquiry of the user
2) writes the name, email and phone number to a MySQL database

All this works, except, Name and Email are "required" fields, designated with an * and the "class=required" attribute. This is not the case for telehone, yet, if you leave the telephone field blank, upon submitting, the form returns an error saying that all fields must be completed.

I'm not sure this is a PHP problem, so here's the HTML

Code:
<form id="enquiryform" action="form-process.php" method="post">
<p><label for="name">Name:</label><input type="text" class="required" id="name" name="name" maxlength="60" /><span class="warning">*</span></p>

<p><label for="telephone">Telephone:</label><input type="int" id="telephone" name="telephone" maxlength="30"  /></p>

<p><label for="email">Email:</label><input type="text" class="email required" id="email" name="email" maxlength="100" /><span class="warning">*</span></p>

<p><label for="enquiry">Your enquiry:</label><textarea class="required" rows="4" cols="40" id="enquiry" name="enquiry" ></textarea><span class="warning">*</span></p>

<p><label for="callback">Please call me back:</label>
<select id="callback" name="callback" >
<option value="Anytime">Anytime</option>
<option value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Evening">Evening</option>
<option value="No">No thanks</option>
</select></p>

<p><label for="hearabout">Where did you hear about us?</label>
<select id="hearabout" name="hearabout" tabindex="109">
<option value="Other">Other</option>
<option value="Already a customer">Already a customer</option>
<option value="Search engine">Search engine</option>
<option value="Referred by a friend">Referred by a friend</option>
</select></p>
<p><input type="submit" class="button" name="submit" value="Submit Enquiry"  /></p>
</form>

Can anyone help me work out why it's not working?

Either you have some javascript that checks for a number and returns error, some PHP that checks for a number and returns error, or the database checks for a number and returns error.

You're HTML alone doesn't give a rats. You set the phone number as an "int", which is slightly weird, could it be that your javascript doesn't consider NULL or '' an int? :)
 
I'd make the telephone input area a text input, just for the sake of keeping everything the same. If you're desperate to be semantec, then go for it.

As the post above me says, we need PHP/Javascript too for any debugging, the HTML alone won't be causing the problem.
 
Cool, yeah, I'm using php to submit the mail and to parse the code to the DB. Here's the g33k bit :)

PHP:
<?php
session_start();
require_once('includes/config.php');

if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") {
    exit("<p>This page should not be accessed directly</p>");
} else {
    $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i";
    $profanity = "/()/i";
    $spamwords = "/()/i";
    $bots = "/()/i";

    if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
        exit("<p>Known spam bots are not allowed.</p>");
    }
    foreach ($_POST as $key => $value) {
        $value = trim($value);

        if (empty($value)) {
            exit("Opps. The form cannot be blank. Please return and fill in the fields");
        } elseif (preg_match($exploits, $value)) {
            exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
        } elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) {
            exit("<p>That kind of language is not allowed through our form.</p>");
        }

        $_POST[$key] = stripslashes(strip_tags($value));
    }

    if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) {
        exit("<p>This is not a valid email address. Please press back and ammend the form.</p>");
    }

    $recipient = "[email protected]";
    $subject = "Contact From ";

    $message = "You've received an enquiry from: \n";
    $message .= "Name: {$_POST['name']} \n";
    $message .= "E-mail: {$_POST['email']} \n";
    $message .= "Telephone: {$_POST['telephone']} \n";
    $message .= "Enquiry: {$_POST['enquiry']} \n";
    $message .= "Callback: {$_POST['callback']} \n";
    $message .= "Hearabout: {$_POST['hearabout']} \n";

    $headers = "From: <$recipient> \n";
    $headers .= "Reply-To: <{$_POST['email']}>";

    if (mail($recipient,$subject,$message,$headers)) {
        header ("Location: http://www.thankyou.php");
    } else {
        header ("Location: http://www.opps.php");
    }
}

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

$query = "INSERT INTO contacts VALUES ('$id','$name','$telephone','$email')";
print($query);
mysql_query($query);

mysql_close();



?>
 
I must admit this is a pretty convoluted way of doing it. Will read it over properly in a minute and see if I can see what's wrong. What exact error is it echoing? Empty form?

It looks to me like you're verifying if $value is empty, and telephone is part of that, so it's going to return an error. Equally, I'd also think the telephone field would be required anyway, as you have secondary information about call backs.
 
Last edited:
As DcD said you're checking every field is not empty, therefore you're always going to execute "exit("Opps. The form cannot be blank. Please return and fill in the fields");" if telephone is left blank.

You need to expand your php code to differentiate between required and not required fields.
 
Back
Top Bottom