0

i'm new in coding and have a problem coding on Mac.

When i do var_dump($_POST), answer is: array(0){}

Here is the code:

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name=strip_tags($_POST["name"]);
    $age=$_POST["age"]*1;
    $_SESSION["name"] = $name;
    $_SESSION["age"] = $age;
}
else {
    $name = $_SESSION["name"];
    $age = $_SESSION["age"];
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Session demonstration</title>
</head>

<body>
<h1>Session demonstration</h1>
<a href="session-2.php">Demo session</a><br>
<a href="session_destroy.php">Close session</a><br><br>
<form action="<?=$_SERVER["PHP_SELF"]?>"
      method="post">
    Your name is:
    <input type="text" name="name" value="<?php echo $name?>"><br>
    You are:
    <input type="text" name="age" value="<?php echo $age?>"><br>
    <input type="submit" value="Submit">
</form>
<?php
if ($name and $age) {   
    if ($name and $age) {
        echo "<h1>Hello, $name</h1>";
        echo "<h3>You are $age</h3>";
    }
    else {
        print "<h3>Bye!</h3>";
    }
}
?>
</body>
</html>

Same time, when i press submit button it shows me:

Notice: Undefined index: name in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 5

Notice: Undefined index: age in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 6

Maybe someone knows what can i do.

Sincerelly

9
  • add method=post in your form element Commented Oct 7, 2016 at 17:58
  • But it is there ... Commented Oct 7, 2016 at 18:01
  • @jevgeni_serstjukov Post is there and nothing is wrong with it. What is the purpose of adding two if condition. Secondly, you get those errors, when the session is empty. Commented Oct 7, 2016 at 18:05
  • Maybe instead of this if ($_SERVER["REQUEST_METHOD"] == "POST") you should use something like if(isset($_POST["name"])) Commented Oct 7, 2016 at 18:07
  • sorry my bad the problem with this is your session variables are not set $_SESSION["name"] and $_SESSION["age"] Commented Oct 7, 2016 at 18:09

1 Answer 1

1

the problem with this is your session variables are not set at the beginning and you are trying to assign your $name and $age with those unassigned variables on your else block. Validate your assignments like this.

<?php
session_start();
$name = $age = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = strip_tags($_POST["name"]);
    $age = $_POST["age"] * 1;
    $_SESSION["name"] = $name;
    $_SESSION["age"] = $age;
} elseif (isset($_SESSION['name']) || isset($_SESSION['age'])) {
    if (isset($_SESSION['name'])) {
        $name = $_SESSION['name'];
    }
    if (isset($_SESSION['age'])) {
        $age = $_SESSION['age'];
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
    <head>
        <title>Session demonstration</title>
    </head>

    <body>
        <h1>Session demonstration</h1>
        <a href="session-2.php">Demo session</a><br>
        <a href="session_destroy.php">Close session</a><br><br>
        <form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
            Your name is:
            <input type="text" name="name" value="<?php echo $name ?>"><br>
            You are:
            <input type="text" name="age" value="<?php echo $age ?>"><br>
            <input type="submit" value="Submit">
        </form>
<?php

    if ($name and $age) {
        echo "<h1>Hello, $name</h1>";
        echo "<h3>You are $age</h3>";
    } else {
        print "<h3>Bye!</h3>";
    }
?>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

10 Comments

Just tried. But after var_dump($_post) = array(0){}
There was some errors before try it now. And var_dump($_POST) UPPER CASE and var_dump after you submit the form.
after i submit the form it gives me: Notice: Undefined index: name in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 5 Notice: Undefined index: age in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 6
Do you have $name = $age = ""; on your third line??
yes i do. And the same thing. Maybe something is with my php.ini?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.