Solved

Select dropdown list validation

Posted on 2013-11-13
5
2,030 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
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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the best PDF generator to use? 1 30
How to loop through records  using SELET COUNT 2 16
Method for submitting dynamically created rows 20 17
Bad <form> statement? 9 26
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to dynamically set the form action using jQuery.
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…

867 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

20 Experts available now in Live!

Get 1:1 Help Now