homepage Welcome to WebmasterWorld Guest from 54.226.191.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
making dynamically displayed checkboxes sticky
knotworking




msg:1251894
 4:14 pm on Jan 7, 2005 (gmt 0)

I am stuck on this last feature of my form before it is published and, I am now even wondering if this is possible:

I have an array (thanks to the help of members here):

$experiences = array('National', 'Regional', 'Market', 'Retail');

that I call as checkboxes (there are actually a few dozen more than the four I listed above):

$cols=3;
$cells=0;
$td='';
for ($i = 0; $i < count($experiences); $i++){
$cells++;
$td.= "\n\t<td width= \"33%\" align=\"left\">&nbsp;<input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"$checked> $experiences[$i]</td>";
if($cells==$cols)
{
echo "\n<tr> $td \n</tr>";
$cells=0;
$td="";
}
}

Now, I tried numerous ways to make these checkboxes stay "checked" if there is an error and the form is redisplayed to the user for correction. I have found examples of how to do this if the checkboxes are created naturally through html but, nowhere can I find info on how to do this when they are dynamic.

Anyone know how to do this?

 

mavherick




msg:1251895
 6:46 pm on Jan 7, 2005 (gmt 0)

Assuming the script displaying the form and validating it is the same file, this might work:

just after this line:

$cells++;

try this:

$checked = (isset($_POST[$experiences[$i]]) && $_POST[$experiences[$i]])?" checked":"";

I can't test the syntax right now, so you may have to play around with it a little.

Hope that helps.

mavherick

knotworking




msg:1251896
 7:24 pm on Jan 7, 2005 (gmt 0)

Thanks but, no dice on that bit of code, mavherick.(it didn't cause any errors though, which is a step forward from what I was trying)

Salsa




msg:1251897
 7:49 pm on Jan 7, 2005 (gmt 0)

Hi Knotworking. It looks like you're making progress with your form.

Mavherick's suggestion should work for restoring checks when restoring the form after it has been submitted with errors, however, as I recall, for returning visitors, you're getting the experiences values from a database, and Mavherick's code alone would overwrite that, unchecking all the boxes when the form is displayed for the first time. So, you'll need to include a condition based on where the checks are coming from; the database or the form?

There are several ways you could do this, but maybe the simplest would be to add a hidden form field with a name/value pair something like: checks_from_form="1". Then, to make sure the $checks_from_form variable isn't unset when you initially query the database, in that case set $checks_from_form = "0".

Finally, in your for loop, do something like:

if ($checks_from_form == 1) // use Mavherick's code
else // use values from the database query

I hope this helps.

Salsa




msg:1251898
 8:35 pm on Jan 7, 2005 (gmt 0)

...As an after-thought. Of course, you'll want to put the querying of the database in the same type of condition. If it's currently a default part of displaying the form, the form's values will be reset to the values in the database (or be reset to blank if there is no corresponding db record) each time the form has to be redisplayed.

Then, putting the database query in a condition like that will cause another problem: Only the $experiences that are checked will be returned from the form. So, if, for example, only two experiences are checked, when you run your for loop under the condition that $i < count($experiences), count($experiences) will only equal two, which won't represent the form the way you want it. So, when you initially query the database, you'll probably, also need to create an array of all experiences that you can pass back and forth in the form, too, and use the count of that array in the count condition of your for loop.

I hope this helps.

knotworking




msg:1251899
 4:12 am on Jan 8, 2005 (gmt 0)

Howdy Salsa.

Yes, it has nearly reached adulthood, this script of mine. This and 2-3 more features are all it needs. I did go ahead and define $experiences as an array in a config file that I include with my forms.

I went back and tinkered with mavherick's code abit but, still no dice. My host has global registers on so, I manipulated the code to accomidate for that (I've been meaning to turn it off for my site but, that would mean updating scripts for several days), here's what I currently have that's returning all values checked regarless of user input:

<?
$cols=3;
$cells=0;
$td='';
for ($i = 0; $i < count($experiences); $i++){
$cells++;
$checked = (isset($experiences[$i]) && $experiences[$i])?" checked":"";
$td.= "\n\t<td width= \"33%\" align=\"left\">&nbsp;<input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"$checked> $experiences[$i]</td>";
if($cells==$cols)
{
echo "\n<tr> $td \n</tr>";
$cells=0;
$td="";
}
}
?>

Also Salsa, I am not using the same script for new registration and modifying profiles so, no need to specify where the checked values are coming from. As an aside that probably has no relevance, I am making my other fields sticky using this method with success:

<input type="text" name="first_name" size="20" value="<?php print $first_name;?>" maxlength="30" >

Salsa




msg:1251900
 11:14 am on Jan 8, 2005 (gmt 0)

I think the problem is that you are using the same variable name to hold both the name of the experience and whether or not it's checked. When you include the array from your config file, that sets all the values so that each one passes the isset test in the for loop. You could just change the checkbox names in the forms to something like $experiences_checked[]--or make them singular--so the two arrays don't conflict.

But, because you are using value attributes in the form fields that are the same as the array members, a simpler way might be to leave the names the same, and use a different test in your for loop, like:

for ($i = 0; $i < count($experiences); $i++){
$cells++;
$checked = in_array($experience[$i],$_POST['experiences'])? " checked" : "";
...

In this case, whether global variables are on or off, you'll need to use the $_POST['experiences'] array--and don't extract its members--because that's a completely different array from $experiences, and you'll want to keep it that way.

I hope this one does it for you.

knotworking




msg:1251901
 3:31 pm on Jan 9, 2005 (gmt 0)

Still no luck with getting them to stick. The code you gave me is displaying this error when the page loads:

Warning: in_array(): Wrong datatype for second argument

I went ahead and submitted anyways to see if it would work but, it didn't. I see what you are getting at with the line, I'm tinkering with it hoping for some dumb luck.

knotworking




msg:1251902
 7:12 pm on Jan 9, 2005 (gmt 0)

I've got it working now (thanks to another board poster). Here it is, for posterity:

foreach ( $experiences AS $value )
{
echo '<td width="33%" align="left">';
echo '<input type="checkbox" name="experiences[]" value="' .$value. '"';
echo ( in_array($value, $_POST['experiences'] ) )? ' CHECKED ' : '';
echo ">$value</input>";
echo '</td>';
}

I'm still getting that:

Warning: in_array(): Wrong datatype for second argument

But,I'll probably just suppress that.

Salsa




msg:1251903
 8:25 pm on Jan 9, 2005 (gmt 0)

I'm glad you got it working. The error message is PHP complaining that the second argument isn't an array. Do you always get the error or only when you initially open the form? When you initially open the form, whether you are populating it from a database query for a previous user, or presenting a blank form for a new user, there won't be a $_POST['experiences'] array, and you'll get a message like that. Even if the form has been submitted, but none of the experiences boxes has been checked, there won't be a $_POST['experiences'] array.

Rather than supressing the error, it would be better to provide for all three conditions for presenting the form, like:

if($case=='validating') checked = in_array($value,$_POST['experiences'])? ' CHECKED':''; 
elseif($case=='db_query') checked = in_array($value,$db_experiences)? ' CHECKED':'';
else checked = "";

...this assumes you've set a '$case' variable based on which phase of processing the form you are in--including checking to see if any boxes have been previously checked--and the $db_experiences can be whatever you name the array from db queries.

knotworking




msg:1251904
 10:48 pm on Jan 9, 2005 (gmt 0)

Here's what I used (since this script will only serve as an initial registration form, the modify profile is another seperate script):

$_POST['experiences'] = ( count($_POST['experiences']) >1 )? $_POST['experiences'] : array();

seems to work. Much thanks for helping me get through my trouble spots.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved