Jump to content

This should be really easy but...


JPark

Recommended Posts

It is driving me crazy!  :facepalm:

 

I have a simple array of the states as such:

$states = array();
$states['AL'] = "Alabama";
$states['AK'] = "Alaska";
$states['AZ'] = "Arizona";
$states['AR'] = "Arkansas";
$states['CA'] = "California";
$states['CO'] = "Colorado";
$states['CT'] = "Connecticut";
etc

 

from which I make a simple list/form:

<form name="myform" method="post" action="test.php">
<select name="states[]" size="10" multiple="yes">
	<?
		foreach ($states as $key=>$value) 
			{
				echo "<option name='".$key."'>".$value."</option>";
			}
	?>
</select>

<input type="submit" value="submit" />
    
<input name="reset" type="reset" id="reset" value="Clear Form" />
</form>

 

When the selections are sent to "test.php", I display their choices:

$states=$_POST['states'];
$sectors=$_POST['sectors'];

if (is_array($states) && isset($states) )
{
	echo "<b>You have selected the following state(s):</b><br><blockquote>";
	foreach($_POST['states'] as $v)
		{

			if( in_array($v, $states, TRUE) )
				{
					echo $v."<br>";
				}
		}
	echo "</blockquote>";
} 
	else 
		{
			echo "<p class='notice'>You have not selected any states.</p>";
		}

 

So far, so good...

 

But, I want to display the picked states AND (in another spot on the page) display the abbreviations for the picked states.

 

How?  It seems that I can only send to "test.php" the state name OR its abbreviation but not both.

 

Help??

Link to comment
https://forums.phpfreaks.com/topic/167212-this-should-be-really-easy-but/
Share on other sites

The name of the postdata is defined in the <select> tag, you should be using

echo "<option value=\"" . $key . "\">" . $value . "</option>\n";

 

// edit

 

You could actually use:

 

echo "<option value=\"" . $key . "-" . $value . "\">" . $value . "</option>\n";

 

Then in your foreach loop explode the posted value ($v) with the dilemiter "-" nd end up with an array of both the abbreviation and full state name.

See???

 

I TOLD you it was easy.

 

echo "<b>These are the state abbreviations:</b><br>";
foreach ($_POST['states'] as $temp)
{
	$state_chunks=explode("-",$temp);
	echo $state_chunks[0]."<br />";
	}


if (is_array($states) && isset($states) )
{
	echo "<b>You have selected the following state(s):</b><br><blockquote>";
	foreach($_POST['states'] as $v)
		{

			if( in_array($v, $states, TRUE) )
				{
					echo $v."<br>";
				}
		}
	echo "</blockquote>";
} 

 

Much thanks, Andy!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.