Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use reset button to reset an array

Status
Not open for further replies.

RPGguy

Programmer
Jul 27, 2001
73
US
I was given the following code by the great JPADIE. It loads column names in an array then displays a checkbox and column name on the form for each occurance. I would like to add a 'reset' button that will remove any check in a box and redisplay the column names and empty boxes. As always, thanks in advance for your help.

$result = mysql_query("Show fields from e107_act_emps") or die (mysql_error());
$checkboxes = "";
while ($row = mysql_fetch_assoc($result)):
$fields[] = $row['Field'];
endwhile;
foreach ($fields as $field):
if(isset($_SESSION['cb_fields'])):
$checked = in_array($field, $_SESSION['cb_fields']) ? "checked" : "";
else:
$checked="";
endif;

$checkboxes .= <<<EOF
<span class="wrapper" style="float:left;width:14em;">
<span class="cbox" >
<input $checked type="checkbox" name="cb_fields[]" value="$field" />
</span>
<span class="fld" >
$field
</span>
</span>

EOF;
endforeach;
 
so to paraphrase, you want a button that will uncheck any checkbox that has been checked by the user?

if so, just wrap the ouput in <form> tags and before the closing form tag add the following:

Code:
<input type="reset" name="reset" value="reset checkboxes" />
 
I added it just after my submit button and while it may clear the checked array it didn't redisplay the form so the boxes still appear checked.

<input type="submit" name="submit" value="Next" />
<input type="reset" name="reset" value="Reset" />
 
it will clear any changes that have been made by the user. are you looking to force all the checkboxes to unchecked irrespective of their starting state? if so, then you could do it without a refresh by using javascript.

if you want to have a refresh then change the code above thus

Code:
$result = mysql_query("Show fields from e107_act_emps") or die (mysql_error());
$checkboxes = "";
while ($row = mysql_fetch_assoc($result)):
    $fields[] = $row['Field'];
endwhile;
foreach ($fields as $field):
        if(isset($_SESSION['cb_fields']  [red] && $_POST['submit'] !== "Reset" [/red])):
            $checked = in_array($field, $_SESSION['cb_fields']) ? "[red]checked=\"checked\"[/red]" : "";
        else:
            $checked="";
        endif;
    
        $checkboxes .= <<<EOF
<span class="wrapper" style="float:left;width:14em;">
 <span class="cbox" >
   <input $checked type="checkbox" name="cb_fields[]" value="$field" />
 </span>
 <span class="fld" >
  $field
 </span>
</span>

EOF;
endforeach;

and add that button
Code:
<input type="submit" name="submit" value="Reset" />
 
Got it. Thanks again. If this is your hobby you must be one hell of a lawyer.
 
;-) thanks!

i'm a technology lawyer - part of my 'value' is being able to talk to the technical teams inside organisations to make sure that things like functional specifications etc are drafted in such a way so as to be intergratable into the development/outsourcing contracts etc. so often the commercial/procurement people don't understand the value of defining what it is they are buying. seems such a little thing but.... it's kept me employed for 15 years or more!
 
I couldn't agree more. I've been in IT for 28 years and it's amazing how the people making the decisions don't know what questions to ask or know if they are being BS'd. My company is currently installing Compudigm/Terradata and no one really knows what we've bought.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top