Jump to content

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 = 'contact@coreknowledge.com.au';

 

$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="example@domain.com" 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 "example@domain.com". 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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.