Forum Moderators: coopster

Message Too Old, No Replies

Manipulating information using php

Manipulating information using php

         

Imy_S3

2:42 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Hi

I have the following code:

echo ( '<p>Child tickets</p>' );
echo ( '<SELECT NAME="childPrices">' );

echo ( '<option value="1">1</option>' );
echo ( '<option value="2">2</option>' );
echo ( '<option value="3">3</option>' );
echo ( '<option value="4">4</option>' );
echo ( '<option value="5">5</option>' );

echo ( '</SELECT>' );

Say for example the usere selects option 5 from the list.

How do i do it so that as soon as the user selects 5 it multiplies it by 10 and gives the result in a box next to it.

here is rest of code:

<snip>

[edited by: jatar_k at 5:33 pm (utc) on Mar. 4, 2004]
[edit reason] snipped massive code dump [/edit]

Timotheos

4:13 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do i do it so that as soon as the user selects 5 it multiplies it by 10 and gives the result in a box next to it.

If you're using PHP then you'll have to wait until the value is submitted but if you want it "as soon as the user selects" then it's a javascript solution (of which I know nothing).

Imy_S3

4:18 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



ok, ill do it so when the user submits.

But still, how do i do this?

andrewB

4:23 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



This is simple javascript
say your box next to the select is
<input type="text" name="result">

Then add this to your select

<SELECT NAME="childPrices" onChange="myFunction()">

Then write your javascript function as follows

function myFunction() {
f = document.forms[0];
f.result.value = f.childPrices.value * 10;
}

And thats it!

Imy_S3

3:17 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



your soultion does not work.

errors on follwing lines:

f = document.forms[0];
f.result.value = f.childPrices.value * 10;

Hope you can help

coopster

7:38 pm on Mar 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you still want to persue the javascript solution, you'll probably want to post your question in that forum. However, to get the desired result using PHP you need to get the form variable value and perform your calculation.
$newPrice = (isset($_POST['childPrices']))? $_POST['childPrices'] * 10 : 0;

Imy_S3

8:52 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



the line you have just written in a simpler way, using if else (i think).

Can you do this as the way you have done it i dont really understand. cheers.

coopster

8:57 pm on Mar 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, but you should get friendly with ternary [php.net] operations, they are quite handy and very easy to understand once you start using them.
if (isset($_POST['childPrices'])) { 
$newPrice = $_POST['childPrices'] * 10;
} else {
$newPrice = 0;
}