Select get value using array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Select get value using array?

Post by jayson.ph »

Hi All,

how to link, where the user choose or select the the following below using array?

Code: Select all

<form name = "1" action = '<?php echo ($_SERVER['PHP_SELF']); ?>' method = 'GET'>
<select name = "seleted" onChange = this.form.submit();>
	<option value = '1'>11</option>
	<option value = '2'>22</option>
	<option value = '3'>33</option>
<?php
	$array_name = array();
	
	if($_GET['seleted']=== 1)
	{
		$array_name[] = 'one.php';
	}
		if($_GET['seleted'] === 2)
		{
			$array_name[] = 'two.php';
		}
			if($_GET['seleted'] === 3)
			{
				$array_name[] = 'tree.php';
			}
	if(sizeof($array_name)>0)
	{
		foreach($array_name as $array_name_value)
		require ("$array_name_value");
	}
?>
</select>
<noscript><input type = 'submit'></noscript>

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Select get value using array?

Post by shiznatix »

Code: Select all

<!-- DONT USE PHP_SELF FOR A FORM ACTION, PUT THE ACTION LOCATION! -->
<form name="1" action="<?php echo ($_SERVER['PHP_SELF']); ?>" method="GET">
	<select name="seleted" onChange="this.form.submit();">
		<option value="1">11</option>
		<option value="2">22</option>
		<option value="3">33</option>
		<?php
		$array_name = array();

		/*
		Two things! First, you can't be sure the $_GET['selected'] exists yet so do that check first
		Second, values from GET are sent as strings and === checks based on data type as well.
			This means that 1 === $_GET['selected'] is asking if $_GET['selected'] is an INT equal to 1 which it never will be
			Use the == instead to solve this (or put the 1 in quotes)
		*/
		if (isset($_GET['seleted']) && '1' == $_GET['seleted'])
		{
			$array_name[] = 'one.php';
		}
		else if (isset($_GET['seleted']) && '2' == $_GET['seleted'])
		{
	        $array_name[] = 'two.php';
		}
		else if (isset($_GET['seleted']) && '3' == $_GET['seleted'])
		{
			//did you mean "three.php"?
			$array_name[] = 'tree.php';
		}
		
		//an empty array will return false in an if check so you can just do this...
		if ($array_name)
		{
			//with your code, $array_name can only have 1 element so no need to loop (or even use an array)
			require $array_name[0];//this is dirty, you should just use a string variable instead
			
			/*
			foreach($array_name as $array_name_value)
				require ("$array_name_value");
			*/
		}
		?>
	</select>
	<noscript><input type="submit" value="submit"></noscript>
</form>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Select get value using array?

Post by twinedev »

If you are asking how to have it take someone to a webpage when they choose the dropdown option (without submitting the form), then that is a javascript issue, you would end up wanting to do a window.location = {url} call.

If you actually wanting to do it on the backend so that it actually does submit back to the server, then off the top of my head there are two options:

1. The script you post to just flat out does an include({file}); to include the file right in line (you would want to make sure this is before any output from the page the form is submitted to most likely.

2. Have it do a header() call to do a redirect

-Greg

PS, it took me a minute to find the reason for shiznatix's reply, then did notice the comment in the code that was added, which is correct, you never want to use $_SERVER['PHP_SELF'] as that opens you up to an XSS attack on the site. ($_SERVER['PHP_SELF'] is something that can be set by the person who is visiting your site, and should never be directly used, same as $_SERVER['HTTP_USER_AGENT'] $_SERVER['HTTP_REFERRER'] and anything in $_POST, $_GET, $_COOKIE, $_REQUEST.
Post Reply