close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Server Validation - Repopulating Checkbox Array

Thread began 4/03/2012 3:42 am by Ian S | Last modified 4/04/2012 6:23 am by Jason Byrnes | 3480 views | 3 replies |

Ian S

Server Validation - Repopulating Checkbox Array

Hi WA,

I have a form with a checkbox array. Nothing fancy, just some checkboxes named Checkbox[]

The form has Server Side validation on it.

How easy (or not) is it to get these repopulated should the validation of the form fail?

Is it a quick task or slightly more involved?

Cheers
Ian

Sign in to reply to this post

Jason ByrnesWebAssist

it's not a quick task, it is definitely more involved and requires hand coding.

first, a multiple checkbox like this will pass the selected values using an array. to validate the checkboxes, the array needs to be converted to a comma separated string.

add the following code at line 1 to do that:

php:
<?php

if($_SERVER["REQUEST_METHOD"] == "POST")     {
    foreach(
$_POST as $key => $val) {
        if(
is_array($_POST[$key])) $_POST[$key] = implode(", "$_POST[$key]);
    }
}
?>



when validation fails, the validated entries code for the checkbox (yours may look different):

php:
<?php echo(ValidatedField("pagename","Checkbox")) ?>




will give us a comma separated list of the checked items, we need to convert that back to an array by adding the following code just before the form tag

php:
<?php $selected explode(", "ValidatedField("untitled4","Checkbox")); ?>




then in each checkbox, we can use the in_array function to see if that checkbox value is in the array, for example:

php:
<input type="checkbox" name="Checkbox[]" value="1" <?php if(in_array("1"$selected)) echo("checked=\"checked\""); ?> id="CheckboxGroup1_0" />

      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="Checkbox[]" value="2" <?php if(in_array("2"$selected)) echo("checked=\"checked\""); ?> id="CheckboxGroup1_1" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="Checkbox[]" value="3" <?php if(in_array("3"$selected)) echo("checked=\"checked\""); ?> id="CheckboxGroup1_2" />
Sign in to reply to this post

Ian S

This post has been deleted.

Ian S

Hi Jason,

I just wanted to let you know that thanks to your clear explanation and helpful code snippets that I was able to resolve my issue very quickly indeed!

Thanks as always!

Cheers
Ian

Sign in to reply to this post

Jason ByrnesWebAssist

glad to hear it is working.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...