?
Solved

Select dropdown list validation

Posted on 2013-11-13
5
Medium Priority
?
2,143 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 800 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1200 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

Independent Software Vendors: 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Find out what you should include to make the best professional email signature for your organization.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

765 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