I'm trying to insert multiple values using radio, here is my example:
<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">
this one work if I insert single input, but if I want to insert more than one for example:
Size: small, medium, large <- name="toppingPrice[]" for all input values
Cheese: yes, no <- name="toppingPrice[]" for all input values
Level: spicy, normal <- name="toppingPrice[]" for all input values
this will not work because it will merge into 1 group so if I have to choose only one of all toppings.
my original code looks like:
foreach ($toppingPrice as $key) {
list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
$tName[] = $toppingName;
$tNameEn[] = $toppingNameEn;
$tPrice += $toppingPrice;
}
$tn = implode(",", $tName);
$tn_en = implode(",", $tNameEn);
$price = $price + $tPrice;
Html:
<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>
I hope I delivered the question in the right way
please give me any idea or solution for fix this issue
You should use name attribute as shown in below code.
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3
<br>
<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6
<br>
<input type="radio" name="toppingPrice.level[]" value="7"> 7
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9
<button type="submit" value="save" /> Save
</form>
This will be your $_POST after form submit
Array
(
[toppingPrice_size] => Array
(
[0] => 1
)
[toppingPrice_toping] => Array
(
[0] => 5
)
[toppingPrice_level] => Array
(
[0] => 9
)
)
Related
I have a list of checkboxes and if checked their values are posted to json file which is working fine. When user uncheck all of them I get NULL value in array and if I set its value="" its returns depends :[""]. what I want is if user did not check any checkbox it returns depends:[] only neither NULL nor "".
Here is my code:
<label for="depends">Linked With</label></br>
<input type="checkbox" name="depends[]" value="1" id="ch2" > 1
<input type="checkbox" name="depends[]" value="2" id="ch3" > 2
<input type="checkbox" name="depends[]" value="3" id="ch5" > 3
<input type="checkbox" name="depends[]" value="4" id="ch6" > 4
<input type="checkbox" name="depends[]" value="5" id="ch7" > 5
<input type="checkbox" name="depends[]" value="6" id="ch8" > 6
<input type="checkbox" name="depends[]" value="7" id="ch9" > 7
in php code I am calling it as
'depends'=>$_POST['depends']
in your php code you can handle it like that :
<?php
/* if $_POST['depends'] is not empty then you store the value, else you create an empty array [] */
$var = isset($_POST['depends']) ? $_POST['depends'] : array(); /* you can replace $var */
print_r($var);
?>
I have multiple checkbox groups, and once the form is submitted, I want each group of checkboxes that were selected to be added to their own variable.
This is the form:
<form action="" method="get">
<p>apple <input type="checkbox" value="apple" name="fruits[]" /></p>
<p>orange <input type="checkbox" value="orange" name="fruits[]" /></p>
<p>peach <input type="checkbox" value="peach" name="fruits[]" /></p>
<br>
<p>red <input type="checkbox" value="red" name="colors[]" /></p>
<p>green <input type="checkbox" value="green" name="colors[]" /></p>
<p>blue <input type="checkbox" value="blue" name="colors[]" /></p>
<br>
<p>chicken <input type="checkbox" value="chicken" name="meats[]" /></p>
<p>pork <input type="checkbox" value="pork" name="meats[]" /></p>
<p>lamb <input type="checkbox" value="lamb" name="meats[]" /></p>
<button>submit</button>
</form>
And this is my code:
$string = 'fruits,colors,meats';
$str_array = explode(',', $string);
foreach ($str_array as $value) {
if (isset($_GET[$value])) {
$group_name = $_GET[$value];
foreach ($group_name as $group_item) {
$group_string .= ' ' . $group_item;
}
}
}
echo $group_string;
With that code, if I choose for example the first checkbox in each group and hit submit, I will get the following value of $group_string = apple red chicken in one string.
What I get does make sense to me as per the code I wrote, but what I want is for each option group to have its own variable to which its values are asigned, so what I want is to get is the following (assuming I again chose the first option from each group):
$fruits = 'apple';
$colors = 'red';
$meats = 'chicken';
But I don't know how to rewrite it so I get that. Also, the number of options groups is not known upfront, it has to happen dynamically.
Ok, I took the liberty of rewriting part of your php for my convenience but here it is
your new and improved php file
<?php
// assume we know beforehand what we are looking for
$groups = explode(',','fruits,colors,meats');
foreach ($groups as $group) {
if (isset($_GET[$group])) {
$vv = array();
foreach ($_GET[$group] as $item) $vv[] = $item;
$$group = implode(' ',$vv);
}
}
var_dump($fruits,$colors,$meats);
?>
I used a construct in PHP called variable variables. This is actually an almost identical answer as the one #Lohardt gave. I hope this can help you out. If it doesn't then post me a comment
You could do something like:
<input type="checkbox" value="apple" name="groups[fruits][]" />
<input type="checkbox" value="apple" name="groups[colors][]" />
<input type="checkbox" value="apple" name="groups[meats][]" />
Your $_POST will look like this:
Array
(
[groups] => Array
(
[fruits] => Array
(
[0] => apple
)
[colors] => Array
(
[0] => red
)
)
)
And it should be simple to use a foreach loop to get the keys and values.
Edit: then you can assign the value to variables like this:
${$key} = $value;
and use it you would do any variable:
echo $color;
I am trying to set up a voting process where there are 20 sets of two radio buttons you can select from. Then when a "vote" button is clicked I can collect the values of the selected buttons and add +1 to their respective database cells.
I am new to MySql and am struggling...
The database Table looks like..
ID Park Votes
1 Zion 0
2 Grand Canyon 0
3 Arches 0
4 Canyonlands 0
5 Yosemite 0
6 Yellowstone 0
The HTML form
<form action="/databases/save.php" method="post">
<input type="radio" name="match1" value="1" />Zion
<input type="radio" name="match1" value="1" />Grand Canyon
<input type="radio" name="match2" value="1" />Arches
<input type="radio" name="match2" value="1" />Canyonlands
<input type="radio" name="match3" value="1" />Yosemite
<input type="radio" name="match3" value="1" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
And the save.php file
<?php
if (isset($_POST['match1'])){
$match1 = $_POST['match1'];
mysql_query("INSERT INTO results () VALUES ('1')");
}
?>
I am getting thrown off by how to get the selected radio and apply a +1 to the correct table cell. Thanks in advance
Your HTML is incorrect for radio inputs. If you want the user to select only one park, all inputs must have the same name. Use the value attribute of the radio input to store the ID of the park (from the MySQL table):
<form action="/databases/save.php" method="post">
<input type="radio" name="match" value="1" />Zion
<input type="radio" name="match" value="2" />Grand Canyon
<input type="radio" name="match" value="3" />Arches
<input type="radio" name="match" value="4" />Canyonlands
<input type="radio" name="match" value="5" />Yosemite
<input type="radio" name="match" value="6" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
The selected value for that input will be returned as $_POST['match'] if one item was selected or else $_POST['match'] will not be set at all.
So on your save.php page you can update your table like this (assuming your table is named "parks_table"):
<?php
if (isset($_POST['match'])) {
mysql_query( 'UPDATE `parks_table` SET Votes = Votes + 1 WHERE ID = '.$_POST['match'] );
}
?>
Since you seem to be starting with PHP and MySQL please read: Why shouldn't I use mysql_* functions in PHP?
<button type="submit" name="vote" id="vote" value=" ' . $row['candidate_position'] . ' " class="btn btn-success">CAST VOTE</button>
<?php
$vote = $_POST['vote'];
if (isset($_POST['vote'])) {
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
echo "string";
}
mysqli_close($con);
?>
Retriving the roles of user using user id
$userId = $userDetails['user_id'];
$stmt = $db->query ( "SELECT user_role_id FROM user_role_xref where user_id=" . $userId);
$userRoles = $stmt->fetchAll ();
print_r($userRoles);
When i print this
Array (
[0] => Array (
[user_role_id] => 3
)
[1] => Array (
[user_role_id] => 4
)
[2] => Array (
[user_role_id] => 5 )
)
how to show check box as checked when value matches else checkbox is unchecked
<input type="checkbox" id="roles_1" value="3" name="roles[]" />
<label for="roles_1">Role1</label><br />
<input type="checkbox" id="roles_2" value="4" name="roles[]" />
<label for="roles_2">Role2</label><br />
<input type="checkbox" id="roles_3" value="5" name="roles[]" />
<label for="roles_3">Role3</label><br/>
HTML attribute checked="checked".
E.g:
<input type="checkbox" <?php if ($var) echo ' checked="checked"'; ?> id="roles_2" value="4" name="roles[]" />
Use in_array function of php
Check if the value exists or not
If it exists then set checked property otherwise display unchecked.
<input type="checkbox" <?php if(in_array("roles_1",$roles_array){ echo 'checked="checked"';} ?> id="roles_1" value="1" name="roles[]" />
I have a form with many checkboxes.
ex.
...
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
...
I want to have all the checkboxes in the array. Array 'dodatkowe'.
When i checked all checkboxes have:
Array ( [0] => 1 [1] => 1 [2] => 1 )
but when i checked example only second I have:
Array ( [0] => 1 )
I need that, when i check example second checkbox:
Array ( [0] => 0 [1] => 1 [2] => 0)
give them indexes so you can reference them specifically...
...
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
<input name="dodatkowe[3]" type="checkbox" value="1" />
...
Not sure why you feel you need to see the unchecked values, this can be assumed to be the inverse of the checked values.... Any attempt to do this is a hack, and is unnecessary.
If a checkbox isn't checked it won't include it's value into the parameters but the first step would be to give the checkboxes a unique id:
<input name="dodatkowe[0]" type="checkbox" value="1" />
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
Then you can use PHP to check is the value is there:
$maxfields = 3;
$selectboxes = $_REQUEST['dodatkowe'];
for($i = 0; $i < $maxfields; $i++)
if(!isset($selectboxes[$i])) $selectboxes[$i] = 0;
This will set all non existent fields to 0 and $selectboxes should contain the result you are looking for.