Gossamer Forum
Home : General : Perl Programming :

Changing a second field automatically?

Quote Reply
Changing a second field automatically?
Hi,

i do have the following in a simple form for a formmailer:

<input type="radio" value="product I" name="product">
<input type="radio" value="product II" name="product">
<input type="radio" value="product III" name="product">

Additionally i'd like to add hidden fields for prices of those products:

<input type="hidden" name="product I" value="100.00">
<input type="hidden" name="product II" value="200.00">
<input type="hidden" name="product III" value="300.00">

The user chooses a product and the script should add the right price to a variable called "product_price".

Is there any chance to control what product the user chooses and then automatically choosing the right price?

Would it be possible to create a small perl routine what checks if those hidden fields are existing and adding the right price to the choosen product (after the user sent the form)?

Thanks for your hints!


Quote Reply
Re: Changing a second field automatically? In reply to
I get the jist of what you are saying but I am still a little unsure about a few things such as what are you going to do with $product_price and how do you want to show it?...You could add the price using javascript to either a hidden or non-hidden but disbled text field and then pass it to a perl script?

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Changing a second field automatically? In reply to
Hi Paul,

sorry for my scrambled question!

Next try: The user should choose a product and the var $product_price should be changed to the price of the choosen product. The content of $product_price should be shown on the next page (like a bill).

My problem is, that i want to show both values on the next page - like:

product: (here goes $product)
price: (here goes $product_price)



Quote Reply
Re: Changing a second field automatically? In reply to
You could have a hidden field like:

<input type="hidden" name="product_price">

Then next to each radio button you have for the products add..

onclick="doprice();">

Change the name for each button...eg

onclick="doprice();">
onclick="doprice1();">
onclick="doprice2();">

-I'm not great with javascript so I don't know the "best" way to do it.
-The best way is probably something like;

if (document.FORMNAME.product1.selected) {
document.FORMNAME.product_price.value = "$15"
}

...or something like that but my way should still work (I THINK).

Then in the head put...

Code:
<script language="javascript">
function doprice() {
document.FORMNAME.product_price.value = "$15"
}
function doprice1() {
document.FORMNAME.product_price.value = "$20"
}
function doprice2() {
document.FORMNAME.product_price.value = "$30"
}
</script>


So then the product_price value will depend on which radio they click.

Then in your perl script you'd need to assign the product_price field to the $product_price variable the same way you are passing the rest of the fields and then just put $product_price on the next page.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Changing a second field automatically? In reply to
Do away with the HIDDEN fields, then create a HASH like the following:

Code:

%product_price = (
Product1 => '100.00',
Product2 => '200.00',
Product3 => '300.00',
Product4 => '400.00'
);


Then add a loop statement in the success subroutine in your form, like the following:

Code:

foreach $key (keys %{$product_price}}) {
$in->param(-name => $key, -value => $product_price->{
$key});
}


Then in the success sub, add the following codes:

Code:

Product: $in{'Product'}
Price: $value


These codes definitely need tweaking, but I think you get the idea.

Also, since you use MySQL, I believe, you could easily create a PRODUCT_PRICE table, then define the table as a DBI object in the SUCCESS subroutine and then get the product price based on the inputted PRODUCT via your form.

EDIT: Paul's javascript suggestion would also work, but I usually prefer a server-side solution.

Regards,

Eliot Lee
Quote Reply
Re: Changing a second field automatically? In reply to
You see, I told you my way was pants! hehe..

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Changing a second field automatically? In reply to
Ehm .. any idea how to solve this, if the script get's the amount of products and prices told by fields of the form?

For example: The form could contain one product and one price, or 10 products and 10 prices ... the script should see this ...


EDIT: Sorry - no MySQL!
Quote Reply
Re: Changing a second field automatically? In reply to
Are you trying to create a shopping cart or something?

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/