xjermx Posted July 10, 2010 Share Posted July 10, 2010 Newbie question: I have the following code. <?php if (filter_has_var(INPUT_POST, "charname")) { function1(); } else { function2(); } function function1() { print "<h2>Function 1</h2>"; } function function2() { print "<h2>Function 2</h2>"; print <<< HERE <form method = "post" action = ""> Please enter a name for your new character: <input type="text" name="charname" value="" /> <form method = "post" action = ""> <input type="submit" value="Create" /> <br> HERE; print <<< HERE <form method = "post" action = "game_playmenu.php"> <input type="submit" value="Back to Game Menu" /> </form> HERE; } ?> What I'm trying to do here is this. Suppose you've arrived at the page and decide that you *don't* want to enter a character name, so you click the Back to Game Menu button.. What seems to happen is that it gives 'charname' a value (""), and then hits function1. I want it to just exit directly back to the game menu. How can I modify my existing code to do this? Thanks! Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/ Share on other sites More sharing options...
Pikachu2000 Posted July 10, 2010 Share Posted July 10, 2010 If I understand correctly, this should work. If the filter_has_var() function does anything other than see if the var has a value you'll need to reincorporate it, however. if ( strlen($_POST['charname']) < 1 ) { function1(); } else { function2(); } Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084097 Share on other sites More sharing options...
Wolphie Posted July 10, 2010 Share Posted July 10, 2010 If I understand correctly, this should work. if ( strlen($_POST['charname']) < 1 ) { function1(); } else { function2(); } Instead of using strlen() you should use empty(). Returns FALSE if var has a non-empty and non-zero value. The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) "0" (0 as a string) NULL FALSE array() (an empty array) var $var; (a variable declared, but without a value in a class) Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084099 Share on other sites More sharing options...
Pikachu2000 Posted July 10, 2010 Share Posted July 10, 2010 That would also work, unless 0 is a valid entry for that field . . . Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084100 Share on other sites More sharing options...
Wolphie Posted July 10, 2010 Share Posted July 10, 2010 I agree, although in the context it's being used, it's unlikely a character will be named 0. Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084102 Share on other sites More sharing options...
Pikachu2000 Posted July 10, 2010 Share Posted July 10, 2010 I hear ya, I was just sayin' . . . Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084104 Share on other sites More sharing options...
xjermx Posted July 11, 2010 Author Share Posted July 11, 2010 Thanks for the responses. I should provide more clarification, and there may be a completely different (better) way to do what I'm trying to do. When a user hits the page, there is no value set for "charname". So function2 is implemented. In function 2 there is an input box to enter a name (charname), and a submit box, but also a box to navigate away to another menu, in the event that the user changes their mind and doesn't want to put in a name. Normally when they DO enter a name, the program behaves normally, taking that value, and running function1, which is normally a mySQL insert or somesuch. However, what's happening is that when someone decides "Nah, I don't wanna put in a name", and hits the Back to Game Menu button, the POST data has a value for charname as "" (blank). It then gives that you function1 which throws the blank value into sql. What I'm really trying to do is simply create a "Nah I changed my mind, get me outta here" button. I know I could do this with a simple a href= link back to another page, but I wanted to do it with form stuff if possible. Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084435 Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2010 Share Posted July 11, 2010 Just noticed you also have nested and unclosed <form> tags in the HEREDOC structure. I'm thinking that a side effect of that is that since your "Back to Game" button is of the "submit" type, it is what's setting the $_POST['charname"] variable, since it's within the open <form> tags above it. Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084483 Share on other sites More sharing options...
xjermx Posted July 11, 2010 Author Share Posted July 11, 2010 OMG. Fixed. So simple. Thanks so much. Link to comment https://forums.phpfreaks.com/topic/207358-exiting-an-if-statement/#findComment-1084491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.