0

i am trying to display variable via session to the another page to whom i send it via action in form. i am using session inside if(isset($_POST['submit'])) seems its not working. here is my form.php code

<!DOCTYPE html>
<html >
<head>
<meta  content="text/html; charset=utf-8" />
<title>form</title>
</head>

<body><form action="submit.php" method="post">
<input type="text" name="username" />
<input type="submit" name="submit" value="submit" />

</form>
</body>
</html>

<?php
session_start();
if(isset($_POST['submit'])){
$username=$_POST['username'];
if(!empty($username)){
//echo $username;
 $_SESSION['username']=$username;

 echo 'session is set';
  }

 }
?>

here is my submit.php code

<?php

  session_start();
  //var_dump($_SESSION);

   echo $_SESSION['username'];
  ?>

i tried all possible way, not working. i want this way for submitting. thankyou in advance.

amit
  • 53
  • 1
  • 1
  • 11
  • 1
    Can you explain what do you mean by it's not working? in the second file you are simply doing `$_SESSION['username']`... which will do nothing. – Spoody Apr 13 '18 at 18:25
  • 4
    `session_start` must start before you outputting something. In your case your html document – Sysix Apr 13 '18 at 18:27
  • 1
    you forgot an echo in front of `$_SESSION['username'];` – sietse85 Apr 13 '18 at 18:28
  • @Sysix That's not true. – Daan Apr 13 '18 at 18:28
  • echoed, working ! – amit Apr 13 '18 at 18:28
  • 1
    to bad i am not getting 15 points for this hahaha – sietse85 Apr 13 '18 at 18:29
  • thnkyou, sorry my bad! – amit Apr 13 '18 at 18:30
  • LOL @sietse85 ... all that effort coming here and asking a question, because he forgot `echo` ... *sigh* ... long road ahead ... looong road ahead. – IncredibleHat Apr 13 '18 at 18:30
  • We all started somewhere didnt we – sietse85 Apr 13 '18 at 18:30
  • @amit What was the mistake the `echo` or the position of `session_start`? – Ermac Apr 13 '18 at 18:31
  • @ankabout the position of `session_start()` does not need to be in front of any output. – Daan Apr 13 '18 at 18:31
  • earlier it was not working even after echoing. i was not using (!empty()) and while debugging process i did that mistake so so sorry . – amit Apr 13 '18 at 18:32
  • i was stuck here since last time had terrible headache though i was feeling ashamed to ask. i had no choice. sorry – amit Apr 13 '18 at 18:36
  • 1
    @Daan Well I think it needs as said Sysix, since it is clearly said in the documentation "To use cookie-based sessions, session_start() must be called before outputing anything to the browser." The reason is also outlined "This function sends out several HTTP headers depending on the configuration. See session_cache_limiter() to customize these headers." and headers must be sent before any content as I remember. – Ermac Apr 13 '18 at 18:36
  • @amit I think no shame to ask but try to read the PHP doc as much as you can and train coding as much as you can to get better in programming. – Ermac Apr 13 '18 at 18:39
  • @MehdiBounya sorry to bother you again, yesterday during debugging i had session set so it worked. now i have edited it. actually i am trying is execute php code and form which sets action to another page, you may say do php code on other page but can we do this on the same page. – amit Apr 15 '18 at 17:33
  • @amit If it's a different question, Open a new one. Include: the code - the problem - the expected result - any errors (make sure errors display is enabled) - most importantly, look for already open questions. – Spoody Apr 15 '18 at 17:35
  • thank you for responding, its same question yesterday i missed echo and when you said to echo it, i did it worked because i had session set for that echoing. now again i am getting error index username not found. what i am doing here is doing php on same page where form send action to another page. – amit Apr 15 '18 at 17:44
  • Check [this](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Spoody Apr 15 '18 at 17:54

1 Answers1

0

Brother your Data Submit code is written in form.php and you're passing "submit.php" as action. So pass action="form.php"

<!DOCTYPE html>
<html >
<head>
    <meta  content="text/html; charset=utf-8" />
    <title>form</title>
</head>
<body>
    <form action="form.php" method="post">
        <input type="text" name="username" />
        <input type="submit" name="submit" value="submit" />
    </form>
</body>
</html>
<?php
    session_start();

    if(isset($_POST['submit'])){
        $username=$_POST['username'];
        if(!empty($username)){
            $_SESSION['username']=$username;
            echo 'session is set';
            //It will redirect to submit.php
            header("Location:submit.php");
        }
    }
?>
Nirav Chavda
  • 111
  • 3
  • 9