dalawh Posted June 8, 2012 Share Posted June 8, 2012 <form name="form" action="enter.php" method="post"> First Name: <input type="text" name="first" /><br /> Last Name: <input type="text" name="last" /><br /> <input type="checkbox" name="c1" value="1" onclick="showSubmit()" /> <input type="checkbox" name="c1" value="1" onclick="showSubmit()" /> <input type="checkbox" name="c3" value="1" onclick="showSubmit()" /> <!-- SUBMIT BUTTON HERE --> </form> If I only check the 1st and the 3rd checkbox and press the submit button, but in my enter.php, I made it check print out each of the inputs like so: <?php echo $_POST["first"];echo $_POST["last"];echo $_POST["c1"]; echo $_POST["c2"]; echo $_POST["c3"];?> I will get a warning/error claiming that the c2 is undefined. Is there a built in function that checks if something is undefined ? That way I can use a if and else statement: if is it undefined, it will print something, and if it is checked (defined), make it print something else. Link to comment Share on other sites More sharing options...
Guest So Called Posted June 8, 2012 Share Posted June 8, 2012 (edited) http://php.net/manua...ction.isset.php if (isset($_POST["c2"])) $c2 = $_POST["c2"];else $c2 = "";echo $c2; or echo isset($_POST["c2"]) ? $_POST["c2"] : ""; or $c2 = isset($_POST["c2"]) ? $_POST["c2"] : "";echo $c2; etc. Edited June 8, 2012 by So Called Link to comment Share on other sites More sharing options...
dalawh Posted June 8, 2012 Author Share Posted June 8, 2012 http://php.net/manua...ction.isset.php if (isset($_POST["c2"])) $c2 = $_POST["c2"];else $c2 = "";echo $c2; or echo isset($_POST["c2"]) ? $_POST["c2"] : ""; or $c2 = isset($_POST["c2"]) ? $_POST["c2"] : "";echo $c2; etc. Thanks a lot, that worked. I read somewhere that isset only works for things set to null, did not know undefined and null where the same. Link to comment Share on other sites More sharing options...
Guest So Called Posted June 8, 2012 Share Posted June 8, 2012 I think eventually everybody who uses $_POST discovers that you need isset() to prevent undefined index errors. Today it's your day! Link to comment Share on other sites More sharing options...
niche Posted June 8, 2012 Share Posted June 8, 2012 (edited) Don't forget to wrap your inputs in a block element. http://w3schools.invisionzone.com/index.php?showtopic=43842&pid=243236&st=0entry243236 Edited June 8, 2012 by niche Link to comment Share on other sites More sharing options...
justsomeguy Posted June 8, 2012 Share Posted June 8, 2012 They aren't the same. Null is a special value in PHP that indicates that a variable has no value. Undefined means the variable does not even exist at all. Undefined is not a value in PHP, null is a value. Undefined is a value in other languages, like Javascript, but not PHP. The error message you were getting was telling you that you are trying to access an index of an array ($_POST) that is not defined, not that the index exists and has a null value. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 8, 2012 Share Posted June 8, 2012 (edited) I read somewhere that isset only works for things set to null, did not know undefined and null where the same.They aren't really the same. For example:$varOne = null;Technically, $varOne is set, but it is set to null. $varTwo is not set at all. The isset function just considers null to be the same as not set. EDIT: Too slow, JSG beat me... Edited June 8, 2012 by ShadowMage Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now