Page 1 of 1

Using the value of a drop list in the current page

Posted: Tue Jul 15, 2003 5:20 am
by steedvlx
Hello again all.

This time I have a question which might actually be intelligently phrased. :wink:

BACKGROUND:
I have a form which updates a single record in the database. On the form, there is a drop down list for 'quantity'. The current quantity database value (usually 1) is selected by default in the box.

When the user selects a new quantity, I want to multiply that value by the unit price to get a line total which will be updated to the database at the same time.

PROBLEM...
I don't know what to call the 'selected' value in my equation. Here is the code for the FORM in question. Could someone clue me in on how I'm supposed to retrieve this select value once chosen.

Code: Select all

<form name="update_cart" method="POST" action="<?php echo $editFormAction; ?>">
          <table width="790" height="103" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFF" id="cart_tbl">
            <tr bgcolor="#990000"> 
              <td width="92" height="30" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Part 
                Number</font></strong></td>
              <td width="278" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Description</font></strong></td>
              <td width="38" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Price 
                (Yen) </font></strong></td>
              <td width="25" align="center" valign="middle" bordercolor="#990000" bgcolor="#33CC00"><strong><font color="#FFFFFF" size="4" face="Arial, Helvetica, sans-serif">QTY</font></strong></td>
              <td width="79" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Total 
                (Yen) </font></strong></td>
              <td align="center" valign="middle" bordercolor="#990000" bgcolor="#990000">&nbsp;</td>
            </tr>
            <tr align="center" valign="middle"> 
              <td width="92" height="71" bordercolor="#990000" bgcolor="#CCCCCC"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row_rs_cart_edit&#1111;'cart_kb_prod_part_num']; ?> </font></td>
              <td width="278" align="left" valign="middle" bordercolor="#990000"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row_rs_cart_edit&#1111;'prod_e_desc']; ?> </font></td>
              <td bordercolor="#990000" bgcolor="#CCCCCC"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row_rs_plu&#1111;'prod_sell_price_jpy']; ?></font></td>
              <td width="25" bordercolor="#990000" bgcolor="#33CC00"> <center>
                  <font color="#000000" size="2" face="Arial, Helvetica, sans-serif"></font> 
                  <font color="#000000" size="2" face="Arial, Helvetica, sans-serif"> 
                  <select name="quantity_sel" size="1" id="select">
                    <option value="1" selected <?php if (!(strcmp(1, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>1</option>
                    <option value="2" <?php if (!(strcmp(2, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>2</option>
                    <option value="3" <?php if (!(strcmp(3, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>3</option>
                    <option value="4" <?php if (!(strcmp(4, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>4</option>
                    <option value="5" <?php if (!(strcmp(5, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>5</option>
                    <option value="6" <?php if (!(strcmp(6, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>6</option>
                    <option value="7" <?php if (!(strcmp(7, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>7</option>
                    <option value="8" <?php if (!(strcmp(8, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>8</option>
                    <option value="9" <?php if (!(strcmp(9, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>9</option>
                    <option value="10" <?php if (!(strcmp(10, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>10</option>
                  </select>
                  </font> </center></td>
              <td bordercolor="#990000" bgcolor="#CCCCCC"> <center>
                  <font color="#009900" size="2" face="Arial, Helvetica, sans-serif"><strong><?php echo $row_rs_cart_edit&#1111;'cart_line_total']; ?> <font color="#000000"> JPY</font></strong></font> 
                </center></td>
              <td width="40" align="center" valign="middle" nowrap bordercolor="#990000" bgcolor="#FFFFFF"> 
                <center>
                  <input name="cart_line_total" type="hidden" value="<?php echo ($row_rs_plu&#1111;'prod_sell_price_jpy'] * $select); ?>">
                  <input name="cart_id_num" type="hidden" value="<?php echo $row_rs_cart_edit&#1111;'cart_id_num']; ?>">
                  <input name="Submit" type="image" value="Update Cart" src="../images/e_update_cart.gif" align= "middle" width="105" height="48">
                </center></td>
            </tr>
          </table>
          <input type="hidden" name="MM_update" value="update_cart">
        </form>
The line of code that reads;

Code: Select all

<input name="cart_line_total" type="hidden" value="<?php echo ($row_rs_plu&#1111;'prod_sell_price_jpy'] * $select); ?>">
is the only one I cannot get to work no matter what I put in place of the "$select". I've tried select, quantity_sel and all variations of these.

Any help greatly appreciated.

--------------------------------
SteedVLX

Posted: Tue Jul 15, 2003 7:56 am
by Tubbietoeter
for inputs with type=hidden the select makes no sense. this is like an invisible text input field so it cannot be selected.


when you click the submit button, the contents the user selected or entered into the form are passed to the site specified in the <form> tag. on this site type
echo "<pre>"; print_r($_POST); echo "</pre>";
at the beginning of the site to view the values that were passed.

Posted: Tue Jul 15, 2003 10:10 am
by steedvlx
Thank you very much for your reply. But, I think you may have misunderstood the situation. (Not hard to figure given the great blob of code I posted for you) :lol:

The Select is not a hidden field. It is very much visible and essential for the user to select this value.

The problem is that I want to use this user-selected value 'immediately' to calculate and assign a value to a hidden input field.

In this example, The user selects a quantity (1-10) when they do this, and click submit (which will UPDATE the record) I want to calculate (Quantity * Sell_price) and pass that to $line_total which will be saved in the same record row.

Only, I don't know how to access the 'SELECT'ed value prior to submitting the form... which kind of stinks when your trying to do something like this. :(

I hope that makes it a bit clearer. Sorry for the confusion. Thanks again.

-----------------------
SteedVLX

Posted: Wed Jul 16, 2003 7:40 pm
by steedvlx
Can anyone give me any clues how to do this with PhP?

Posted: Wed Jul 16, 2003 8:15 pm
by Gen-ik
If you want the <hidden> value to be updated before the form is even submitted you will need to use JavaScript, PHP is a server-side language.

There are two ways which you can do it. The first is to update the <hidden> value everytime a <select> menu is changed. The second way is to update the <hidden> value as the form is submitted.

I won't write all of the JS code for you but here are some examples...

This one changes the 'TOTAL' value when either of the <select> menus are changes..

Code: Select all

<head>
<script language="JavaScript">
function updateform()
&#123;
var form_name = "my_form";
var select_items_to_gather = new Array('selone','seltwo');
var this_total = 0;
for(var i=0; i<select_items_to_gather.length; i++)
&#123;
this_total = this_total + eval("document.forms."+form_name+"."+select_items_to_gather+".value");
&#125;
eval("document.forms."+form_name+".TOTAL.value=this_total");
&#125;
</head>

<body>

<form name="my_form" method="post" action="page.php">
<input type="hidden" name="TOTAL" value=0>
<select name="selone" onchange="updateform()">
<option value=5>5</option>
<option value=10>10</option>
</select>
<select name="seltwo" onchange="updateform()">
<option value=99>99</option>
<option value=999>999</option>
</select>
<input type="submit" value="submit">
</form>

</body>
...right, and this version will update the TOTAL value when the submit button is pressed but before the form gets sent to the page.php for processing...

Code: Select all

<head>
<script language="JavaScript">
function updateform()
&#123;
var form_name = "my_form";
var select_items_to_gather = new Array('selone','seltwo');
var this_total = 0;
for(var i=0; i<select_items_to_gather.length; i++)
&#123;
this_total = this_total + eval("document.forms."+form_name+"."+select_items_to_gather+".value");
&#125;
eval("document.forms."+form_name+".TOTAL.value=this_total");
&#125;
</head>

<body>

<form name="my_form" method="post" action="page.php" ONSUBMIT="updateform()">
<input type="hidden" name="TOTAL" value=0>
<select name="selone">
<option value=5>5</option>
<option value=10>10</option>
</select>
<select name="seltwo">
<option value=99>99</option>
<option value=999>999</option>
</select>
<input type="submit" value="submit">
</form>

</body>
...notice the changes to the <form> and <select> tags...

...hope this helps! :D

Posted: Wed Jul 16, 2003 8:56 pm
by steedvlx
Gen-ik,

Thanks for the fine examples. I keep running into things that require client-side (java script) when I know even less about it than I do PhP.

Unfortunately, other than a very basic recognition of what each script here does, I would have no idea how to adapt it to my situation. I will try to plug these in and figure them out, but I hold little hope in that working out for me.

At least I know now that it can't be done with PhP alone. Perhaps if I modify my strategy somewhat I can Just use an extra page/form reference to get the job done. I won't be nearly as clean though.

Thanks
---------------------------
SteedVLX

Posted: Wed Jul 16, 2003 9:11 pm
by Gen-ik
hehe.. no probs. Here's a quick run-through of the script.

Code: Select all

<script language="JavaScript"> 
function updateform() 
&#123; 

var form_name = "my_form"; 

/*
form_name is the name of the form you want to be updated.
Just remember to make sure that you set this variable to the name of your form otherwise you'll get JS errors.
*/

var select_items_to_gather = new Array('selone','seltwo'); 

/*
select_items_to_gather is an array of all of the <select> names that you want to change the TOTAL variable. If you look at the form you will see that the two <select> menus are named selone and seltwo.. I want these two menus to influence the TOTAL variable so I have included their names in the array.
*/

var this_total = 0; 

/*
this_total is just set-up to count the total values of the <select> menus in the select_items_to_gather array.
*/

for(var i=0; i<select_items_to_gather.length; i++) 
&#123; 
this_total = this_total + eval("document.forms."+form_name+"."+select_items_to_gather+".value"); 
&#125; 

/*
this will run through all of the <select> menus you have included in the select_items_to_gather array and add together all of their values.
*/

eval("document.forms."+form_name+".TOTAL.value=this_total"); 

/*
this simply updates the TOTAL value in the form with the this_total value.
*/

&#125;
</script>
Hope that helps you out a bit :D

Posted: Thu Jul 17, 2003 8:03 pm
by steedvlx
Gen-ik,

Thanks again. That certainly does clarify it for me. I will work with it today and let you know if I'm sucessfull.

-----------------------------
SteedVLX

Posted: Fri Jul 18, 2003 3:05 am
by steedvlx
Well, this is what I have so far... (Yeah, I know this is not really a PHP post any more)

I've modified the script to match my particular situation. I have one select field (quantity_sel) and another field called price which is populated from the database.

What I am trying to do is update the hidden field (cart_line_total) using the onsubmit="updateform()" in my form tag.

I'm not sure what the problem(s) is/are, but it doesn't update the value at all yet. I suspect my feeble attempt at re-building the equation is all wrong...

Code: Select all

<script language="JavaScript"> 
function updateform() 
&#123; 
var form_name = "update_cart"; 
var select_items_to_gather = new Array('quantity_sel','price'); 
var this_total = 0; 
for(var i=0; i<select_items_to_gather.length; i++) 
&#123; 
this_total = quantity_sel * price
/*this_total + eval("document.forms."+update_cart+"."+select_items_to_gather+".value"); 
*/
&#125; 
eval("document.forms."update_cart".cart_line_total.value=this_total"); 
&#125;
</script>
Also, the code for the form itself...

Code: Select all

<form name="update_cart" method="POST" action="<?php echo $editFormAction; ?>" Onsubmit="updateform()">
          <table width="790" height="103" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFF" id="cart_tbl">
            <tr bgcolor="#990000"> 
              <td width="92" height="30" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Part 
                Number</font></strong></td>
              <td width="278" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Description</font></strong></td>
              <td width="38" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Price 
                (each) </font></strong></td>
              <td width="25" align="center" valign="middle" bordercolor="#990000" bgcolor="#33CC00"><strong><font color="#FFFFFF" size="4" face="Arial, Helvetica, sans-serif">QTY</font></strong></td>
              <td width="79" align="center" valign="middle" bordercolor="#990000"><strong><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif">Total 
                (Yen) </font></strong></td>
              <td align="center" valign="middle" bordercolor="#990000" bgcolor="#990000">&nbsp;</td>
            </tr>
            <tr align="center" valign="middle"> 
              <td width="92" height="71" bordercolor="#990000" bgcolor="#CCCCCC"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row_rs_cart_edit&#1111;'cart_kb_prod_part_num']; ?> </font></td>
              <td width="278" align="left" valign="middle" bordercolor="#990000"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row_rs_cart_edit&#1111;'prod_e_desc']; ?> </font></td>
              <td bordercolor="#990000" bgcolor="#CCCCCC"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"> 
                <input name="price" type="text" id="price" value="<?php echo $row_rs_plu&#1111;'prod_sell_price_jpy']; ?>" size="6" maxlength="6">
                <strong>JPY </strong></font></td>
              <td width="25" bordercolor="#990000" bgcolor="#33CC00"> <center>
                  <font color="#000000" size="2" face="Arial, Helvetica, sans-serif"></font> 
                  <font color="#000000" size="2" face="Arial, Helvetica, sans-serif"> 
                  <select name="quantity_sel" id="select">
                    <option value="1" <?php if (!(strcmp(1, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>1</option>
                    <option value="2" <?php if (!(strcmp(2, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>2</option>
                    <option value="3" <?php if (!(strcmp(3, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>3</option>
                    <option value="4" <?php if (!(strcmp(4, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>4</option>
                    <option value="5" <?php if (!(strcmp(5, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>5</option>
                    <option value="6" <?php if (!(strcmp(6, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>6</option>
                    <option value="7" <?php if (!(strcmp(7, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>7</option>
                    <option value="8" <?php if (!(strcmp(8, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>8</option>
                    <option value="9" <?php if (!(strcmp(9, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>9</option>
                    <option value="10" <?php if (!(strcmp(10, $row_rs_cart_edit&#1111;'cart_quantity']))) &#123;echo "SELECTED";&#125; ?>>10</option>
                  </select>
                  </font> </center></td>
              <td bordercolor="#990000" bgcolor="#CCCCCC"> <center>
                  <font color="#009900" size="2" face="Arial, Helvetica, sans-serif"><strong> 
                  <?php echo $row_rs_cart_edit&#1111;'cart_line_total']; ?> <font color="#000000"> JPY</font></strong></font> 
                </center></td>
              <td width="40" align="center" valign="middle" nowrap bordercolor="#990000" bgcolor="#FFFFFF"> 
                <center>
                  <input name="cart_line_total" type="hidden" value="">
                  <input name="cart_id_num" type="hidden" value="<?php echo $row_rs_cart_edit&#1111;'cart_id_num']; ?>">
                  <input name="Submit" type="image" value="Update Cart" src="../images/e_update_cart.gif" align= "middle" width="105" height="48">
                </center></td>
            </tr>
          </table>
          <input type="hidden" name="MM_update" value="update_cart">
        </form>
If you don't mind, I think it will work if someone would let me know what the correct syntax is.

BTW; I finally broke down and ordered a Javascript book. Amazon says it will be here (Japan) in about three weeks...argh!

--------------------------
SteedVLX

Posted: Fri Jul 18, 2003 7:41 am
by Gen-ik
Here's the most obvious problem that stands out at me...

You've managed to kill the JavaScript :D

Try this version.......

Code: Select all

<script language="JavaScript"> 
function updateform() 
&#123; 
var form_name = "update_cart"; 
var select_items_to_gather = new Array('quantity_sel','price'); 
var this_total = 0; 

for(var i=0; i<select_items_to_gather.length; i++) 
&#123; 
this_total = this_total + (eval("document.forms."+form_name+"."+select_items_to_gather+".value") * eval("document.forms."+form_name+".price.value"));
&#125; 

eval("document.forms."+form_name+".cart_line_total.value=this_total");
&#125; 
</script>
You only need to enter the name of the form at the start of the script.... don't replace the form_name code beyond the "for(var i=0; " part of the script else it won't work.

Posted: Sat Jul 19, 2003 12:39 pm
by steedvlx
It took me quite a while of tinkering to come up with the following change that allowed the entire thing to work beautifully!

THANK YOU Gen-ik!

I had to change

Code: Select all

var select_items_to_gather = new Array('quantity_sel','price');
to

Code: Select all

var select_items_to_gather = new Array('quantity_sel');
And the correct totals lit up like a X-mas tree. I got no explanations as to why, but....

I just can't tell ya how good that makes me feel!

-------------------------
SteedVLX

Posted: Sat Jul 19, 2003 12:54 pm
by Gen-ik
Glad to be of service :D

Posted: Sat Jul 19, 2003 1:04 pm
by steedvlx
"...of service"

definite understatement from my perspective. :D

----------------------
SteedVLX