2

I am trying to get data from a form and send it to another page using sessions only. Without using post

I have read this but if i put the page name in the action atribute, the script is not executed, and the action is always performed first when the button is pushed.

Here is my solution

<html>
<body>
   <h3>a) Inserir uma nova pagina: </h3>
        <form action="" method="post">
            <p>userid: <input type="text" name="input_userid"/></p>                
            <p>Nome de Nova Pagina <input type="text" name="input_nova_pagina"/></p>
            <p><input type="submit" name="Submit" value="Adicionar nova pagina!"/></p>

            <?php
            session_start();
            if (isset($_POST['Submit'])) { 
                 $_SESSION['userid'] = $_POST['input_userid'];
                 $_SESSION['nova_pagina'] = $_POST['input_nova_pagina'];
                 header('Location: /xampp/Aptana/BDproj2/addp.php');
             } 
            ?>
        </form>
</body>

The Second page is:

<?php
  session_start();
 ?>

and this

<html>
<body>
<?php
    echo "Favorite color is " . $_SESSION["userid"] . ".<br>";
    echo " nome da pag : " . $_SESSION["nova_pagina"];
    //$userid = $_REQUEST['input_userid'];
    //$nova_pagina = $_REQUEST['input_nova_pagina'];

?>
</body>

Is there a better way to do what i want? i hope i was clear.

5
  • 3
    that is because you are outputting before header my friend Commented Feb 1, 2016 at 22:19
  • 2
    possible duplicate of stackoverflow.com/questions/8028957/… Commented Feb 1, 2016 at 22:19
  • Before i had the session_start right at the begining but it didn't make it work either. I don't have any error with the code i showed. i think that0s what you ment? Commented Feb 1, 2016 at 22:42
  • 2
    Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. If you see headers sent, you'll know what to go after. Commented Feb 1, 2016 at 22:51
  • Tried that with the original version without the header instructions, with the php session at the top and with the action="addp.php" but it said the same error when jumping to the second page trough the action: Undefined variable: _SESSION Commented Feb 1, 2016 at 23:12

1 Answer 1

3

I hope I got it this time :). I've deleted the previous answer. It's just a matter of having action = "current_page.php". Mine was php_sessions.php. If this isn't what you are looking for then you should try jquery or ajax....

FIRST PAGE (named php_sessions.php):

 <?php
        ini_set('display_errors', 1);
        error_reporting(E_ALL); 
        session_start();

         if (isset($_POST['Submit'])) { 
             $_SESSION['userid'] = $_POST['input_userid'];
             $_SESSION['nova_pagina'] = $_POST['input_nova_pagina'];
             header('Location: xampp/Aptana/BDproj2/addp.php');
         } 

    ?>    
    <html>
        <body>
           <h3>a) Inserir uma nova pagina: </h3>
                <form action="php_sessions.php" method="post">
                    <p>userid: <input type="text" name="input_userid"/></p>                
                    <p>Nome de Nova Pagina <input type="text" name="input_nova_pagina"/></p>
                    <p><input type="submit" name="Submit" value="Adicionar nova pagina!"/></p>
                </form>

        </body>
    </html>

SECOND PAGE xampp/Aptana/BDproj2/addp.php:

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL); 
    session_start();

?>

    <html>
        <body>
            <?php
                echo "Favorite color is " . $_SESSION["userid"] . ".<br>";
                echo " nome da pag : " . $_SESSION["nova_pagina"];
                //$userid = $_REQUEST['input_userid'];
                //$nova_pagina = $_REQUEST['input_nova_pagina'];

            ?>
        </body>
    </html>
Sign up to request clarification or add additional context in comments.

12 Comments

your second page: will fail. Their error is obvious.
have you tested it? :) this works for me as long as the second page is "addp.php" and it's relative path is "xampp/Aptana/BDproj2/". I tried to keep the original data.
you mean to tell me that your second page works without session_start();? I find that rather hard to believe.
Yes, I presume that when I started the session in the first page a cookie file has been created, so the second page will have a session saved and there won't be any errors. I hope it's not because I've tested this in cloud9.
OP is outputting before header. That's why their code isn't working.
|

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.