Given this code:
<input type="checkbox" id="Coke" name="Price" value="70" />
<input type="checkbox" id="Fanta" name="Price" value="70" />
<input type="checkbox" id="Sprite" name="Price" value="70" />
I would like to know how, if my user selects Fanta checkbox, I want my php variable $type="Fanta" but I need form checkbox VALUES to stay NUMERIC for total price calculation.
It's not clear what exactly you're asking, but I believe you basically want 2 fields, one that defines the price and one that defines the selected type.
In that case, your best bet would be to store the prices server-side (That way people can't modify them too, which is good!). If you do this, your checkboxes will look like this:
<input type="checkbox" id="Coke" name="type[]" value="Coke" />
<input type="checkbox" id="Fanta" name="type[]" value="Fanta" />
<input type="checkbox" id="Sprite" name="type[]" value="Sprite" />
Your backend code would look like this:
$prices = array(
'Coke' => 70,
'Fanta' => 70,
'Sprite' => 70
);
$types = $_POST['type'];
$total = 0;
foreach($types as $key => $type) {
if (!isset($prices[$type]))
continue;
$total += $prices[$type];
}
// Use $total as your total price for whatever calculation
echo $total;
As per your comment, if you still want these prices client side for calculations, you can use json_encode to output it into a script tag and use the prices directly. It's basically going to turn the server-side prices array into a client-side array of prices.
<script type="text/javascript">
var prices = <?= json_encode($prices) ?>;
// Now you can use prices['Coke'] etc, based off the value of the selected checkbox.
</script>
I would like to know how, if my user selects Fanta checkbox,
Then name you field fanta:
<input type="checkbox" id="Fanta" name="fanta" value="70" />
Once you have done that you can get the values using either the $_POST or the $_GET superglobal (depending on your form method):
if (isset($_POST['fanta'])) {
echo $_POST['fanta'];
}
However you should never ever ever rely on the prices coming from the clientside!
but I need form checkbox VALUES to stay NUMERIC for total price calculation.
That is not going to happen because in HTTP values are being send as strings. Luckily PHP does automatic type juggling for you so you will still be able to do calculations with the string values.
http://codepad.viper-7.com/GOhHdz
In some situations you want to make it a integer value explicitly. In that case you can use type casting:
var_dump((int) '18');
http://codepad.viper-7.com/kJlGoS
When you post a form, anyway you will get 70 as value.
echo $_POST['Price'];
gives 70
<input type="checkbox" value="Coke" name="Price" rel=70 />
<input type="checkbox" value="Fanta" name="Price" rel="70" />
<input type="checkbox" value="Sprite" name="Price" rel="70" />
use javascript to get rel attribute value for selected checkbox for total price
Related
I have arrays of radio buttons in my view
<input type="radio" id="s20" name="radio[1]" value="5" { { old('radio.1') ? checked="checked" : '' } } />
...
I need to get their count to pass it to a my PostRequestValidator via <input type="hidden" value="count" name="count" />
I suppose the number of inputs are not known at priori, but there are many ways to do it.
The easiest way is counting them using JavaScript or jQuery to update the value field of the hidden input.
$("input[type=hidden][name=count]").val(
$("input[type=radio]").length
);
Obviously you can select those elements in better ways (using their name or class).
I have code similar to the following:
<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...
Once the form is submitted I want to get checked checkboxes, so far I've been using
if (isset($_POST['visitProperty']) {..}
But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">
<?php
foreach($_POST['visitProperty'] as $check) {
echo $check . "<br>"; // for example
}
?>
NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]
When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.
You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).
So in case of a unique, identifyable ID, you could do something like:
<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">
The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.
i have a form in html that when the certain fields are clicked i want it to add up the values in the php part, so i thought that if i put the php part in the value area of the html then it would add it up, but it adds them all up but not the specific ones when clicked, Please help:
<p>
<input type="checkbox" name="one" value= <?php $number = 2.39; ?>
<label for="one">Four 100-watt light bulbs for $2.39</label>
<p>
$total = $number + $numberone + $numbertwo + $numberthree;
echo "Total cost is " .$total;
echo $card;
?>
</form>
First, you need to echo out the number value in your form, not just assign the variable, nothing will be rendered there. Also, I find that if you're adding numbers for totals, it's easier to name them in a fashion that you can pull the values back out in an array
name="one"
would become
name="price[]"
or something of the like.
<p>
<input type="checkbox" name="price[]" value= <?php echo('2.39'); ?>
<label for="one">Four 100-watt light bulbs for $2.39</label>
</p>
Then on the server side, just sum the totals
$total = 0;
foreach($_POST['price'] AS $value){
$total += $value;
}
You'll want to do some input sanitization, but this example should get you going.
This example will only work on the server-side after a form submission, if you want to add them up in realtime on the page, you'd have to use ajax to add the values up.
I am using position absolute's validation engine for my form. I would like to check whether at least one checkbox from group is selected. In examples it is done by setting the same name attribute for group of checkboxes.
I cannot name checkboxes with the same name, because I am saving their state in database with following code:
$values = array(
'checkbox1' => null,
'checkbox2' => null
);
foreach (array_intersect_key($_POST, $values) as $key => $value) {
$values[$key] = mysql_real_escape_string($value);
}
$query_add_candidate=sprintf("INSERT INTO dbase (checkbox1, checkbox2) VALUES ('$values[checkbox1]', '$dates[checkbox2]')"
Now checkbox1 and checkbox2 are validated individually, beacuse they have different names. How can I check if selected is at least one of them?
Here is my HTML code:
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox1" id="maxcheck1" value="1"/> Text1
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox2" id="maxcheck2" value="2"/> Text2
on php ,
if(!$_POST['checkbox1'] && !$_POST['checkbox2']){
echo 'Error check at least one';
}
but what you really want is an array,
HTML,
<input type="checkbox" value="ch1" name="check[]" />
<input type="checkbox" value="ch2" name="check[]" />
php
<?php
if(empty($_POST['check'])){
echo 'Error: hey, check at least one will you!?';
}
?>
so this way you don't have to check all of them one by one, especially if you have loads of them on the same page.
NOTICE: You should also know, if checkbox is not ticked it will also not be set on the php $_POST superglobal, otherwise if it is ticked, it will show whatever the value="..." holds,
if its posted then its checked,
so if you have it in $_POST["checkbox_name"] then its checked, otherwise it wont be posted.
You can either add loads of code to reimplement control arrays in a poor way, or you can alter the code that builds your query so it can accept control arrays.
I would prefer the latter.
I've this simple form, when the price is set to 120 $:
<input type="checkbox" name="addon[book]" value="120" /> book
<input type="checkbox" name="addon[plane]" value="420" /> Plane
Could I send two values (USD & €) with thr form?
EDIT:
I added name="addon[book]" because I'll display the name too
EDIT 2:
PHP:
foreach($addon as $name => $value) {
echo 'your addons: '.$name;
}
$total = array_sum(array_map("intval", $_POST["addon"]));
echo 'the total '.$total.' $';
<input type="hidden" name="addon[book_USD]" value="250" />
<input type="checkbox" name="addon[book_EUR]" value="120" /> book
However NEVER trust the input from the form. Check the price in the backend code or users can change the price!
EDIT
What about json encoding?
<input type="checkbox" name="addon[book]" value="{"USD":250,"EUR":120}" /> book
EDIT2
To explode the values you'll have to use json_decode()
EDIT3
As others (and myself) already stated what you are trying to do (at least from the looks of it) is that you use the price from the input field as the price the user is going to pay.
That is not the best thing you can do, cause users can easily change the price of the input field.
I don't know your code but somehting like the following would be much better:
<input type="checkbox" name="addon[book]" value="book1">
And in your PHP code:
$prices = array('book1'=>array('USD'=>250, 'EUR'=>120),
'book2'=>array('USD'=>120, 'EUR'=>60),
);