napsterved
New Member
TL;DR versionI 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).\[code\]$form_data = http://stackoverflow.com/questions/13828845/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',);\[/code\]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.\[code\]// Create user output data$out_data = http://stackoverflow.com/questions/13828845/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 = http://stackoverflow.com/questions/13828845/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 fileforeach ($out_data as $key => $value) {$fileLine = $key . "\t" . $value . "\r\n";fwrite($fileHandle, $fileLine);}//Close filefclose($fileHandle);\[/code\]The first block of names in the array belong to checkboxes and they follow the format of the following:\[code\]<input type="checkbox" name="trav_emer_med_insur" id="trav_emer_med_insur_if" value="http://stackoverflow.com/questions/13828845/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';}"/>\[/code\]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 \[code\]$_POST[$form_data[0]] (note: $form_data[0] = trav_emer_med_insur)\[/code\] is the value of the above checkbox code, \[code\]value="http://stackoverflow.com/questions/13828845/YES"\[/code\]. However the output in the file reads as follows.\[code\]trav_emer_med_insur trav_emer_med_insur\[/code\]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.\[code\]smoked_if no\[/code\]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.