Jump to content

Recommended Posts

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

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

 

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

 

 

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

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

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.