0

I am offering a series of options which attract price additions

eg Bevelled Glass +£20, Mirror Glass +£10, Antique Effect +£20

So I have two array variables $option_description and $option_price and

I am making a <select> element with the options thus;

<select>
 <option value='0'>Please select...</option>
 <option value='<?=$option_price[0]; ?>'><?=$option_description[0]; ?> +£<?=$option_price[0]; ?></option>
 ...
</select>

What I need to do, after the user has made their selection, is pass the two variables separately in the $_POST[] variable. The price will be added at submission to the price field and the description needs to be in the order as an instruction.

How do I handle my user choice given that the value of $option_price is not unique?

Chris Pink
  • 1,558
  • 1
  • 15
  • 28
  • `base64_encode` your description and put it into `value` attribute. – u_mulder Aug 09 '16 at 12:26
  • 1
    I think you need to use bit javascript here. Maybe something like this help: [link](http://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values) – Aleksandar Varicak Aug 09 '16 at 12:28
  • Probably a bad idea to use the price as a form field *if* you're going to rely on that value being accurate in the back-end... it's pretty trivial to change it from the front-end (e.g. Tamper). Better off just having a unique id for the option and use that; propagate the price on the server. – CD001 Aug 09 '16 at 13:15

3 Answers3

1

I think you're running into trouble using the price as the value for each option. As you have the same price for multiple options you may end up not knowing which option actually was selected.

Why don't you create an array, that has unique keys and contains the price and name of each option. Something like:

$options = [
    'beveiled' => ['price' => 20, 'name' => 'Bevelled Glass'],
    'antique' => ['price' => 20, 'name' => 'Antique Glass'],
];

Then in your template:

<?php foreach($options as $key => $value) { ?>
    <option value='<?=$key; ?>'><?=$value['name']; ?> +£<?=$value['price]; ?></option>
<?php } ?>

Finally you can check using array_key_exists whether an option was selected by the user.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
0

You need to create an id for each option and do the price lookup server-side on form post. Otherwise, you're letting the customer set their own price if they use firebug or postman or whatever to post to your form submission page. You can doubly store the price information on the page to update the displayed total for the customer without an extra round trip to the server, but don't trust or rely on user submitted price.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
0

id | price |description |userid

1 | £20 |Bevelled Glass |2

2 | £10 |Mirror Glass |2

You can make another table for insert details of product description and price and using userid you will get information for particular user with description of product and price

Swapnil Mahadik
  • 640
  • 5
  • 3