Get name and value with PHP
I have this simple form:
HTML:
<input type="checkbox" name="product[]" value="20$" /> book
<input type="checkbox" name="product[]" value="30$" /> plane
PHP:
$product = $_POST['product'];
foreach($product as $value) {
echo $value;
}
Note:
The user can choose one or two ... products.Question:
I want to know if there is a solution to get thename
too, like adding an ID class or something ..
Basically I cant get the name
at开发者_Go百科tribute because I didn't sent it with the form.
Add the missing information to your POST-parameters:
<input type="checkbox" name="product[book]" value="20$" /> book
<input type="checkbox" name="product[plane]" value="30$" /> plane
You can iterate over it like this:
foreach ($_POST['product'] as $name => $value) {
// ...
}
You can use the option presented by elusive, but it seems that in this case you're passing the price from the client to the server. That might allow people to fraud. They could with only a little hacking send another (lower) price. If you just send 'book' and redetermine the price on the server, this won't be a problem.
If you choose that path, you can just put the product ('book') in the value:
<input type="checkbox" id="book" name="product[]" value="book" />
<label for="book">book</label>
<input type="checkbox" id="plane" name="product[]" value="plane" />
<label for="plane">plane</label>
精彩评论