I'm trying to create something of a packing list that will export into an array that I can print out.
The basic code is as follows:
<form method='post' id='items' action='items.php'>
<input type="checkbox" name='list[]' value='sunglasses' />sunglasses
<input type='checkbox' name='list[]' value='sunblock' />sunblock
<input type='checkbox' name='list[]' value='socks' />Socks<input type="text" name="socks_qty">
<?php
if (isset($_POST['list'])) {
print_r($_POST['list']);
}
?>
Only items that are checked will appear in the array.
I would like to add functionality that will allow me to specify a quantity of SOME items (ie. socks)with a text input field next to the checkbox so that I can extract the quantity from the array somehow. I'm not sure the best way to approach this as not all items need to have a quantity and I'm not sure how to tie the quantity to a specific item.
The output might be something like 'list[socks][3]'.
What would be the best way to approach something like this?
You can use the item names as keys instead of values, then another key after that to indicate yes/no vs. quantity.
<input type="checkbox" name='list[sunglasses][checked]' value='1' />sunglasses
<input type='checkbox' name='list[sunblock][checked]' value='1' />sunblock
<input type='checkbox' name='list[socks][checked]' value='1' />Socks
<input type="text" name="list[socks][qty]">
Remember with checkboxes that you won't get anything in $_POST unless it was checked, so if you're going to use this to determine what you still need, you'll have to set some default false values for all the various keys and then update with the values from $_POST for the things that were checked.
Another possibility is to forgo the checkbox for things that you want a quantity of (unless the quantity is optional) because with a quantity > 0, the checkbox value is kind of redundant. Unless you want the ability to express that you have a certain number of socks, but you aren't bringing them. ;-)
<input type="checkbox" name='list[sunglasses]' value='1' />sunglasses
<input type='checkbox' name='list[sunblock]' value='1' />sunblock
socks<input type="number" name="list[socks]">
Related
I have a form with a get method that is used for searching items (vehicles) from the database. The form has multiple checkbox sets. Like user can filter the search results by selecting multiple makes from the makes checkbox list. Similarly user can also apply filter on car models from models chebox list and so on. There are about 10 such filters on the form.
Here is the code snippet of the my form:
<form action='./search' method='get'>
<input type='checkbox' name='make[]' value='BMW'/>
<input type='checkbox' name='make[]' value='Mercedes'/>
<input type='checkbox' name='make[]' value='Honda'/>
<input type='checkbox' name='make[]' value='Toyota'/>
<input type='checkbox' name='make[]' value='Porsche'/>
......//Remaining Form
</form>
Similarly for car models I have similar markup...There are such 10 filters all implemented using checkbox list.
Now coming towards the problem, when I submit the form I get URL like this:
http://localhost/auto/search?make=BMW&make=Mercedes&make=Honda
This is forming a type of query string which I don't like i.e it is repeating 'make' attribute for all the checked values and will do so for remaining 9 filters as well. This will result in very long ugly looking URL.
What I want is that my URL should look something like this:
http://localhost/auto/search?make=BMW,Mercedes,Honda
This is much better but I don't know how would I achieve that. What I have tried is to get all the values of checked boxes and then write them into hidden field value so that I get my desired format in the query string. And unset all the checboxes selected by the user and submit the form with hidden field containing all the selections. But the problem is when the form is submitted I get two 'make' fields in the URL like:
http://localhost/auto/search?make=&car_makes=BMW,Mercedes,Honda
where car_makes is the hidden input field in which I wrote all the selections in value attribute.
Any solution to this? So that make attribute does not get submitted, but it does, since it is the part of the form.
Thanks.
Try these:
<html>
<head>
<script type="text/javascript">
function buildup(){
var makes=document.getElementsByName('make[]');
var m=document.getElementById('make');
m.value='';
ms='';
for (var i = makes.length - 1; i >= 0; i--) {
if(i>0)ms=ms+',';
ms=ms+makes[i].value;
}
m.value=ms;
document.getElementById('form').submit();
}
</script>
</head>
<body>
<input type='checkbox' name='make[]' value='BMW'/>
<input type='checkbox' name='make[]' value='Mercedes'/>
<input type='checkbox' name='make[]' value='Honda'/>
<input type='checkbox' name='make[]' value='Toyota'/>
<input type='checkbox' name='make[]' value='Porsche'/>
<form action='./search' id='form' method='get'>
<input type='hidden' name='make' id='make'>
<input type='button' value='submit' onclick='buildup()'>
</form>
</body>
If you don't want the make inputs to get submitted, you should disable them before the form gets submitted (or remove the name attribute...).
It would probably be easiest to add a class to all inputs you don't want to submit and then do something like this right before the form submit:
html:
...
<input type='checkbox' name='make[]' value='BMW' class='dont-send-class' />
...
js:
$('.dont-send-class').prop("disabled", true);
// now you can submit the form
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.
When a process changed required me to change a radio button field, to a checkbox to allow multiple selections, I made the following change to my html:
<input type='checkbox' name='ptype[]' value='1'> Jail/not sentenced</br>
<input type='checkbox' name='ptype[]' value='2'> Jail/Sentenced</br>
<input type='checkbox' name='ptype[]' value='3'> State/DOC</br>
<input type='checkbox' name='ptype[]' value='4'> ICE/US Marshall</br>
<input type='checkbox' name='ptype[]' value='5'> 7x/wardens Agree</br>
When making ptype an array to handle multiple selections, I am finding my $_POST variable missing a key. If I try to revert ptype to a radio button and handle only one value, I don't get any error/warning.
I've checked Firebug, and when ptype[] is set, one of my $_POST variables is not relayed. I know the max post variable is not an issue, as I only have 52 post variables on my form.
I don't know if it's relevant, but this field is never coming through:
<input type='radio' name='wc' value='1'> Yes
<input type='radio' name='wc' value='0'> No
Notice: Undefined index: wc in C:\inetpub\wwwroot\internal_tools\include\consults\consult_utilities.php on line 53
Any help would be greatly appreciated.
EDIT: As requested, my form.
EDIT 2: Firebug POST variables
EDIT 3: Added line 53:
$data['workers_comp'] = $_POST['wc'];
Probably you didn't select an option for the wc radiobutton and therefore the variable is not submitted.
You should change your PHP code to:
$wc = isset($_POST["wc"]) ? $_POST["ws"] : "0";
Or, as I already suggested in comment, you have a problem in the javascript method validateFrm which you call upon submitting the form.
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
I apologise for the vagueness of the title but I'm finding this hard to put into words.
I have a form which contains key:value pairs which are read from the database, placed as values in inputs and then read back in again (possibly edited, deleted or added to). On the server side, I need to figure out just what the user has done to muck up my data.
Example:
<input type='text' name='keyID[]' value='1'/>
<input type='text' name='key[]' value='someKey'/>
<input type='text' name='value[]' value='someValue'/>
<input type='text' name='keyID[]' value='2'/>
<input type='text' name='key[]' value='someOtherKey'/>
<input type='text' name='value[]' value='someOtherValue'/>
<input type='text' name='keyID[]' value='3'/>
<input type='text' name='key[]' value='yetAnotherKey'/>
<input type='text' name='value[]' value='yetAnotherValue'/>
<input type='text' name='key[]' value='aCompletelyNewKey'/>
<input type='text' name='value[]' value='aCompletelyNewValue'/>
When the form is submitted, I delete the original data, do a foreach loop through the keys (geting the nth value of the other arrays as my new data), and add this to the database.
This worked up until I hit a snag with my updated database design - now I need to be able to tell what hasn't been submitted (removed on the page), what's still there (been changed or left as-is), and what is new (won't have a keyID[]).
I've been using array_diff() against a list of currently stored keyID[]s to figure out what to delete and update, but how would I figure out what has been added when there won't be a keyID[] for the new inputs (but there will be both a key[] and value[])?
Also I can't guarantee the order in which the values of the arrays will be submitted so I don't think counting to the end of the keyID[] array and then checking the others for extra values would give me the correct values every time.
Any ideas?
An idea is to change slightly the last block, like this:
...
<input type='hidden' name='keyID[]' value='NEW'/>
<input type='text' name='key[]' value='aCompletelyNewKey'/>
<input type='text' name='value[]' value='aCompletelyNewValue'/>
This way, all three arrays will have the same number of elements. In addition, it is now trivial to see whether something is new or not.
I think you might be overcomplicating it a little bit. You can pass an ID in the name of your field, so you can identify it directly. Usually this would be the record ID from the database, or some other unique identifier. Lets assume the checkboxes turn on/off a 'featured' flag - so what you could do is something similar to this:
<input name="featured[<?php echo $record['id']; ?>]" value="<?php echo $record['value']; ?>">
The $record['value'] is always going to hold some data (it might just be a blank string) which will update/process on submitting.
When you submit, you'll need to iterate through your $_POST['featured'] array as $key=>$value and set the value of the record to $value where the record id is equal to $key. If the record doesn't exist to update, you could create a new one using the INSERT ON DUPLICATE KEY UPDATE sql command.
I hope that helps!
If you place the keyID inside each record [], you can check back at your database which one was updated, and how.
For example:
<input type='text' name='key[1]' value='someKey'/>
<input type='text' name='value[1]' value='someValue'/>
<input type='text' name='key[2]' value='someOtherKey'/>
<input type='text' name='value[2]' value='someOtherValue'/>
<input type='text' name='key[3]' value='yetAnotherKey'/>
<input type='text' name='value[3]' value='yetAnotherValue'/>
<input type='text' name='key[n]' value='aCompletelyNewKey'/>
<input type='text' name='value[n]' value='aCompletelyNewValue'/>
Note that [n], if generated through javascript, would need to be automatically incremented.
Another suggestion would be to invert the keyID and the key|value names, so that you could receive and compare each array element with the database. But that would need a prefix (like the tablename):
<input type='text' name='tablename[1][key]' value='someKey'/>
<input type='text' name='tablename[1][value]' value='someValue'/>