0
votes

I've this simple form, when the price is set to 120 $:

  <input type="checkbox" name="addon[book]" value="120" /> book
  <input type="checkbox" name="addon[plane]" value="420" /> Plane

Could I send two values (USD & €) with thr form?

EDIT:

I added name="addon[book]" because I'll display the name too

EDIT 2:

PHP:

foreach($addon as $name => $value) {
    echo 'your addons: '.$name;
}
$total = array_sum(array_map("intval", $_POST["addon"]));
echo 'the total '.$total.' $'; 
1
What do you want to do? Why not compute the value at the server side?Felix Kling
@Felix Kling I want to display two currency @Pushpendra OKCheerio
What if someone changed the price in html and sent 12 instead of 120? Will the book be sold for 12E price?zerkms
Are you aware that anybody can change the value you send with tools like Firebug? You don't want this to happen if this value is a price, do you?greg0ire
Why do you use input elements for displaying? Please be more specific.Felix Kling

1 Answers

5
votes
<input type="hidden" name="addon[book_USD]" value="250" />
<input type="checkbox" name="addon[book_EUR]" value="120" /> book

However NEVER trust the input from the form. Check the price in the backend code or users can change the price!

EDIT

What about json encoding?

<input type="checkbox" name="addon[book]" value="{&quot;USD&quot;:250,&quot;EUR&quot;:120}" /> book

EDIT2

To explode the values you'll have to use json_decode()

EDIT3

As others (and myself) already stated what you are trying to do (at least from the looks of it) is that you use the price from the input field as the price the user is going to pay.

That is not the best thing you can do, cause users can easily change the price of the input field.

I don't know your code but somehting like the following would be much better:

<input type="checkbox" name="addon[book]" value="book1">

And in your PHP code:

$prices = array('book1'=>array('USD'=>250, 'EUR'=>120),
                'book2'=>array('USD'=>120, 'EUR'=>60),
);