php and html select command (little question here)

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
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

php and html select command (little question here)

Post by grabber_grabbs »

How to assign different value using HTML Select command :
Echo "<FORM NAME=order onSubmit=\"AddToCart(this); return false\"> ";
echo " <input type=hidden name=NAME value=\"$desc\" >";
Echo " <input type=hidden name=\"ID_NUM\" value=\"$prodno\"> ";
echo "<input type=\"hidden\" name=\"SHIPPING\" value=\"0.00\">";
echo "<input type=\"hidden\" name=\"WEIGHT\" value=\"0\">";

Echo " Category: <select name=\"CATEGORY\">";
Echo " <option value=\"$catA1\">$catA1</option>";
Echo " <option value=\"$catA2\">$catA2</option>";
Echo " <option value=\"$catA3\">$catA3</option>";
Echo " </select>";

the above code work fine but would like to add avariable to the code. So depending on the category you choose,
the price with be different. The info is stored in MySQL database and retreived in $priceA1, $priceA2 and
$priceA3

What i want to do is assigne a value in the variable price = \"$priceA1\" depending on what option selected
If the fist option selected, assign $priceA1 to variable price, if option 2 selected, assign $priceA2 to
variable price, and finally if option 3 is selected, assign $priceA3 to price

something like this

Echo " Format: <select name=\"CATEGORY\">";
Echo " <option price= \"$priceA1\" value=\"$catA1\">$catA1</option>";
Echo " <option price= \"$priceA2\" value=\"$catA2\">$catA2</option>";
Echo " <option price= \"$priceA3\" value=\"$catA3\">$catA3</option>";
Echo " </select>";

the above code does not work. Did search on internet for the proper way to code it but cant find anything

Maybe someone here could help me resolve this problem.
thanks.
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: php and html select command (little question here)

Post by Amanda1998 »

No to sure about Second added avariable to the code.. :crazy:
But, how about, making the option-name and set it as value of price ? ... like:

Code: Select all

<option name="Price_Cat1" value="$ 350" id="Cat1">Cat1 = $ 350</option>
to something like :

Code: Select all

<?php
	echo "<form action=\"<?=$_SERVER[\'PHP_SELF\']?>\" method=\"post\">\n";
	echo "<select name=\"order\" multiple=\"multiple\">\n";
	echo "    <option name=\"Price_Cat\" value=\"Cat1 = $ 350\" id=\"Cat1\">Cat1 = $ 350</option>\n";
	echo "    <option name=\"Price_Cat\" value=\"Cat2 = $ 450\" id=\"Cat1\">Cat2 = $ 450</option>\n";
	echo "    <option name=\"Price_Cat\" value=\"Cat3 = $ 550\" id=\"Cat1\">Cat3 = $ 550</option>\n";
	echo "</select>\n";
	echo "<input type=\"submit\" value=\"Send\" />\n";
	echo "</form>\n";

	$order_list = $_POST['order'];
	if ($order_list)
	{
	foreach ($order_list as $ord)
	{
/** Bellow ... You echoes, query's, if or else statements, etc.... ??? **/
/** like **/
	echo 'You selected ',$ord,'<br />';
/** and, or **/
	$query = "INSERT INTO order(Price_and_Cat) VALUES('$ord')";
/** **/
/** */
?>
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: php and html select command (little question here)

Post by grabber_grabbs »

wow, sounds good.

will test this and come back with the results


thanks.
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: php and html select command (little question here)

Post by grabber_grabbs »

oups someting i noticed though, is that i allready have the code value= in the option...

value = \"$catA1\"

once selected i have the value of \"catA1\" stored in CATEGORY,

now how can i have the priceA1 associated with that catA1 to be stored in the variable price ?
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: php and html select command (little question here)

Post by Amanda1998 »

grabber_grabbs wrote:oups someting i noticed though, is that i allready have the code value= in the option...
value = \"$catA1\"
Don't follow you..? ...Did you meant ?

Code: Select all

<?php
        echo "<form action=\"<?=$_SERVER[\'PHP_SELF\']?>\" method=\"post\">\n";
        echo "<select name=\"order\" multiple=\"multiple\">\n";
        echo "    <option name=\"catA1\" value=\"catA1 = $ 350\" id=\"catA1\">catA1 = $ 350</option>\n";
        echo "    <option name=\"catA2\" value=\"catA2 = $ 450\" id=\"catA2\">catA2 = $ 450</option>\n";
        echo "    <option name=\"catA3\" value=\"catA3 = $ 550\" id=\"catA3\">catA3 = $ 550</option>\n";
        echo "</select>\n";
        echo "<input type=\"submit\" value=\"Send\" />\n";
        echo "</form>\n";

        $order_list = $_POST['order'];
        if ($order_list)
        {
        foreach ($order_list as $ord)
        {
/** Bellow ... You echoes, query's, if or else statements, etc.... ??? **/
/** like **/
        echo 'You selected ',$ord,'<br />';
/** and, or **/
        $query = "INSERT INTO order(Price_and_Cat) VALUES('$ord')";
/** **/
/** */
?>
grabber_grabbs wrote:once selected i have the value of \"catA1\" stored in CATEGORY,
now how can i have the priceA1 associated with that catA1 to be stored in the variable price ?
Do you meant this ?

Code: Select all

<?php
/** **/
/** **/

	$Product = "Cat 1  $ 350";
	if($Product == "Cat 2  $ 450")
	{
	echo "blah, blah about $Product";
	/** and , or **/
	$query = "INSERT INTO order(Price_and_Cat) VALUES('$Product')";
	/** ** **/
	}
	 elseif($Product == "Cat 1  $ 350")
	{
	echo "blah, blah about $Product";
	/** and , or **/
	$query = "INSERT INTO order(Price_and_Cat) VALUES('$Product')";
	/** ** **/
	}
	else
	{
	echo "You din't make a Product choice";
	}
/** **/
/** **/
?>
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: php and html select command (little question here)

Post by Amanda1998 »

Let me do this, You can consider this following example:

Code: Select all

<div id= "mydiv">
    	<label for="bithdate">Enter your DOB</label>
<select name="year" id="id_here">
	<option value="2011">2011</option>
	<option value="2010">2010</option>
	<option value="2009">2009</option>
</select>
<select name="month" id="id_here">
	<option value="January">January</option>
	<option value="February">February</option>
	<option value="March">March</option>
</select>
<select name="date" id="id_here">
	<option value="01">1</option>
	<option value="02">2</option>
	<option value="03">3</option>
</select><br />
</div>
/***  code   ***/
/***  code   ***/
/*** then on you (proccess) you do ***/
	$year = $_POST['year'];
	$month = $_POST['month'];
	$date = $_POST['date'];
/***  code   ***/
/***  OPTION 1) and you can INSERT all data together like   ***/
	$MyDOBvar =  $_POST['year'].'-'.$_POST['month'].'-'.$_POST['date'];
	$query = "INSERT INTO tablename(NameOfMyColumn) VALUES('$MyDOBvar')";
	$result = @mysql_query($query);
	if($result) {
	echo ' blah, blah ';
	/** code keeps goin here **/
/***  code   ***/
/***  OPTION 2) OR you can INSERT all data SEPARATED like   ***/
	$query = "INSERT INTO tablename(year, month, date) VALUES('$year', '$month', '$date')";
	$result = @mysql_query($query);
	if($result) {
	echo ' blah, blah ';
	/** code keeps goin here **/
	/** All depends on struture of your DB, or how you wanna keep records on it.. good luck **/
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php and html select command (little question here)

Post by social_experiment »

grabber_grabbs wrote:What i want to do is assigne a value in the variable price = \"$priceA1\" depending on what option selected
If the fist option selected, assign $priceA1 to variable price, if option 2 selected, assign $priceA2 to
variable price, and finally if option 3 is selected, assign $priceA3 to price
Do you want to assign a value to a option that you just selected?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: php and html select command (little question here)

Post by grabber_grabbs »

ok sorry for my poor english. Let take this example

I sell shirt.

I have a MySQL database with shirt in it.

Here is the database

itemno = 12345
Description = "shirt "
catA1 = "Small"
pricecatA1 = 15.00
catA2 = "Medium"
pricecatA2 = 17.00
catA3 = "Large"
procecatA3 = 19.50

// note here i have no problem loading in memory the value of all fields present in the MySQL database. The problem i have is that i want to send the value of the PHP variable into the javascript addtocart(this) function. If my customer select the Small shirt, i want to load the price variable with the 15.00 value. If my customer select the large shirt, 19.50 has to be loaded into the price variable.

NOW what i have is that if the customer select the first option, i have CATEGORY = "small" loaded. (fine)
How can i have 15.00 loaded in the variable price ?

in my E-Shop i have all my shirt displayed with a HTML Selection like this :

Echo "<FORM NAME=order onSubmit=\"AddToCart(this); return false\"> ";
echo " <input type=hidden name=NAME value=\"$desc\" >";
Echo " <input type=hidden name=\"ID_NUM\" value=\"$prodno\"> ";
echo "<input type=\"hidden\" name=\"SHIPPING\" value=\"0.00\">";
echo "<input type=\"hidden\" name=\"WEIGHT\" value=\"0\">";

Echo " Category: <select name=\"CATEGORY\">";
Echo " <option value=\"$catA1\">$catA1</option>";
Echo " <option value=\"$catA2\">$catA2</option>";
Echo " <option value=\"$catA3\">$catA3</option>";
Echo " </select>";

** again with the code above, if the customer select the fist option, i will have CATEGORY = "Small" to be sent to the javascript addtocart() function.

1000 times thanks for helping here.
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: php and html select command (little question here)

Post by Amanda1998 »

@ grabber_grabbs do you speak Spanish.. If "yes" read this... If "not" I will edited..
Hay muchas maneras de hacerlo. Si todos tus datos entran bien en la base de datos, (cada Articulo, con su precio, en su propia columna, etc).. entonces solo tienes que guardar el resultado, nombrando una nueva variable del producto elegido.. como? ... Si miras el ejemplo de la fecha de nacimiento (arriba) .. donde te escribo /*** OPTION 1) and you can INSERT all data together like ***/

Bueno en ese caso creamos una nueva variable que se llamaba $MyDOBvar, que recogia las entradas de year, month y date (todo en una solo variable, la que se puede recoger desde java script:

Code: Select all

<script type="Javascript">
<!--
var string="<?php echo $MyDOBvar; ?>";
//-->
</script>
Te recomiendo dropdown porque puedes insertar varios valores en diferentes campos, y almacenarlos en tu base de datos "juntos o separados", y de igual manera manipularlos "juntos o separados"
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php and html select command (little question here)

Post by social_experiment »

@grabber_grabbs :) The explanation was good the first time i just needed to confirm what you wanted to do; have a look at this url
http://www.javascriptkit.com/javatutors/dom2.shtml
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: php and html select command (little question here)

Post by grabber_grabbs »

Spanish is so beautiful to ear... sorry but i dont speak spanish... Thanks Google for translate though.
The way you seems to explain is to transfer the variable to the server... this is not my case here. Im on customer side here.
maybe i dont understand.

Look what i have found on internet that seems to be exactly what i need. look at how each <option has different variable in it... for 1 Litre bottle selection, that would load cost=1.00, id_num="33251 etc etc...

In my example, i would need to load a different price per selection.


<select name="PRODUCTSELECTOR">
<option selected name="select">Please select size</option>
<option cost="1.00" id_num="33251" name="1L bottle Sheep Dip" shipping="1.00">1 Litre bottle $1.00 Shipping $1.00 </option>
<option cost="1.00" id_num="33253" name="1L bag Sheep Dip" shipping="1.50">1 Litre bag $1.00 shipping $1.50 </option>
<option cost="3.00" id_num="33252" name="4L can Sheep Dip" shipping="2.00">4 Litre can $3.00 Shipping $2.00 </option>
<option cost="10.00" id_num="33254" name="205L Drum Sheep Dip" shipping="120.00">205 Litre (45 Gal/55 US Gal) Drum $200.00 Shipping $120.00</option>
</select><BR>

if the example above works... (i assume) why mine is not working
Echo " Format: <select name=\"CATEGORY\">";
Echo " <option price= \"$priceA1\" value=\"$catA1\">$catA1</option>";
Echo " <option price= \"$priceA2\" value=\"$catA2\">$catA2</option>";
Echo " <option price= \"$priceA3\" value=\"$catA3\">$catA3</option>";
Echo " </select>";
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: php and html select command (little question here)

Post by grabber_grabbs »

thanks guys, found my problem.
Post Reply