lesliesoon Posted July 9, 2012 Share Posted July 9, 2012 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 "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> '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/ Share on other sites More sharing options...
Drummin Posted July 9, 2012 Share Posted July 9, 2012 <?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> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/#findComment-1360211 Share on other sites More sharing options...
lesliesoon Posted July 9, 2012 Author Share Posted July 9, 2012 it still doesnt work :/ its the same as before when i refresh or submit the page the checkbox unchecks byitself when i go back to the page no errors though. Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/#findComment-1360215 Share on other sites More sharing options...
Drummin Posted July 9, 2012 Share Posted July 9, 2012 Why not give your checkboxes a more reliable name like Chemicals? It's that the value you are sending anyway? name="Chemicals[]" Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/#findComment-1360217 Share on other sites More sharing options...
lesliesoon Posted July 9, 2012 Author Share Posted July 9, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/#findComment-1360218 Share on other sites More sharing options...
Drummin Posted July 9, 2012 Share Posted July 9, 2012 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" Quote Link to comment https://forums.phpfreaks.com/topic/265406-how-do-i-make-a-php-checkbox-stay-check-after-refreshing-submitting-page/#findComment-1360219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.