jammer Posted April 3, 2008 Share Posted April 3, 2008 Hi i was wondering is there any way to store multiple variables to a value with a checkbox. I know you can store more then 1 but is there anyway to have them separate like value"1", value"2" and do on. <input type="checkbox" name="chkbox" value="<?= $product['price'] ?><?= $product['description'] ?>"> i can store these two but say if i want to echo "chkbox" on a separate page it prints both out automatically, is there anyway i can print price and then be able to print the description later? thanks Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/ Share on other sites More sharing options...
craygo Posted April 3, 2008 Share Posted April 3, 2008 you can try doing something like this <input type="checkbox" name="chkbox[$price]" value="<?= $product['description'] ?>"> now it should be in an array print_r($_GET['chkbox']); will give you your values Ray Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508703 Share on other sites More sharing options...
laffin Posted April 3, 2008 Share Posted April 3, 2008 Checkbox shudn display any text, just a check box. so the value, can be the product id code instead of the product price. on the processing page u can than pull both price/description from the db from the id. Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508718 Share on other sites More sharing options...
jammer Posted April 3, 2008 Author Share Posted April 3, 2008 hey ray if i do, <input type="checkbox" name="chkbox" value="<?= $product['price'] ?><?= $product['description'] ?>"> and then print_r($_GET["chkbox"]); it will print out both values, however it is both values at the same time for example: 3.99 Terdsandwich now say i want to print 3.99 and then print "is ridiculous for a " Terdsandwich but there is no way for me to add anything between 3 and sandwich it just automatically does price description and your way just prints out Array([$price] => terdsandwich Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508727 Share on other sites More sharing options...
jammer Posted April 3, 2008 Author Share Posted April 3, 2008 laffin i don't want the checkbox to really display text maybe i am confused but i want the checkbox value to store price and id i already found the price and description from the DB in <html> <head> <title>Show Products</title> <style> @import url(my.css); </style> </head> <body> <? require_once "model/Product.php"; $all = Product::findAll(); ?> <? require_once "nav.php" ?> <h3>the products</h3> <form action ="addTo-cart.php"> <table border="1" cellpadding="5px"> <tr> <th>id</th> <th>description</th> <th>price</th> </tr> <? foreach ($all as $product): ?> <tr> <td><?= $product['id'] ?></td> <td><?= htmlspecialchars($product['description']) ?></td> <td><?= '$' . $product['price'] ?></td> <td> <input type="checkbox" name="chkbox[$price]" value="<?= $product['description'] ?>"> <input type="submit" name="addTo_cart" value="Add to Cart"/> </td> </tr> <? endforeach ?> </table> </form> where in the for each loop it finds the price and description from the DB and displays the products the checkbox is to buy the item(add it to the cart) so i want to save both of these values for that specific item any idea Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508739 Share on other sites More sharing options...
craygo Posted April 3, 2008 Share Posted April 3, 2008 You would replace $price with something from a database or the actual value. try this <html> <head> <title>Show Products</title> <style> @import url(my.css); </style> </head> <body> <? require_once "model/Product.php"; $all = Product::findAll(); ?> <? require_once "nav.php" ?> <h3>the products</h3> <form action ="addTo-cart.php"> <table border="1" cellpadding="5px"> <tr> <th>id</th> <th>description</th> <th>price</th> </tr> <? foreach ($all as $product): ?> <tr> <td><?= $product['id'] ?></td> <td><?= htmlspecialchars($product['description']) ?></td> <td><?= '$' . $product['price'] ?></td> <td> <input type="checkbox" name="chkbox[<?= $product['id'] ?>]" value="<?= $product['description'] ?>"> <input type="submit" name="addTo_cart" value="Add to Cart"/> </td> </tr> <? endforeach ?> </table> </form> now the key of the chkbox array hold your id and the value is the price. A simple loop will show. forech($_GET['chkbox'] as $id => $price){ echo "ID = $id and price = $price"; } Ray Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508746 Share on other sites More sharing options...
jammer Posted April 3, 2008 Author Share Posted April 3, 2008 it works thanks ray Quote Link to comment https://forums.phpfreaks.com/topic/99412-store-multiple-variables-with-a-selection-form-checkbox/#findComment-508774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.