Jump to content

How do i make a php checkbox stay check after refreshing / submitting page?


lesliesoon

Recommended Posts

Hello, i am quite new to php and i need some help in making my checkbox stay checked. I have been searching tutorials about checkboxes for hours but i still cant find one which fits my needs because none of them has a variable as a checkbox name. I will admit my friend helped me alot in the checkbox part but i still dont fully get it... would be great if someone was kind enough to explain them to me :)

 

Description

=======

Ok i am making a chemical printout.i have added a chemical list from the database and the idea is to print out the list of chemicals which i checked in the checkbox. The table currently is able to print out which ever checkbox i have ticked but the problem is if i click on refresh or submit button, the checkbox automatically unchecked it. this is my current checkbox $count=0(initially).  ignore the sql connection and the search bar coding with it :P "SelectedChemicalsPrintingPage.php" is the other folder that i will be directed to after i clicked on submit. There, it will print out all the chemicals which i had checked.

<?php

error_reporting (0);

mysql_select_db("leslie", mysql_connect("localhost","root",""));

$result=mysql_query('select * from table2 WHERE Chemicals LIKE "%'.$_REQUEST['Search'].'%" '.$Controlled);

$count=0;

echo "<form action=SelectedChemicalsPrintingPage.php method=post>";

echo "

<table border='1'>

<tr>

<th>Select</th>

<th>Chemicals</th>

<th>Quantity</th>

<th>Location</th>

<th>Controlled</th>

</tr>

";

while($row = mysql_fetch_array($result))

{

echo "

  <tr>

  <td><input type=checkbox name=".$count." value=".$row[Chemicals]."></td>

  <td>" . $row[Chemicals] . "</td>

  <td>" . $row[Quantity] . "</td>

  <td>" . $row[Location] . "</td>

  <td>" . $row[Controlled] . "</td>

  </tr>

        ";

 

    if( isset($_POST[$count]) ) echo "checked='checked'"; <---i suppose this is placed wrongly?

$count++;

 

}

echo '</table>';

echo '

<input type=hidden name=counts value="'.$count.'">

<input type=submit name=submit >

</form>

        ';

?>

 

 

Link to comment
Share on other sites

<?php
while($row = mysql_fetch_array($result))
         {
	 $checked={isset($_POST[$count]) && $_POST[$count]==$row['Chemicals'] ? 'checked="checked"' : '');  
            echo "
               <tr>
               <td><input type=checkbox name=".$count." value=".$row['Chemicals']." $checked /></td>
               <td>" . $row['Chemicals'] . "</td>         
               <td>" . $row['Quantity'] . "</td>
               <td>" . $row['Location'] . "</td>
               <td>" . $row['Controlled'] . "</td>
               </tr>
                    ";
?>

Link to comment
Share on other sites

Yep i am sending the chemicals i ticked away to another page that will display the chemical's name, Location, Controlled, and Quantity, and at that page there is a back button to return to the selection page which i hope the check box will still be ticked. I also have a working search bar in which it will search for which ever chemicals i want by the name, with the checkbox at the side(also uncheck my checkbox when i click search). Lastly If i changed my checkbox name to something else could i still display out what i have ticked? i need to assign all the chemicals in the row with the value $count so i can display them without typing out all the 20+ checkboxes for each chemicals

Link to comment
Share on other sites

Anytime you wish to have "sticky posts", it's a good idea to post to the same page with processing at the top before <html>.  Any reason why your results can't be on the same page as the form?  As you're not POSTing back to the form, you can't really pick up the POST values can you.  You could set posted values to session, then compare session values to your query array for the selected options if the back button is pressed.

 

BTW, I didn't really notice you had type checkbox, in which case it should be selected="selected"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.