Solved

Select dropdown list validation

Posted on 2013-11-13
5
2,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
dimmergeek earned 200 total points
ID: 39644675
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
ID: 39645869
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 39645967
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
ID: 39646167
Thanks for the answer. Could you please be give me an example relevant to my code. Thanks.
0
 
LVL 110

Assisted Solution

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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

688 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