Jump to content

Contact form not working


doctortim

Recommended Posts

Hi guys,

 

Have an important website ready to go live tomorrow night, so any help would be greatly appreciated, anyway...

 

Problem with the contact form, I'm testing it locally (the php and form are on the one page 'contact.php' and the form action posts to itself).

 

Mail is being sent (I expect tha since I'm testing locally), but when I leave the name or email field blank, I dont even trigger my errors. Here is my code:

 

The PHP:

 

<?php

 

// Address error handing.

ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);

 

 

// Check if the form has been submitted.

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

 

$problem = FALSE; // No problems so far.

 

// Check for each value.

 

if (empty ($_POST['name'])) {

$problem = TRUE;

print '<div id="email_warnings">Please enter your name!</div>';

}

 

if (empty ($_POST['email'])) {

$problem = TRUE;

print '<div id="email_warnings">Please enter your email address!</div>';

}

 

if (!$problem) { // If there weren't any problems...

 

print '<div id="email_warnings">Your message has been sent<br />Thank You!</div>';

 

// Send the email.

 

 

$email_to = '[email protected]';

 

$subject = $_POST['enquiry_type'];

 

$body = "A contact form email has been received !!/n/n".

"The message is from ".$_POST['name'].", with email: ".$_POST['email']."/n/n".

"Message Sent: "."/n/n".

$_POST['message_body'];

 

mail($email_to, $subject, $body);

 

}

 

else { // Forgot a field.

 

print '<div id="email_warnings">Please try again!</p></div>';

 

}

 

}

 

?>

 

 

Then I have the contact form right after:

 

 

<form action="contact.php" method="post">

  <table class="table_margin" width="343" border="0" cellspacing="0" cellpadding="0">

    <tr>

      <td width="129" align="right" valign="top"><em>Name</em></td>

      <td width="180" class="cell_margin"><label>

        <input name="name" type="text" id="textfield"  value="first and last name" size="24" />

      </label></td>

    </tr>

    <tr>

      <td align="right" valign="top"> </td>

      <td class="cell_margin"> </td>

    </tr>

    <tr>

      <td align="right" valign="top"><em>Email</em></td>

      <td class="cell_margin"><input name="email" type="text" id="textfield2" value="[email protected]" size="24" /></td>

    </tr>

    <tr>

      <td align="right" valign="top"> </td>

      <td class="cell_margin"> </td>

    </tr>

    <tr>

      <td align="right" valign="top"><em>Enquiry</em></td>

      <td class="cell_margin"><label>

        <select name="enquiry_type" id="select">

          <option value="sale">Sales & Order Enquiry</option>

          <option value="feedback">General Feedback</option>

          <option value="issue">Website Issue</option>

        </select>

      </label></td>

    </tr>

    <tr>

      <td align="right" valign="top"> </td>

      <td class="cell_margin"> </td>

    </tr>

    <tr>

      <td align="right" valign="top"><em>Message</em></td>

      <td class="cell_margin"><label>

        <textarea name="message_body" id="textarea" cols="45" rows="5">Type your message here ...</textarea>

      </label></td>

    </tr>

    <tr>

      <td align="right" valign="top"> </td>

      <td class="cell_margin"> </td>

    </tr>

    <tr>

      <td align="right" valign="top"> </td>

      <td class="cell_margin"><label>

        <input type="submit" name="button" id="button" value="Submit" />

      </label></td>

    </tr>

  </table>

  </form>

 

 

Thanks guys!

Link to comment
https://forums.phpfreaks.com/topic/142524-contact-form-not-working/
Share on other sites

It's not the default values. Remove them and it still does not trigger the errors.

 

Time to smack yourself in the forehead. :) Don't worry, we all do it.

 

Your submit button has a name of "button"

<input type="submit" name="button" id="button" value="Submit" />

 

But, you are testing for a POST item with the name "submit"

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

 

So your page never checks the form data. Either change the name of the submit button or the variable you are testing for. You don't have to check for a particular post value either - just check that $_POST was set

if ( isset ($_POST)) {

 

I would also look at revising your error handling to re-populat the fields with any values the user did enter.

It's not the default values. Remove them and it still does not trigger the errors.

 

Time to smack yourself in the forehead. :) Don't worry, we all do it.

 

Yea,  I complete missed that, however I was sort of on :) They still would cause problems.

It's not the default values. Remove them and it still does not trigger the errors.

 

Time to smack yourself in the forehead. :) Don't worry, we all do it.

 

Yea,  I complete missed that, however I was sort of on :) They still would cause problems.

Yes, I agree. If you leave the default values in there are going to be a lot of submissions with the email address "[email protected]". At the very least the validation should check for the default values and treat them as errors. I'd also suggest an option at the top of the select list such as "--Select One--" with an empty value to force the user to select the appropriate value.

 

By the way, the "smack yourself i nthe forehead" was meant for the OP, not you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.