Solved

Select dropdown list validation

Posted on 2013-11-13
5
2,005 Views
Last Modified: 2013-12-03
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
Comment
Question by:webdesigner_dk
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
dimmergeek earned 200 total points
Comment Utility
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
 

Author Comment

by:webdesigner_dk
Comment Utility
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
Comment Utility
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
 

Author Comment

by:webdesigner_dk
Comment Utility
Thanks for the answer. Could you please be give me an example relevant to my code. Thanks.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now