Jump to content

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"' : ''; ?>>

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.