Jump to content

Recommended Posts

So, i need this.

 

I have a input button and, lets say, 2 checkboxes in a form.

 

Form is about ordering items. 

 

Button -  value-10  "Modern Pen"

Checkboxes -   value-300 (checkbox1) label-"Galaxy S3"

and   value-350 (checkbox2) label-"Galaxy S4"

 

When user makes calculation my script need to make the job and say:

 

"Dear user you have selected Modern Pen and Galaxy S4 and it costs 360€."

 

I have this code, and it tells Sum of all selected items (this 360€), but I cant print labels of forms. Can someone tell me the easiest way (with example) of how to use, lets say value="10,ModernPen" in one input button/checkbox or any other way of making it do what i want?

 

 

 

What can I do with this code to do what I need? This works, now  I need improvement.

 



<form action='order.php' method='post'>
<label>
<input type='radio' name='Pen' value='10' id='Pen' />
Modern Pen</label>

<label>
<input type="checkbox" name="Phone[]" value="300" id="Phone" />
Galaxy S3</label>

<label>
<input type="checkbox" name="Phone[]" value="350" id="Phone" />
Galaxy S4</label>

<input type='submit' name='Submit' value="Calculate" />
</form>


 



<?php

$pen = $_POST['Pen'];

if (!empty($_POST['Phone'])) {
$Phone = 0;
foreach ($_POST['Phone'] as $value) {
$Phone += $value;

}
}

$sum = $pen + $Phone;


echo "You Ordered items cost is:<br />";
echo "<div style='clear:both'></div><div class='style'><h3> $sum €</h3></div>";

?>



Please? Any help?

Sorry .  Don't know what you really want.  Try again?

 

Perhaps you need to create array to hold your 'value' possibilities as keys and a sub-array with the desc and cost and then in your php you take the phone[] element and grab it out of the array to get your info.?

Edited by ginerjm

This isn't how this is normally done.  You never want to have the raw prices in the form. This would allow a user to change the price by manipulating the HTML on the page by using many available browser tools. I could easily change the 350 price for the Galaxy S4 to 1 and submit the form. You'd lose money and I'd get a cheap new phone. Generally, you'd have a db table for products. Something like:

id | description | price (and probably more like category ID for things like "phones", "office supplies", "tablets", etc.

like:

1 | Pen | 10

2 | Galaxy S3 | 300

3 | Galaxy S4 | 350

etc.

 

When generating your form, you'd query the db and get the values and display them on your form. Something like:

<label><input type="checkbox" name="product[]" id="1">Pen</label>

<label><input type="checkbox" name="product[]" id="2">Galaxy S3</label>

<label><input type="checkbox" name="product[]" id="3">Galaxy S4</label>

 

Then when you submit your form, you'd get the $_POST['product'], which will be an array of the product ID's that they made purchases for.

Say they purchsed a pen and the galaxy s3.

$products = $_POST['product']; //products = array(1, 2)

 

Using those ID's, you'd again select those items from the DB but only getting the rows where the ID's are the $products

SELECT * FROM products WHERE id IN(1,2);

which gives you the Pen and Galaxy S3, their price and description.

You can now calculate the total, and show the description of each item to the user on the checkout page because you just retrieved those items from the db using only their product ID.

 

It's obviously a bit more complex than that, but hopefully this gives you a better (and more secure) direction to go in.

Edited by CroNiX
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.