Forum Moderators: coopster
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\"> <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?
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
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.
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.
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\"> <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" >
$experiences_checked[]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.
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.
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.
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.
$_POST['experiences'] = ( count($_POST['experiences']) >1 )? $_POST['experiences'] : array();
seems to work. Much thanks for helping me get through my trouble spots.