Forum Moderators: coopster

Message Too Old, No Replies

Help! Using PHP to process my form.

Using one variable name to process many different checked values

         

bartainer

3:43 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Hello:

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.

henry0

4:33 pm on Dec 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not sure on what you try to achieve but give a try at Variable Variable there are a few samples if you search here.

coopster

4:36 pm on Dec 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to set them up as an array. See the page on PHP and HTML [php.net] for a "how-to" example.

rjwmnews

4:55 pm on Dec 28, 2006 (gmt 0)



<?php
/*
Caveat: code *not* verified and debugged!

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);
}
?>

bartainer

5:14 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



I know what you're saying; however. The checked box name is C1 (there are approx. 46 boxes that can be checked), so of course I named my ONE variable ($C1) to match the checked (or unchecked) boxes.

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.

coopster

5:28 pm on Dec 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hopefully I'm not the only one confused here ;)

By using brackets on the value in the "name" attribute of the <input> element you can use the same name. See the example given by rjwmnews? PHP will create an array for that variable and you can loop through the values.

bartainer

5:32 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



You're not, I'm the one confused here. ;)

bartainer

5:37 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



You know, I never thought about the enter or using the submit button would make a difference. Wow! Thank you. So, would I use a hidden field to name the submit button?

bartainer

5:41 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Here is the variable @$C1 = ($_POST['C1']);

rjwmnews

5:49 pm on Dec 28, 2006 (gmt 0)



I'm not sure what you mean by "I cannot give the 'same' checked name to all my different values."

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 '>&nbsp;<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.

rjwmnews

6:04 pm on Dec 28, 2006 (gmt 0)



I noticed that others have posted replies before I had a chance to complete my responses, so this reply may not be in chronological order and be redundant or not consider the above-listed replies.

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

rjwmnews

6:09 pm on Dec 28, 2006 (gmt 0)



Re-reading your replies it sounds to me that you should use an associative array with descriptive (string) keys to create checkbox labels and values rather than using the values as the variable name. With array_flip() on the original checkbox array for the HTML form to swap the keys and values so that you can use array_search() to associate the returned form submitted values to the original array keys. This may sound confusing so again, I need to know what you are trying to accomplish.

rjwmnews

6:20 pm on Dec 28, 2006 (gmt 0)



> Here is the variable @$C1 = ($_POST['C1']);

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.

bartainer

6:30 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Wow! Great stuff!

It seems to me that I'll still have a lot of work ahead me and that's fine too!

Back to the submit button. Please explain that in more detail? You caught my attention. :)

bartainer

6:42 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



<input type="checkbox" name="C1[]" value="one">First
<input type="checkbox" name="C1[]" value="two">Second
<input type="checkbox" name="C1[]" value="three">Third

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.

bartainer

12:13 am on Dec 29, 2006 (gmt 0)

10+ Year Member



Can I do this?

$C1= array (1, 2, 3, 4 etc);

rjwmnews

1:27 am on Dec 29, 2006 (gmt 0)



> So, would I use a hidden field to name the submit

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.

bartainer

1:47 am on Dec 29, 2006 (gmt 0)

10+ Year Member



Great stuff here.

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.