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
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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.