Using isset or is not set with variable in PHP?

20,339

Solution 1

Working code and explanation:

<?php
    $test="";
    if (isset($_POST["text"])) { //always directly check $_POST,$_GET var without assigning
        echo "This var is set";
        $test=$_POST["text"]; // then assign
    }
    else{ // and use else clause for otherwise case
        echo "This var is not set";
        $test = 'set';  // AND if you want set to default/custom value in case of not set.
    }
?>
<form action="" method="post">
    <input type="text" id="text" name="text" autocomplete="off">
    <br /><br />
    <input type="submit" value="submit">
</form>

Solution 2

If you are using form to submit values, then try this one,

if (isset($_POST['text'])) {
    echo "This var is set";
}

if (!isset($_POST['text'])) {
    echo "This var is not set";
    $test = 'set';  
}

Otherwise, If a variable set to empty value like $test = ''; (It means variable is set but it has no values) It will execute your first if condition only.

Share:
20,339
bobbyjane
Author by

bobbyjane

Updated on July 09, 2022

Comments

  • bobbyjane
    bobbyjane almost 2 years

    I have a form which submits data, and as a test I am attempting to first check if a variable is set on submit. If the variable is set, a text will be displayed stating something like "Variable is set". However if it isn't set, a text will be displayed saying "variable not set", once the variable hasn't been set it should then be set so next time when the form is submitted it displays variable is set however this is not working for me, for some reason it always displays that the variable is not set, my PHP code will be below:

    <?php
    if (isset($test)) {
        echo "This var is set";
    }
    
    if (!isset($test)) {
        echo "This var is not set";
        $test = 'set';  
    }
    ?>
                <form action="" method="post">
                    <input type="text" id="text" name="text" autocomplete="off"><br><br>
                    <input type="submit" value="submit">
                </form>
    

    I feel really stupid for not being able to do something which seems so easy, I am only just learning and sort of trying to teach myself, thank you for any help provided!!!

    • kittykittybangbang
      kittykittybangbang about 9 years
      Can you post the HTML for the form please?
    • bobbyjane
      bobbyjane about 9 years
      @kittykittybangbang There you go, I just updated it!
    • Sagar
      Sagar about 9 years
      @bobbyjane check isset() and !empty() both in if statement and see.
    • Narendrasingh Sisodia
      Narendrasingh Sisodia about 9 years
      Really not clear about your question its bit confusing.
  • kittykittybangbang
    kittykittybangbang about 9 years
    OP's using text, not test as the name for the input. Also, you need quotes around the name for the $_POST array key ($_POST['text'], not $_POST[test]).
  • Vignesh Bala
    Vignesh Bala about 9 years
    oh sorry I did there some spell mistake @kittykittybangbang
  • Blue
    Blue about 9 years
    Having no action in your form will tell the form to redirect to itself. See this post for more info.
  • Wh1T3h4Ck5
    Wh1T3h4Ck5 about 9 years
    "first check, than use" is never bad logic ;)
  • scrowler
    scrowler about 9 years
    Bad example. You're better to do the is set check on the original post variable not the variable you assign it to
  • kittykittybangbang
    kittykittybangbang about 9 years
    @scrowler Thanks for the input; edited to reflect your suggestion.