Send checkbox Array with jquery ft.upload, PhoneGap - php

I have some checkboxes in my app, which could be checked (naturally). This checkboxes are named like 'checkboxname[]', and every checkbox has it's own value (4,5,6,7,8, and so on).
Now I would like to get all the values (just from the checked checkboxes!!) into an array, send this array over ft.upload, and split the array with foreach with php. But my php file is always saying that the arguments are invalid for my foreach function.
Heres the javascript.
var values = new Array();
$("input[name='checkboxname[]']:checked").each(function(){values.push($(this).val());});
params.checkboxname = values;
Heres the php code.
$rubriks = $_POST['checkboxname'];
foreach($rubriks as $ausgabe){
echo $ausgabe; }
I don't know whats wrong. I tested with var_dump whats actually send from the javascript, an theres no checkbox-array so I think there is the problem?!

Related

Assign variable to an array from Active Directory

I am having trouble figuring out how to correct an issue I am having with the following code. I am trying to list the names and emails of all the people in my Active Directory. This code works. However, I also get the following warning when it is executed.
Warning: Cannot use a scalar value as an array.
From what I have read online I need to set " $name['mail']['0'] = "Not Found";" to an array. My question is how would I go about doing this. I have tried every way I could think of with no success. If anyone could provide me with some feedback it would be greatly appreciated.
foreach ($results as $name) {
if (!isset($name['mail']['0'])){
$name['mail']['0'] = "Not Found";
}
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
It looks like you are trying to assign the "Not Found" value BACK into the results of the ldap query you are running.
When I wrote code to get user information from ldap, I always inserted values into a different array (getting the fields I wanted) and then displayed THAT array out in the HTML:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
}
}
Then after you have all the fields you want you display the $allnames array.
Having said that, when I construct the array of user information, I make the array much much simpler - knowing what I want to display out, more along the lines of this:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames['mail'] = $name['mail']['0'];
}
}
You (or at least I) don't need to follow the ldap structure when making the array of information I have gathered, but rather I want to get specific fields from the directory and then display them by what they are - for example when I output the value in mail I might want to hyperlink it as an email address, so I keep it in an array element that I know by key and refer to later.

Form submission: PHP S_SESSION statements within a foreach loop

Let's suppose I have the following PHP code which attempts to read from an array called $arr which takes on the values {fullname, studentnumber, email}. Upon submission of my HTML form, this PHP code will execute the foreach loop, and store the values posted to the page in the $_SESSION array.
foreach($arr as $field):
$_SESSION[$field] = $_POST[$field];
endforeach;
The above code doesn't work as intended. If I were to replace the above code block with the code below, the values in the fields on my page get stored correctly in the $_SESSION array.
$_SESSION[fullname] = $_POST[fullname];
$_SESSION[studentnumber] = $_POST[studentnumber];
$_SESSION[email] = $_POST[email];
How do I accomplish this, in an efficient and expandable way? I don't want to have to write new $_SESSION statements every time I add a new field to my form.
EDIT: // The first block of code actually works as intended. There was a typo originally!
what you want is to use array_keys() otherwise you're getting the array contents.
foreach (array_keys($arr) as $field)
Let me know if this works/is what you're looking for :)

PHP $_POST is working but not getting value of HTML element

TL;DR version
I have a big form with hundreds of html input elements on it, I made a php array to store all the element names in it. When I run my process.php file it runs through that array using a loop and creates a new array, the key of that array is the name of each element, and the value of that array is the value of each element. I know this code works as element values for text boxes, radio buttons, and drop-down selections work fine. Checkboxes do not have the values put into the array, instead the key and value read the same thing, the name of the element. Why is $_POST giving me the NAME or ID of my checkbox, and not its value?
Full explanation /w code samples:
The problem I am having seems rather unusual to me as from my understanding it goes against how $_POST should work.
In any case I have been developing a travel insurance website for the past year and it is nearing completion. The client wants me to create a version of the form which they can view after people have submitted applications and it will get the values out of the file they select.
My problem is that $_POST is not getting the value of my checkbox elements but rather their name or id (both are identical so I cannot be sure which it is getting). $_POST is successfully getting the value of all radio, text, and drop-down elements, just not checkboxes.
I have a very large array in my process.php file as the form is quite large. What I've done is create an array which has the name of each element I wish to access. Below is a sample of the structure the array follows, it is far to large to post the entire thing here (400 plus elements on form).
$form_data = array
('trav_emer_med_insur',
'trav_emer_single',
'trav_emer_single_date_go',
'trav_emer_single_date_ba',
'trav_emer_single_days',
'trav_emer_annual',
'trav_emer_annual_date_go',
'trav_emer_annual_days',
'trav_emer_extend',
'trav_emer_extend_date_go',
'trav_emer_extend_date_ef',
'trav_emer_extend_date_ba',
'trav_emer_extend_days',
);
This is the code that runs on process.php to create the user data file, which is saved to a protected folder on the server.
// Create user output data
$out_data = array();
$count = count($form_data);
for( $i = 0; $i < $count; $i++ )
{
if(empty($_POST[$form_data[$i]])) {
$out_data[$form_data[$i]] = " ";
}
else {
$out_data[$form_data[$i]] = $_POST[$form_data[$i]];
}
}
//Set variable names for new file
$dir = "/home/imelnick/public_html/getawayinsured.ca/userdata/";
$timestamp=date("YmdGis");
$name = $out_data['txtApp1Name'];
$value = str_replace(" ", "", $name);
$item = $value . "^" . $timestamp . ".txt";
$filename = $dir . $item;
//Put data in file
//Open file for writing
$fileHandle = fopen($filename, 'w') or die("Can't open file");
//Write contents of out_data array to file
foreach ($out_data as $key => $value) {
$fileLine = $key . "\t" . $value . "\r\n";
fwrite($fileHandle, $fileLine);
}
//Close file
fclose($fileHandle);
The first block of names in the array belong to checkboxes and they follow the format of the following:
<input type="checkbox" name="trav_emer_med_insur" id="trav_emer_med_insur_if" value="YES" class="form_elements" onClick="if(this.checked){document.getElementById('trav_emer_med_options').style.display='block';}else{document.getElementById('trav_emer_med_options').style.display='none';}"/>
The onClick statement expands a div containing additional checkboxes which offer further options to the applicant.
My output data array which is created from the form data has the key of each item in the array as the name of the element, and the value of each item in the array the value of the corresponding HTML element.
Upon splitting the array and writing the $key/$value combination the expected value of $_POST[$form_data[0]] (note: $form_data[0] = trav_emer_med_insur) is the value of the above checkbox code, value="YES". However the output in the file reads as follows.
trav_emer_med_insur trav_emer_med_insur
I am quite sure that there is not a problem with the code that processes the form itself as other elements on the form have their values saved perfectly well to the file (radio buttons, text boxes, drop-downs all work). The Checkboxes do not, they refuse to $_POST the value of the HTML element, and simply keep putting out the name twice.
For example, another element in the form not listed in my array sample above named smoked_if is a radio button pair which asks if the applicant has smoked or not. Here is a sample output from a recent application.
As can be seen the desired result of my code is being performed.
smoked_if no
I am at a loss here because not only does this contradict the functionality of $_POST itself but since all other elements on the form have their values posted without issue it tells me there is a problem with checkbox elements and $_POST.
Any assistance is greatly appreciated.
I think your problem might be related to the fact that checkboxes are not posted when they are not checked.
See Post the checkboxes that are unchecked
if you need to get around this behaviour
AS long as the information is inside of the Form tags, it will be passed to the server. With that said, you need to understand that what is sent to the server is the NAME and VALUE of the items.
When you are looking at PHP, you want to submit, and then a string will look like: sample from http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox as well
?vehicle=car&vehicle=truck
In PHP when you post it, the idea is the same but just hidden from the unskilled eyes. To get the values of it in my example:
$vehicle= $_POST["vehicle"];
echo ''.$vehicle; //shows ARRAY
foreach( $item in $vehicle){
echo ''.$item.'\n'; //will iterate through all the vehicle and print them out
}
YOu can also say things like
if(in_array("car", $vehicle)){
echo "there exists a car\n";
}
http://php.net/manual/en/function.in-array.php
Since the checkboxes are posted only if they are checked, you need a workaround for that. I think the most usual fix is to use a hidden input with the same name as the checkbox's and usually 0 as value. And do not forget to put the hidden input before the checkbox :)
This way even if the checkbox is not checked you get the value of the hidden input. If the checkbox is selected the hidden field's value is overwritten in the POST array by the one from the checkbox.

Multidimensional Array in PHP (form data with checkboxes)

I'm new to PHP and having a problem reading checkboxes submitted by a form. Before explaining it i would like to mention that i'm trying to edit a much larger application and the data cannot be sent in any other way. It's just a matter of finding a good method of reading what is being sent.
When a normal text input is sent, the form will post the following:
custom[0][type]="text"
custom[0][name]="VariableName"
custom[0][value]="VariableName"
Basically there is a main "custom" multidimensional array that has several elements (0,1,2,3 etc) and each element has name and value.
However, when one of the elements is a checkbox, the following stuff gets posted:
custom[1][type]="list"
custom[1][name]="SelectedOptions"
custom[1][value]="Value1"
custom[1][value]="Value3"
custom[1][value]="Value5"
Getting to the PHP side of things, this is the code i'm using to read the data sent by the form. The code below works ok in scenario 1 (with text based inputs) but only reads one value when we have list type custom data.
foreach($_POST['custom'] as $item){
if($item['value'] != "") echo $item['name'].'='.$item['value']
}
The problem is that $item['value'] only reads one of the values, not all 3. How can i get all 3 values in a variable ? It probably is a very easy thing...
To put it all combined, this is what it is sent with POST (3 checkboxes are checked for Variable2)
custom[0][name] Variable1
custom[0][type] text
custom[0][value] ValueForVariable1
custom[1][name] Variable2
custom[1][type] checkbox
custom[1][value] Value1
custom[1][value] Value3
custom[1][value] Value5
And this is what print_r($_POST) shows for the posted data above
[custom] => Array
(
[0] => Array
(
[value] => ValueForVariable1
[name] => Variable1
[type] => text
)
[1] => Array
(
[value] => Value1
[name] => Variable2
[type] => checkbox
)
Just to be sure we're all on the same page, the actual data is generated by a more complex system and we can't really change that. I'm interested in seeing how we can read all 3 values for Variable2 that are sent in the POST.
Thanks !
EDIT:
With the extra information that you've since provided, I see that I originally misunderstood the problem.
Since you have no control over over the form that sends the data to your PHP script, and thus can't change it to ensure that the later checkboxes with the same name do not overwrite the earlier ones, you'll have to get access to and process the raw post data itself.
$postdata = file_get_contents("php://input");
echo $postdata;
... will output the postdata just like a GET querystring: blah=1&blah=2&blah=3 (blah indicates 3 form fields with the same name blah, the first two of which would be overwritten in $_POST leaving $_POST['blah'] = 3).
with a little exploding on & and looping and parsing looking for the variables in question, or even any conflicting variables, will get you where you're trying to go.
Original Answer:
HTML forms only submit checkboxes (or radio buttons) that have been checked. If they haven't been checked, the browser won't send the data back to the server.
The main way to solve this is to know what you're looking for on the back end and test for it (i.e. if (isset($_POST['checkboxname'])).
If you truly need a general purpose backend that will dynamically incorporate all checkbox elements, checked or not, the way I've solved this in the past is to use javascript to record all form elements on the page and submit that info with the rest of the form (and, in my case, also submit whether that field was changed or not).
Here's a function that you can run at the top of the script to treat POST the way you expect it to be treated from your ASP background:
function post_process() {
$rawpostdata = file_get_contents('php://input');
if (!$rawpostdata) return;
$fields = explode('&', $rawpostdata);
$post = array();
foreach ($fields as $field) {
list($key, $val) = explode('=', $field);
if (isset($post[$key])) $post[$key] .= ','.$val;
else $post[$key] = $val;
}
$_POST = $post;
}

How to make array of textbox fields in PHP?

I'm using the CodeIgniter framework.
I'm using the form_input function to make a 2d array of textboxes and pulldowns.
The function produces HTML like this:
<input type="box" name= "variable">
I need to create 30 rows of 5 textboxes (time, event, supplies, sucess{yes/no}, comment).
My plan was to somehow be able to uniquely identify them all so at a later stage when I $Post them to another page it wont get confused as to which textbox is time1 or which textbox is time2.
I'm trying to make this array of texboxes in php so that when I use a for loop with (ISSET) I can stop when I get to a row that is not completed by the user.
This is my code here but im not too sure if its spot on
for ($i =0; $i< 30; $i++)
{
//time part of event field
echo form_input ($events['time',$i]),
//the event itself
form_input ($events['event',$i]),
//supplies used
form_input ($events['supplies',$i]),
//successful?
form_dropdown ($events['success',$i] $success),
//comment if necessary
form_input ($events['time',$i]);
echo '<br/>';
}
I really don't understand either but regarding the $_POST object; this is from the CI docs:
CodeIgniter comes with three helper
functions that let you fetch POST,
COOKIE or SERVER items. The main
advantage of using the provided
functions rather than fetching an item
directly ($_POST['something']) is that
the functions will check to see if the
item is set and return false (boolean)
if not. This lets you conveniently use
data without having to test whether an
item exists first. In other words,
normally you might do something like
this:
if ( ! isset($_POST['something']))
{
$something = FALSE;
}
else
{
$something = $_POST['something'];
}
With CodeIgniter's built in functions
you can simply do this:
$something = $this->input->post('something');

Categories