Jump to content

exiting an IF statement


xjermx

Recommended Posts

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

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();
}

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)

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.

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.

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.