Page 1 of 1

Passing variable to OPTION VALUE statement

Posted: Sun May 10, 2009 9:00 pm
by imimin
Cansomeone help me out here?

I am trying to set up a _GET command and echo a variable in an OPTION statement in a dropdown list. What I have so far is this (line 3 is my challenge) , but it doesn't work:

Code: Select all

                    Garment Style:
                    <SELECT NAME=custom10 SIZE=1>
                    <OPTION SELECTED="SELECTED" VALUE=<?php $item_prod_name = $_GET['item_prod_name']; echo "$item_prod_name">echo "$item_prod_name"</OPTION>?>
                    <OPTION VALUE="Goucho Pants w/Elastic Waist">Goucho Pants w/Elastic Waist</OPTION>
                    <OPTION VALUE="Goucho Pants w/Draw String">Goucho Pants w/Draw String</OPTION>
                    <OPTION VALUE="Poncho w/Hood">Poncho w/Hood</OPTION>
                    <OPTION VALUE="Poncho without Hood">Poncho without Hood</OPTION>
                    <OPTION VALUE="Baby Bunting">Baby Bunting</OPTION>
                    <OPTION VALUE="Romper">Romper</OPTION>
                    </SELECT><BR>
Any help on this would be appreciated.

Re: Passing variable to OPTION VALUE statement

Posted: Sun May 10, 2009 9:19 pm
by imimin
I also tried this:

Code: Select all

                    <?php
                    $item_prod_name = $_GET['item_prod_name'];
 
                    Garment Style:
                    <SELECT NAME=custom10 SIZE=1>
                    <OPTION SELECTED="SELECTED" VALUE=echo "$item_prod_name">echo "$item_prod_name"</OPTION>
                    <OPTION VALUE="Goucho Pants w/Elastic Waist">Goucho Pants w/Elastic Waist</OPTION>
                    <OPTION VALUE="Goucho Pants w/Draw String">Goucho Pants w/Draw String</OPTION>
                    <OPTION VALUE="Poncho w/Hood">Poncho w/Hood</OPTION>
                    <OPTION VALUE="Poncho without Hood">Poncho without Hood</OPTION>
                    <OPTION VALUE="Baby Bunting">Baby Bunting</OPTION>
                    <OPTION VALUE="Romper">Romper</OPTION>
                    </SELECT><BR>
                    ?>
and it DIDN'T WORK and got this error:

Parse error: syntax error, unexpected T_STRING in /file/poj/poj_order_form.php on line 65

Thank you!

Re: Passing variable to OPTION VALUE statement

Posted: Sun May 10, 2009 10:30 pm
by mickd
Any HTML has to be echo'ed out from the PHP. You can't have this:

Code: Select all

 
<?php
 
$foo = 'Hello!';
 
<div>
echo $foo;
</div>
 
?>
 
But you can have this:

Code: Select all

 
<?php
 
$foo = 'Hello!';
 
echo '<div>';
echo $foo;
echo '</div>';
 
// or
 
echo '<div>'.$foo.'</div>';
 
// or
?>
 
<div><?php echo $foo; ?></div>
 
Hope that helps.

Re: Passing variable to OPTION VALUE statement

Posted: Mon May 11, 2009 11:09 am
by imimin
OK then, when I try this:

Code: Select all

                <?php
                    $item_selected_style = $_GET['item_selected_style'];
 
 
                    echo "Garment Style:";
                    <SELECT NAME=custom10 SIZE=1>;
                    echo 
                    "<OPTION SELECTED="SELECTED" VALUE="$item_selected_style">"$item_selected_style"</OPTION>
                    <OPTION VALUE="Goucho Pants w/Elastic Waist">Goucho Pants w/Elastic Waist</OPTION>
                    <OPTION VALUE="Goucho Pants w/Draw String">Goucho Pants w/Draw String</OPTION>
                    <OPTION VALUE="Poncho w/Hood">Poncho w/Hood</OPTION>
                    <OPTION VALUE="Poncho without Hood">Poncho without Hood</OPTION>
                    <OPTION VALUE="Baby Bunting">Baby Bunting</OPTION>
                    <OPTION VALUE="Romper">Romper</OPTION>";
                    </SELECT><BR>;
                    ?>
it is still NOT working. I get the following error:

Parse error: syntax error, unexpected '<' in /homepages/27/d120150310/htdocs/poj/poj_order_form.php on line 69

Thank you!

Re: Passing variable to OPTION VALUE statement

Posted: Mon May 11, 2009 1:25 pm
by mdk999
your issue is that you have not escaped the quotes within the echo statement. You can get around this by using a multi-line string ..

Code: Select all

 
               <?php
                    $item_selected_style = $_GET['item_selected_style'];
 
 
$select = <<<FO
                      Garment Style:
                    <SELECT NAME=custom10 SIZE=1>
                    <OPTION SELECTED="SELECTED" VALUE="{$item_selected_style}">"{$item_selected_style}"</OPTION>
                    <OPTION VALUE="Goucho Pants w/Elastic Waist">Goucho Pants w/Elastic Waist</OPTION>
                    <OPTION VALUE="Goucho Pants w/Draw String">Goucho Pants w/Draw String</OPTION>
                    <OPTION VALUE="Poncho w/Hood">Poncho w/Hood</OPTION>
                    <OPTION VALUE="Poncho without Hood">Poncho without Hood</OPTION>
                    <OPTION VALUE="Baby Bunting">Baby Bunting</OPTION>
                    <OPTION VALUE="Romper">Romper</OPTION>
                    </SELECT><BR>
FO;
 
echo $select;
                    ?>