Jump to content

[SOLVED] Form to require at least one field to have x number of characters


chadrt

Recommended Posts

Hello,

I am very new to PHP, a week to be exact, so bear with me.  Last week I decided to build this database and create these interface pages and I may have made it more complicated than it should be but, well you know.  I will describe the problem and give you a little background as well so that you will have everything you need.  This is my very first attempt at PHP or MySQL stuff and I believe that this particular question has more to do with PHP than MySQL so here goes.

 

I have a form http://ki4mve.com/cs this form has 3 sections which are just three individuale forms on one page.  The third form has 5 fields that POST to a custom script that I created to show the contents of the database now I want the STATE field to always be filled in and I accomplished that but the other 4 fields I would like to have at least 3 characters in one of the remaining 4 fields!  This will hopefully prevent too many entries from being displayed simultaneously and too many database entries from flooding the screen.

 

How I am doing the STATE field: (Those are two single quotes not a double)

if ($state==''){
echo "You must select a state to search in!";
}

 

If I have been to vague please let me know what if anything you need and I will gladly post it. Thank you in advance to anyone who has a moment to spare in this request.

 

~Chad

The results page broad-search.php has a $_POST for each field in my form and a hidden field in the form specifies which set of instrcutions will followed by the broad-search.php page.  When the name and address section of the page is being used it has 5 fields that it listens to:

  • Name
  • Address
  • City
  • State
  • zip

 

What I want to happen:

=> When that form is used the field ZIP is a requirement, I have that accomplished.

=> Then the NAME, ADDRESS, or CITY not all of them or but just one of them, has to have at least 3 characters in it for it to do what it is supposed to do.

<?php
$hasThree = false;
if ((isset($_POST['NAME']) && strlen($_POST['NAME']) > 2) || (isset($_POST['ADDRESS']) && strlen($_POST['ADDRESS']) > 2) || (isset($_POST['CITY']) && strlen($_POST['CITY']) > 2)) {
   $hasThree = true;
}

if (!$hasThree) {
    die('Address, Name, Or City is required.'); // probably not DIE here but yea.
}

?>

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.