TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Required fields in a contact form (http://www.talkphp.com/general/3774-required-fields-contact-form.html)

code_junkie 12-16-2008 02:55 PM

Required fields in a contact form
 
I am making a contact form that needs required fields. I have it set for one field to be required, but when I try to make the other fields required I get a white page. Here is my code:
Code:

if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $to = "something@somewhere.com";
  if(!$name && !$email && !$message) {
    $error = '<p style="color:#FF0000">Please complete all required fields</p>';
  } else {
        $subject = "TxDOT Watch Report";
        $from = "From: $email \r \n";
       
    ini_set('sendmail_from', $email);
        mail($to, $subject, $from, $message);
  }
}

Any help will be appreciated. Thanks

Village Idiot 12-16-2008 03:00 PM

You are using and commands in your if statement, thus it will not trigger unless all of them are empty. The blank screen is the script sending the mail.

code_junkie 12-16-2008 03:02 PM

Quote:

Originally Posted by Village Idiot (Post 20566)
You are using and commands in your if statement, thus it will not trigger unless all of them are empty. The blank screen is the script sending the mail.

The mailing script is on the same page as the html form. How do I make it work?

code_junkie 12-16-2008 03:16 PM

Ok I got the fields to be required by changing
Code:

if(!$name && !$email && !$message) {
To
Code:

if(empty($name) || empty($email) || empty($message)) {
Now, how would I set ti so the user does not have to enter the data in again and just fill in the empty fields?

Village Idiot 12-16-2008 03:34 PM

There are a few ways, ajax could return an error message so the fields don't go blank, you could have the script post to itself so that you can fill the form with the posted data. Those are what I can think of off hand.


All times are GMT. The time now is 12:32 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0