Session loses its data or value after form submitting in php

BrightSmileNewbieProgrammer12

Greeting Senior and junior developers, I am new to php. Currently, I am facing a problem related to session. But before I describe my problem, I have tried other solutions as well and it didn't work. So here is my problem : I have two php files s1 and s2.php When I enter my name into the inputbox and click the submit button, I want my name to be displayed on another page which is s2.php. I want to achieve the above result by using session. What I have done is I wrote a conditional statement to cheak if the button has been submited and if it does, then assign the value I put in the input box in the url to my session variable. Then I defined session start on top of my html document. I have done the same to the s2 page and I tried to echo the session variable on the page but it displays nothing. Then I tried to cheak if the session is set or not and I have found that it has not been set yet.

So How to solve the problem? Why my session has not been set? If I tried without submitting form, it works fine. here is my s1.php code:

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
        <form action="s2.php" method="get">
            <input type="text" name="username">
            <input type="submit" name="submit">
        </form>
    

    <?php
        
            if(isset($_GET['submit'])){
                $_SESSION['name']=$_GET['username'];
            }
            
        
        
      
    ?>
</body>
</html>

here is my s2.php code:

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    

        if(isset($_SESSION['name'])){
            echo "it set"."<br>";
            echo $_SESSION['name'];
        }else{
            echo "it not set";
        }
    ?>
</body>
</html>
Muhammad Ali Abed

s1.php is processed on the server & there your condition is false so an html form will be back & you see it in the browser. when you submit the form, form data will be sent to s2.php. there you're checking if session is set, tell us & show the content of session['name'].

so basically the problem is that your setter snippet at s1.php does not get the change at all to set session['name']. it is in the wrong place. like what Robert said move

if(isset($_GET['submit'])){
   $_SESSION['name']=$_GET['username'];
}

to s2.php before checking to see if session['name'] is set

& if you have nothing else to do with sessions in s1.php, you can remove

<?php
session_start();
?>

from the head of s1.php

best regards :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Variable loses its value PHP

Where form fields data are stored after submitting a FORM in PHP?

How to load table data after submitting the form in react js php

Submitting form using selection value with form data

Symfony 3: set value after submitting a form

Django Form Field Not Setting Value After Submitting

Get the check box value after submitting a form

form in php not redirecting to page after submitting

Trying to redirect in PHP after submitting an email form

How to redirect after submitting form in php

PHP - $_POST variable is not set after form submitting

Echo data in same form after submitting

nodemon app crashed after submitting form data

PHP AJAX - not getting value on form submitting

After submitting form its showing success message on different page

session variable loses value after detailsview new cancel event

Paypal loses session data

Submitting form data with php with no page reload?

Global Variable loses its value after page index changed in DataGrid

Unable to hold form 1 value after submitting form 2

Storing form data in session with php

Form clears after submitting

store form value into session after submit and save until form submit again: PHP

Global variable loses its value after providing its value in a function.Unable to access its value outside that function

React Router redirect after submitting a form using the input value

how to instantly load a list of options after submitting a value in a django form

Keep $_GET value of a drop down menu after submitting the form

How to compare value of the form before and after submitting in Symfony?

Javascript variable loses its value