3

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!!!

andih
  • 5,570
  • 3
  • 26
  • 36
bobbyjane
  • 43
  • 1
  • 1
  • 4

4 Answers4

4

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>
Adarsh Rajput
  • 1,246
  • 14
  • 24
3

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.

Vignesh Bala
  • 889
  • 6
  • 25
0

You haven't declared the variable $test.

Unless you've still got a bit of PHP somewhere that you haven't included here, your variable is empty. When a form is submitted, the input will be added to either the $_POST array (for method = "post") or else the $_GET array (for method = "get").

To Fix:

<?php 

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

if (!isset($_POST['text'])) {
    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>
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
  • Having no action in your form will tell the form to redirect to itself. See [this post](http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a) for more info. – Blue Jun 06 '15 at 06:07
  • Bad example. You're better to do the is set check on the original post variable not the variable you assign it to – scrowler Jun 10 '15 at 07:57
  • @scrowler Thanks for the input; edited to reflect your suggestion. – kittykittybangbang Jun 10 '15 at 13:58
0
<?php
$test=$_GET["text"];
if (isset($test)) {
    echo "This var is set";
}

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