Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating arrays

Status
Not open for further replies.

dadms

Technical User
Mar 8, 2003
67
0
0
US
I have the following drop down boxes for people to select on a form. I would like to make sure when they submit the form that at least on of the drop down boxes has a value selected. As of right now when they submit I get NONE in my email and database. Could someone please help me out with how to do this in PHP.

<select name='local[]' multiple size="1" style="font-size: 8pt">
<option selected value="NONE">NONE</option>
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>

<select name='foreign[]' multiple size="1" style="font-size: 8pt">
<option selected value="NONE">NONE</option>
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>

<select name='modern[]' multiple size="1" style="font-size: 8pt">
<option selected value="NONE">NONE</option>
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>


Thanks in Advance
 
I don't understand what it is you're asking.

Given an HTML page, based on your post, which reads:

Code:
<html><body>	
<form method="POST" action="show_post.php">
	<select name="local[]" multiple style="font-size: 8pt">
		<option selected value="NONE">NONE</option>
		<option value="city1">city1</option>
		<option value="city2">city2</option>
	</select>
	
	<select name="foreign[]" multiple style="font-size: 8pt">
		<option selected value="NONE">NONE</option>
		<option value="city1">city1</option>
		<option value="city2">city2</option>
	</select>
	
	<select name="modern[]" multiple style="font-size: 8pt">
		<option selected value="NONE">NONE</option>
		<option value="city1">city1</option>
		<option value="city2">city2</option>
	</select>

	<br><input type="submit">
</form>
</body></html>

(notice I've removed the 'size="1"' attributes from your selects. multiselects are difficult to use when they only display one option at at time).

The form submits to show_post.php, which reads:
Code:
<?php
print '<html><body><pre>';
print_r ($_POST);
print '</pre></body></html>';
?>

When I point my browser to the HTML page, pick just "NONE" in the first select, "NONE" and "city1" in the second, "NONE" and "city2" in the third then submit, the script returns:

Code:
Array
(
    [local] => Array
        (
            [0] => NONE
        )

    [foreign] => Array
        (
            [0] => NONE
            [1] => city1
        )

    [modern] => Array
        (
            [0] => NONE
            [1] => city2
        )

)

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleepnir thanks for the multiple heads up.

I am trying to verify that in at least one of the arrays that something other than NONE is selected. But my validating techniques seem weak.

if (($local == 'NONE') && ($foreign == 'NONE') && ($modern == 'NONE')){
print '<p>You must select at least one city.</p>'
}
else

{ blah blah blah
 
if NONE is selected on all the dropdowns I would like You must select at least one city - to display.

<?php
if (($local == 'NONE') && ($foreign == 'NONE') && ($modern == 'NONE')){
print '<p>You must select at least one city.</p>';
}
else
{
print '<html><body><pre>';
print_r ($_POST);
print '</pre></body></html>';
}
?>
 
You must keep in mind that when using multiselects with PHP, the input in the form will be an array. So comparing $local, $foreign, and $modern to 'NONE' will always return FALSE.

I recommend that for each of the three arrays, you set a flag in a variable to TRUE, then loop through the array looking for an array element that does not have the value 'NONE'. If the loop finds one, set the flag to FALSE.

Then you can check whether $flag1 and $flag2 and $flag3 are all equal to FALSE. If all three are, no cities were selected.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Slepnir214,

Would you be able to point me in the right direction with an example or a URL to documentation?

Thanks
 
Something like:

Code:
<?php

$found_local = FALSE;
foreach ($_POST['local'] as $local)
{
	if ($local != 'NONE')
	{
		$found_local = TRUE;
	}
}

$found_foreign = FALSE;
foreach ($_POST['foreign'] as $foreign)
{
	if ($foreign != 'NONE')
	{
		$found_foreign = TRUE;
	}
}

$found_modern = FALSE;
foreach ($_POST['modern'] as $modern)
{
	if ($modern != 'NONE')
	{
		$found_modern = TRUE;
	}
}

if (!$found_local && !$found_foreign && !$found_modern)
{
	print "You must select at least one city.";
}

?>

should do it.

Another method, a little obscure but easier to change (as you only have to change the values of variables to change behavior) is:

Code:
<?php
//this variable determines whether a value other than "NONE" must be selected
//from each dropdown, or just one non-"NONE" value among all the dropdowns.
//TRUE indicates that each field must have a selected value.
$one_from_each = FALSE;

//this holds the form names to check
$form_names = array ('local', 'foreign', 'modern');

$form_check_value = 0;
$bit = 1;
foreach ($form_names as $form_name)
{
	foreach ($_POST[$form_name] as $form_selection)
	{
		if ($form_selection != 'NONE')
		{
			$form_check_value |= $bit;
		}
	}
	$bit = $bit << 1;
}

$input = 'bad';
if ($one_from_each)
{
	if ($form_check_value == pow (2, count($form_names)) - 1)
	{
		$input = 'good';
	}
}
else
{
	if ($form_check_value != 0)
	{
		$input = 'good';
	}
}

print $input;

?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top