I’m using PHP to show only selected records from a MySQL table. There’s a form with a submit button so the user can refine their search (“Show only records where fieldX = valueY”…)
To make the form sticky I’ve used this code:
<input type="checkbox" name="SBS" value="SBS" <?php if($_POST['SBS'] == 'SBS') echo'checked="checked" ' ?> />
The only problem is that I need the default value to be checked. I tried adding
$_POST['SBS'] = 'SBS';
at the top, but this obviously sets the variable every time the form is submitted, so I can’t turn it off!
Thanks
Don’t set
$_POST['SBS']. Let the submit of the form do that.If you want to default the checkbox to ticked, then you can use
$_SERVER['REQUEST_METHOD']to detect aGETor aPOST.This will use the
$_POSTif the form has been submitted, otherwise if it is aGET, it will default to checked.You could also use
$_REQUEST['SBS']instead of$_POST['SBS']to default the value from thePOSTor from the query string, via aGET.