$_POST variables missing when adding HTML checkbox array - php

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.

Related

Checkbox with quantity field to array

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]">

Why i cannot get POST values?

This is my form, that is dynamically generated:
<form action='index.php?direction=saveUserPreferences' method='post'>
<label>Office space<input type='checkbox' name='userInterest[]' value='1' id='1'></label>
<label>Grants<input type='checkbox' name='userInterest[]' value='2' id='2' checked></label>
<label>Loans<input type='checkbox' name='userInterest[]' value='3' id='3'></label>
<label>Events<input type='checkbox' name='userInterest[]' value='4' id='4' checked></label>
<label>Connecting<input type='checkbox' name='userInterest[]' value='5' id='5' checked></label>
<label>Office administration<input type='checkbox' name='userInterest[]' value='6' id='6' checked></label>
<label>TAX<input type='checkbox' name='userInterest[]' value='7' id='7' checked></label>
<label>Self employment<input type='checkbox' name='userInterest[]' value='8' id='8' checked></label>
<label>Start up<input type='checkbox' name='userInterest[]' value='9' id='9' checked></label>
<label>Banks<input type='checkbox' name='userInterest[]' value='10' id='10' checked></label>
<input type='text' name='name' id='name'/>
<input type='submit' name='sendData' value='Save'/>
Form points to controller function:
public function saveUserPreferences() {
var_dump($_POST);
}
I tried checking whether post is set etc, and i cannot get array from the post.
I have also login form, that points to the same controller and another function, and it works fine. So i know controller file can and do receive post values.
But for some strange reason, for this particular form i cannot get any values. I have looked up another questions and answers, but cannot get this thing to work.I am also familiar with:
PHP tutorial about the forms
Following that solution i get unidentified index error. Is there something i miss?
Thanks in advance for any tips and tricks ;)
08 03 2015
Hey once again. I have no idea if that helps someone by i would like to give an update of what is happening. As you know, i would get nothing from the page, that is when i would echo values in php file, there would be nothing. Yet, in the firebug and chrome developer tools (in the headers and response part of network section) i was able to see actual response. So what i did is this: created a demo table in database, and tried to insert values that i should see from the form into database. And weirdly enough, it did worked. So, don't ask me how is it possible, since i have no idea, yet. So i decided to work based on the responses from developer tools and firebug. And current path of my data is this. View->Controller->Model (post data to array variable) -> dao class->database. Ok, thanks all for viewing and commenting on that one. Thanks
Is there a redirect before reaching the controller? POST data does only exist for the next request so if you submit to /redirect.php that takes you to /controller.php, $_POST would be empty.
Also your checkboxes doesn't seem right to me. A normal checkbox is like this:
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
So the name is what the checkbox "is about", and the value it's own unique value. The [] is only for select boxes.

How to customize query string with multiple checkboxes under same name?

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

Storing checkbox values in session and then reselecting them through session

I am simply trying to pass my checkbox values through a session variable for use if the user goes back at some point. After going to my first page and POSTING, I want these selections stored in a SESSION variable. I've had no luck so far in figuring this one out. My code is below.
Here is my html of my checkboxes. I have about 15 checkboxes with the same name as below. I take all those checkboxes and break them down in another script for insertion into a database.
<input type='checkbox' name='list[]' id='product' value='Product'></input>
Here I am setting my variable with the POST of the checkboxes.
$checkboxes = $_POST['list'];
$_SESSION['list'] = $checkboxes;
How can I pass these checkbox selections into and out of a session variable for selecting elements on a previous page?
Change:
<input type='checkbox' name='list[]' id='product' value='Product'></input>
To
<input type='checkbox' name='list[0]' id='product' value='Product'></input>
<input type='checkbox' name='list[1]' id='product' value='Product'></input>
etc.
And use foreach:
Okay so. We know that in $_SESSION['list'] we have only checked ones!
foreach ($_SESSION['list'] as $key => $value)
{
echo '<input type="checkbox" name="list['$key']" value="'.$value.'" checked="checked >';
}
Just thought I share my code / solution for storing the html form checkbox value in a session. My search did not get me to cover all scenarios that I needed.
My scenario includes that I pass a default state for checked or not checked.
HTML Form Code:
<input type="hidden" name="product[]" value = 0>
<input name="product[]" type="checkbox" value = 1 <?php echo (($productvalue == 1) ? 'checked' : '')?>>
PHP Form processing code:
$productvalue = ((isset($_POST['product'])) ? array_sum((array)$_POST['product']) : ((isset($_SESSION['productsession'])) ? $_SESSION['productsession'] : 0));
$_SESSION['productsession'] = $productvalue;

PHP HTML form arrays - how to tell when a user adds a new entry

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'/>

Categories