Forum Moderators: coopster
I am using PHP to process and validate my form for a wedding web site. I am not new to PHP or Dreamweaver; however. Can I use the same variable name (e.g. $C1) to process different checked boxes. I did try; however. I had checked 20 boxes and only the last box checked was processed. The remaining values for the 19 boxes were not processed. There are approx. 45 boxes (values) that need to be processed if checked.
I have the solution (time consuming): Create different variables to represent each value. However; I want a short cut. Keep in mind, these boxes are not required or validated. I just want the values processed.
Thanks in advance.
Use a hidden form value so as to check if the form was submitted.
If you use a form submit button, then the $_GET[] or $_POST[] global array
will not contain the button id if the user submits the form via keyboard
'Enter' key rather than clicking the button.
IMPORTANT: Checkbox input fields name value must contain the '[]' array type designator
<input type="hidden" name="hdnfld" id="hdnfld" value="9">
<input type="checkbox" name="chkbxarray[]" value="one">First
<input type="checkbox" name="chkbxarray[]" value="two">Second
<input type="checkbox" name="chkbxarray[]" value="three">Third
*/
$chkbx_array = array(); // initialize checkbox values array
$array_is = false; // $chkbx_array empty or not
$frmsub = true; // variable to indicate that form was submitted [not required]
if ( isset($_POST['hdnfld']) ) { // form submitted?
if ($_POST['hdnfld'] == '9') {
$frmsub = true;
if (isset($_POST['chkbxarray']) ) {
$chkbx_array = $_POST['chkbxarray'];
if ($chkbx_array) { // array not empty
$array_is = true; // not required
// diagnotic debugging to print array values
echo "<pre>\n";
print_r($chkbx_array);
echo "</pre>\n";
}
}
}
}
// some possibilities
// loop through the array and process index key values
if ($array_is) { // checkbox values array $chkbx_array not empty
foreach ($chkbx_array as $val) {
// Process the array values, creating a variable named
// the same as the array element value
$$val = $val;
}
// Or create a comma-delimited string of the array values
// to insert into a database [recreate the array using the explode() function]
$chkbx_vals = implode(', ', $chkbx_array);
}
?>
Now, the checked values are different. The previous web master numbered the values and the client knows the definition of each number (e.g. C1 may equal number 5, or number 6).
It seems to me that I cannot give the "same" checked name to all my different values.
I hope this helps. :)
Thanks.
A form that uses checkboxes with the same name value is useful for multiple selections -- and you must use the '[]' array designator -- then all the checked values will be placed into an array, which can be captured via the $_POST['chkbxarray'] or $_GET['chkbxarray'] global arrays.
You can use separate name values for each checkbox but then it would be more difficult to associate the various checkboxes to a common reference. It's better to group them using an array. If you use an associate array (descriptive index keys vs. integers), then you have more options. You can use the associative index keys as variable names.
You can assign a different array variable to the 'posted' array if you use an array of checkbox values to dynamically create the HTML checkbox input element tags. For example:
<?php
$chkbx_array = array('one' => 'first',
'two' => 'second',
'three' => 'third',
'four' => 'fourth');
?>
<form>
<?php
foreach ($chkbx_array as $key => $val) {
print '<input type="checkbox" name="chkbxarray[]" id="'.$key.'" value="'.$val.'"';
print '> <label for="'.$key.'">'.$val.'</label><br>'."\n";
}
?>
</form>
Then assign a *different* array variable to the posted array, for example:
$chkbx_posted = $_POST['chkbxarray'];
The posted array will be a numerical indexed array (you lose the associated index keys but you can peform a lookup against the original array to associate keys to values [e.g. array_flip(), array_search(), etc.]
Rather than me post a lot of possible code examples, please more clearly indicate precisely what you are attempting to accomplish with data capturing, processing, archiving, retrieval, etc. so that I can better provide ideas for a useful solution.
You can also make the form 'sticky' (remembering checkbox selections for returning to the form after validation errors) by maintaining separate checkbox array variables and using session variables to store the arrays. Array loop code constructs make for more compact code to display and process large checkbox groups.
If you are using a web server host that has 'Register Globals' enabled, then a variable with the same name as the checkbox array will be created, so you should assign a different named variable in code to avoid confusion or overwriting of session variables, etc, example:
$chkbx_posted = $_POST['chkbxarray'];
You can check to see if 'Register Globals' are enabled via phpinfo() [copy below code to a script to your server and then open in web browser]:
<?php
// Script: [mysite.com...]
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
You can disable 'Register Globals' in your local web site partition by placing the following in an .htaccess file at site root:
php_flag register_globals off
If 'C1' is the *array* name of all the checkboxes:
<input type="checkbox" name="C1[]" value="one">First
<input type="checkbox" name="C1[]" value="two">Second
<input type="checkbox" name="C1[]" value="three">Third
Then $C1 is an array type variable, *not* a 'scalar' single-value variable. Therefore you must process the variable as an array, even if it contains a single element. If no checkboxes are selected, then $_POST['C1']) will not exist -- not even an empty array.
I know the above (somewhat); and I am sure I know how tackle my situation now.
In brackets [ ]for separation only!
If [<input type="checkbox" name="C1[]" value="one">First] is chosen and [<input type="checkbox" name="C1[]" value="three">Third] is not, only the value of [<input type="checkbox" name="C1[]" value="one">First] will be displayed. However; the way I had it coded all will be displayed.
I hope I'm understanding. My 43 year brain is smoking here.
The form's hidden input is a type 'text' and since it has a default value it's name attribute will always be included in the POST or GET array, even if the form is submitted programatically or via keyboard 'Enter' key. You still need the submit button to enable manual submission of the form. The hidden form element serves only to provide programatic verification that the form has been submitted. This is useful if you want to use various form 'includes' depending on the form submission status. For example, you won't use form validation code if the form has been loaded but not submitted. Usually you will validate form data and either return an error with highlighted descriptions or an acknowlegement 'Thank You...' page and/or re-direction to another URL.
> Can I do this?
> $C1= array (1, 2, 3, 4 etc);
Sure, you create a numerically indexed array, starting with element 0 (zero). The array returned by a form with multiple checkbox elements of the same name however is automatically *created* during the form submission. You will create an array only to hold the name and value attributes of the checkboxes if you want to *dynamically* generate the checkboxes HTML code via PHP on the server, as detailed previously.
You can hard-code the HTML tags which doesn't require creating an array to hold the keys and values:
<input type="checkbox" name="C1[]" value="one">First
<input type="checkbox" name="C1[]" value="two">Second
<input type="checkbox" name="C1[]" value="three">Third
If you use manually created HTML, then you won't create an array, just process the returned checkbox values array from the form submission.
In regards to the processing the form, I do utilize my mailer and process with PHP. After the submission the person is sent to a thank you page; however. I do this differently. I include the html under my php closing?> tag and then the html is processed after the PHP. The html cannot be seen until the PHP is processed.
Thank for all your help...I have learned a lot from everyone's expertise.