简体   繁体   中英

if (isset($_POST[“submit”]))

I am a completely new programmer and I am trying to develop a website in php. All I want to do in this part of the code is to "read" the user's inputs and save them as session variables in order to do/ calculate something else in another subpage. In order to see if everything is working fine I added the lines echo "Welcome ",$_SESSION["firstname"]; echo "ok" but it is not working. Can you help me? Here is my code:

<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
        <b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
        <b>Last Name:</b>  <input name="lastname" type="text" value=""></br></br>
        <b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
        <b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>

        </br></br>
        <input type="submit" name="submit" value="Submit">
</form

<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
echo "Welcome ".$_SESSION["firstname"];//here is....
echo "ok"
?>

</body>
</html>

That is actually working. If you look at the source code you will see the your name is echoed out.

One issue is you are missing a greater than sign at the end of your form tag.

</form

should be

</form>

Also, you need to start the session before any html. Try this...

<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
        <b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
        <b>Last Name:</b>  <input name="lastname" type="text" value=""></br></br>
        <b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
        <b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>

        </br></br>
        <input type="submit" name="submit" value="Submit">
</form>

<?php
if ($_SESSION["firstname"] != ""){
    echo "Welcome ",$_SESSION["firstname"];
    echo "ok";
}
?>

</body>
</html>

What session_start() does is sends a cookie in the page header when it's served to the browser. If you've already send some data to the browser, like your form, then PHP will not be able to start a session. You need to move session_atart() to the very top of your document, above the form or any echo s. Also, you're missing a semi-colon, as noted by others. Also, you need to properly close your <form> tag.

echo output inside if, delcare session start on top, tested and works 100%. Note- add php code on top and close form tag

<?php
session_start();  //session start must be first line

if (isset($_POST["submit"])){
    $_SESSION["firstname"]= $_POST["firstname"];
    $_SESSION["lastname"]= $_POST["lastname"];
    $_SESSION["age"]= $_POST["age"];
    $_SESSION["numberofpeopleinhousehold"]=  $_POST["numberofpeopleinhousehold"];
    echo "Welcome ".$_SESSION["firstname"];  //inside if condition
    echo "ok";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="#">
    <b>First Name:</b> <input name="firstname" type="text" value=""/></br></br>
    <b>Last Name:</b>  <input name="lastname" type="text" value=""/></br></br>
    <b>Age:</b> <input name="age" type="number" min="0" value=""/></br></br>
    <b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""/></br></br>

    </br></br>
    <input type="submit" name="submit" value="Submit"/>
</form>  <!-- close form tag -->

</body>
</html>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM