• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2177
  • Last Modified:

Select dropdown list validation

Hi,

I would like to add validation on a SELECT drop down list. Could anyone please help?

I have this code:
<td>Choose:</td>
                <td>
                <?php
				$limit = 1000;
				$start = 0;
				$searchmode = " WHERE t.brand_id=$brand_id";
				$orderby = " ORDER BY d.City ASC";				
                $results = $records->getDealersPerBra<wbr ></wbr><wbr ></wbr>nds($searc<wbr ></wbr>hmode, $orderby);
				?>
                <select name="dealer_post">	
                	<option value="none">choose one</option>
                    <?php
                    	$max = count($results);
						for($i=0; $i<$max; $i++){
							if ($dzresults[0]['Dealer_id'<wbr ></wbr><wbr ></wbr>] == $results[$i]['Dealer_id'])<wbr ></wbr><wbr ></wbr>{
								$selected = 'selected="selected"';
							}else{
								$selected = '';
							}
					?>
                    	<option <?php echo $selected?> value="<?php echo $results[$i]['Dealer_id']?<wbr ></wbr><wbr ></wbr>>----<?php<wbr ></wbr> echo $results[$i]['EmailLeads']<wbr ></wbr><wbr ></wbr>?>">
						<?php echo $results[$i]['City']?> - <?php echo $results[$i]['Name'] ?></option>
                    <?php
						}
					?>
                </select>
                </td>

Open in new window


How should I change the code to make this work?

Thanks!
0
webdesigner_dk
Asked:
webdesigner_dk
  • 2
  • 2
3 Solutions
 
dimmergeekCommented:
What kind of validation?  You just want to know if they picked something other than "choose one"??

if(isset($_POST['Submit'])) {
     $dealer_post = $_POST["dealer_post"];
     if($dealer_post == "none") {
          // Handle your error here...
     }
     else {
          // All is well, they picked something other than the default
     }

Open in new window

0
 
webdesigner_dkAuthor Commented:
Yes, I would like to display an error message if they haven't picked anything else than "choose One". Could you please be more specific on where I should put the code. Thanks.
0
 
Ray PaseurCommented:
A "select list" is just like any other HTML input control.  It has a name and a value.  These two elements come into the action= script in the request.  This is how it's done.

<?php // RAY_temp_webdesigner_dk.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);


// QUESTION: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28292843.html


// ACCEPTABLE VALUES ARE KNOWN TO THE SCRIPT HERE
$colors = array( 'red', 'green', 'blue' );

// IF THERE IS A REQUEST
if (!empty($_GET['color']))
{
    if (in_array($_GET['color'], $colors))
    {
        echo "YOU CHOOSE " . $_GET['color'];
    }
    else
    {
        echo "INVALID INPUT";
    }
}

// CREATE THE OPTION TAGS FROM THE KNOWN GOOD VALUES
$opts = '<option value="">Choose A Color</option>' . PHP_EOL;
foreach ($colors as $color)
{
    $opts .= '<option value="' . $color . '">' . $color . '</option>' . PHP_EOL;
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<FORM
<form>
<select name="color">
$opts
</select>
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

HTH, ~Ray
0
 
webdesigner_dkAuthor Commented:
Thanks for the answer. Could you please be give me an example relevant to my code. Thanks.
0
 
Ray PaseurCommented:
The answer I gave is about the best I can do, and it teaches the standard design pattern.  I do not have any test data set that I can use to test your code, I do not have the functions that your code uses, etc.

If you're new to PHP and want to learn the language, this article may help guide to good learning resources, and more importantly, guide you away from bad examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now