Solved

PHP form and Variable

Posted on 2010-11-18
9
206 Views
Last Modified: 2012-05-10
How do I turn a for id into a variable?

Like the below simple piece of script:
<?php
if (isset($_GET['submitted'])) {
$color = @$_GET The Option ID;
}
?>


<form action="this-page.php" method="GET">
<select>
<option name="color" id-"1" value="walnut">Walnut</option>"; 
<option name="color" id="2" value=\"whiteBlonde">White Blonde</option>"; 
</select>

<input type="submit" name="submit" value="go" />
	<input type="hidden" name="submitted" value="TRUE" />
	</form>

Open in new window

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:roynaufal
ID: 34169359
can you explain your question a bit more? didn't really understand you
thanks
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 34169380
There's a bunch of syntactical typos in your html. You'll need to clean that up before it will work.

But aside from that, you are using the <select></select> entity wrong. You do it something like this:

<select name="color">
<option id="1" value="walnut">Walnut</option>
<option id="2" value="whiteBlonde">White Blonde</option>
</select>

Then you can access it in your php as follows:

<?PHP
    echo $_GET["color"];       <-- will output whatever the user picked in the "color" select box
?>

Note that assigning an "id" attribute to each option is not necessary - and in this case is unneeded. The "id" attribute isn't used in forms or in PHP, it's just for Javascript to access the DOM which you aren't using in this simple example.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 34169589
I want to use the id to turn into a variable, in addition to the value.  So the GET will create two variables.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:rgranlund
ID: 34169864
Can I do:
<select name="color">
<option id="1" value="walnut">Walnut</option>
<option id="2" value="whiteBlonde">White Blonde</option>
</select>


<?PHP
    echo $_GET["color"];   
    echo $_GET['id'];
?>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34169976
No, that won't work.  Forms submit info in 'name/value' pairs where the value is associated with the 'name' attribute, not the 'id'.  If you take the 'name' out, the PHP page won't see anything.  It has to have the name.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 34170051
so is there answer to what I want to do?
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 34170249
You would have to include the 'id' number as part of the value and split them in the PHP page.   Something like '1:walnut' and 'explode' it into two parts in PHP.  Note also that 'id' already has a special meaning in HTML.
<select name="color">
<option value="1:walnut">Walnut</option>
<option value="2:whiteBlonde">White Blonde</option>
</select>


<?php
  $display = array();
  $display = explode(":", $_GET["color"]);
  echo $display[0];   
  echo $display[1];
?>

Open in new window

0
 
LVL 13

Expert Comment

by:dsmile
ID: 34172819
This should do the trick: get selected option's id value

btw, to have a better and more "production" solution, you should tell why you want to do it anyway :)
<?php
if (isset($_GET['submit'])) {
	die('Selected ID is: '. $_GET['selected_id']);
}
?>
<script language="JavaScript">
<!--
function getId() {
	var id = document.forms[0].color[document.forms[0].color.selectedIndex].id;
	document.forms[0].selected_id.value = id;

	return true;

}
//-->
</script>

<form action="?" method="get" onsubmit="return getId()">
<select name="color">
<option id="1" value="walnut">Walnut</option>"; 
<option id="2" value="whiteBlonde">White Blonde</option>"; 
</select>

<input type="submit" name="submit" value="go" />
        <input type="hidden" name="selected_id" />
        </form>

Open in new window

0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 34176547
Thanks for the simple PHP.  I knew it was simple, I just could not get my head around it.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if (is_singular not working 5 42
Make check boxes work 8 47
PHP Web Development 6 30
Difference between PHPClasse.php and SimplXLS.php 5 26
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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