Jump to content

value of arrays in form (checkboxes)


TenTillNoon

Recommended Posts

I've created a single file PHP form with radio buttons, check boxes and arrays. It was working until i tried to print the results! LOL Can anyone help me figure out what went wrong and help me figure out how to add the total of the options the user has chosen? As you can see, I want a table in the return that shows the name of the item on the left, the dollar amount on the right and at the very bottom the total of the whole order...any help would be great! Thanks in advance...  ;D

 

<body>
<?php
if (isset($_POST['extras']) AND is_array($_POST['extras']) && (isset($_POST['color'])) {

foreach($_POST['extras'] as $key=>$value)
echo "<table border=\"2\">";
echo " <tr> <td>item</td> <td>cost</td> </tr>";
echp " <tr> <td>".$color."</td><td>".$(how do i get cost of color)."</td> </tr>";
echo "<tr>";
    echo "<td>".$value."</td><td>".$(how do i get cost of value)."</td>";
echo "</tr>";
echo " <tr> <td>Your total is:</td> <td>".$(how do i get total cost)."</td> </tr>";
echo "</table>";
?>

<form action='' method='post'>
    <table border="1">
  <tr>
    <td colspan="2" align="center" scope="col">Select paint kit</td>
  </tr>
  <tr>
    <td><input type="radio" name="color" value="red" />Red $15.99</td>
    <td><input type="radio" name="color" value="blue" />Blue $18.00</td>
  </tr>
  <tr>
    <td><input type="radio" name="color" value="yellow" />Yellow $19.99</td>
    <td><input type="radio" name="color" value="green" />Green $20.00</td>
  </tr>
</table>
<br>

  <table border="1">
  <tr>
    <td colspan="2" scope="col">Pick your extras ($3.00 ea.)</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="extras[]" value="brush" />Brush</td>
    <td><input type="checkbox" name="extras[]" value="palette" />Palette</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="extras[]" value="primer" />Primer</td>
    <td><input type="checkbox" name="extras[]" value="glaze" />Glaze</td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" name="submit" value="Submit Your Order" /></td>
  </tr>
</table>
</form>
</body>

 

 

Link to comment
Share on other sites

Man, that's some messy code...

 

well, to start, you've opened an if statement and haven't closed it...

 

you've also opened a foreach loop and haven't closed it.

 

also, you cannot get the costs of each color, because you're not posting them. You're only posting the color

 

and... in this code:

if (isset($_POST['extras']) AND is_array($_POST['extras']) && (isset($_POST['color'])) {

 

why are you using AND and && ?

Link to comment
Share on other sites

to answer your questions/concerns:

 

- I'm not seeing where they should be closed/ i thought they were?

 

- When i've listed the cost of each item as the value, it didn't work as a numerical value to add together...any idea what i need to do to add the values that were selected together??

 

- And finally (or should I say &&, lol) I have no idea why i did that... my lack of php skills is very upsetting to me, and I'm really trying to learn something (what a concept!). When I see finished code I can work my way back and usually figure it out/ adapt and change it to what I need...but when it comes to writing my own code I am not good. Any more suggestions would be welcomed with open arms!

Link to comment
Share on other sites

you can try something like:

 

instead of this:

<input type="radio" name="color" value="yellow" />Yellow $19.99</td>

add the price to the value that gets posted:

<input type="radio" name="color" value="yellow 19.99" />Yellow $19.99</td>

...

when it's posted, grab the value and split it where the space is:

$color_and_value = explode(" ",$_POST['color']);
$selectedColor = $color_and_value[0];
$selectedPrice = $color_and_value[1];

Link to comment
Share on other sites

...

add the price to the value that gets posted:

<input type="radio" name="color" value="yellow 19.99" />Yellow $19.99</td>

...

 

 

Keep in mind that going this route makes it fairly easy for someone to tamper with the prices. I would recommend storing that information in a database or an array instead. You could then use the database/array to create the form and the script to process the form.

Link to comment
Share on other sites

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.