Jump to content

$_POST exists problem


dadamssg87

Recommended Posts

i'm trying to check if a checkbox should be checked or not. I want the box to be checked on initial page load. And then after the form is submitted check whether or not the user had it check it or not and get it to remember. Oh and i have the checkbox's value to be "yes" but i've learned the $_POST value for this input doesn't even exist if the user submits the form without the box checked. This loads the form initially with it unchecked(not what i want) but it does remember what the user inputted if there is an error in the form, unchecked or checked so thats good.

 

<?php
    if(isset($_POST['length']) && $_POST['length'] == "yes") 
    {
    	$values['length'] = TRUE;
    }
	elseif(!isset($_POST['length']))
	{
		$values['length'] = FALSE;
	}
    else
    {
		$values['length'] = TRUE;
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/240907-_post-exists-problem/
Share on other sites

If I've understood what you're trying to achieve, here's a way that would work for you.

 

<input type="checkbox" name="my_checkbox" value="Yes" <?php echo ( isset($_POST['my_checkbox']) && $_POST['my_checkbox'] == 'Yes' ) ||  strtolower($_SERVER['REQUEST_METHOD']) !== 'post' ? 'checked="checked"' : ''; ?>>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.