Jump to content

[SOLVED] Required fields in form


Y2Jackie

Recommended Posts

Hi,

I'm a noob at PHP (this is my first script) but need to use it so I can add in the reCaptcha code. It all works except for one thing...

 

In my form file I have:

 

<select name="contactMethod" id="contactMethod">

                              <option selected="selected">Please Choose</option>

                              <option>Home Phone</option>

                              <option>Mobile Phone</option>

                              <option>Email</option>

                            </select>

 

In my sendmail.php file I have the following statement:

 

if (!isset($_REQUEST['emailAddress'])) {

  header("Location: myformlink.php");

  } elseif (empty($EmailAddress) || empty($Enquiry) || empty($FirstName) || empty($Surname) || empty($ContactMethod)){

 

    header("Expires: Mon, 20 Dec 1998 01:00:00 GMT");

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

    header("Cache-Control: no-cache, must-revalidate");

    header("Pragma: no-cache");

 

where $ContactMethod = $_REQUEST['contactMethod'];

 

Because "Please Choose" is automatically selected, it validates as if the user has selected it. I need Please Choose at the top, but I also need the script to be able to understand that if that is selected, then it is to consider it as if it has no information and fail the test. Is this possible?

 

Thank you!

Jackie

Link to comment
https://forums.phpfreaks.com/topic/155352-solved-required-fields-in-form/
Share on other sites

if (!isset($_REQUEST['emailAddress'])) {
  header("Location: myformlink.php");
  } elseif (empty($EmailAddress) || empty($Enquiry) || empty($FirstName) || empty($Surname) || $ContactMethod == "Choose") {
.....

 

Give that a try, it will check the ContactMethod var for the word choose since it will not be empty.

the reason that it wont work is because you have to set a value to the option. instead of

<option selected="selected">Please Choose</option>

try

<option selected="selected" value="choose">Please Choose</option>

 

Then try and see if what matthewJ posted will work

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.