bobbyjane
bobbyjane

Reputation: 53

Using isset or is not set with variable in PHP?

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

Upvotes: 4

Views: 33517

Answers (4)

kittykittybangbang
kittykittybangbang

Reputation: 2400

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>

Upvotes: 0

sakshi
sakshi

Reputation: 1

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

Upvotes: 0

Adarsh Rajput
Adarsh Rajput

Reputation: 1276

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>

Upvotes: 4

Vignesh Bala
Vignesh Bala

Reputation: 919

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.

Upvotes: 4

Related Questions