php - Save checked values on page refresh? -
ive saved form data ajax , php, reusing data database.
however way approaching different, there no database, insight great.
i emailing form data, data simple checkboxes, values either 0
or 1
. when user refreshes page id keep checked values.
i guess without database need use cookies, , way avoid cookies ajax , database (strictly logic, not sure if true), why asking, want simple solution.
form snippet:
<input name="sharks" type="hidden" value="0"> <input name="sharks" type="checkbox" value="1" id="sharks" '.$value ? ' checked="checked"' : ''.'>
the php part of input shaky, id question whether value 0 or 1, if 1 checked if 0 empty.
getting database easier not sure since there no database, im guessing cookies come place.
sorry if last part shaky im little unsure , dont know look.
using sessions:
session_start(); if(isset($_post['submit'])) { if(isset($_post['personalization_result'])) { $_session['value'] = $_post['personalization_result']; } else { $_session['value'] = ''; } }
form
<form action="<?php the_permalink(); ?>" method="post" id="question-form"> <input type="hidden" name="submit" value="1"> <?php if ($_session['value'] == 1) { $checked = 'checked="checked"'; } ?> <li> <input name="personalization_result[memory_0]" type="hidden" value="0"> <input name="personalization_result[memory_0]" type="checkbox" value="1" id="personalization_result_memory_0" <?php $checked ?> > </li> <li> <input name="personalization_result[memory_1]" type="hidden" value="0"> <input name="personalization_result[memory_1]" type="checkbox" value="1" id="personalization_result_memory_1" <?php $checked ?> > </li> <li> <input name="personalization_result[memory_2]" type="hidden" value="0"> <input name="personalization_result[memory_2]" type="checkbox" value="1" id="personalization_result_memory_2" <?php $checked ?> > </li>
this code stores data in session:
<?php session_start(); if(isset($_post['submit'])) { if(isset($_post['sharks'])) { $_session['value'] = $_post['sharks']; } else { $_session['value'] = ''; } } ?> <form action="" method="post"> <?php print '<input name="sharks" type="checkbox" value="1" id="sharks" '; if ($_session['value'] == 1) { print ' checked="checked"'; } print ">"; ?> <br> <input type="submit" name="submit" value="save" /> </form>
worked me, keeping checkbox checked after closed , opened browser again. after testing added rather complex if
avoid undefined variable notice. set part seems robust.
Comments
Post a Comment