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.

Community
  • 1
  • 1
Ibil
  • 21
  • 1
  • 3
  • 3
    that is because you are outputting before header my friend – Funk Forty Niner Feb 01 '16 at 22:19
  • 2
    possible duplicate of http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Funk Forty Niner Feb 01 '16 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? – Ibil Feb 01 '16 at 22:42
  • 2
    Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Feb 01 '16 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 – Ibil Feb 01 '16 at 23:12

1 Answers1

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>
  • your second page: will fail. Their error is obvious. – Funk Forty Niner Feb 01 '16 at 22:40
  • 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. – Bogdan Haidu Feb 01 '16 at 22:44
  • 2
    you mean to tell me that your second page works without `session_start();`? I find that rather hard to believe. – Funk Forty Niner Feb 01 '16 at 22:50
  • 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. – Bogdan Haidu Feb 01 '16 at 23:00
  • OP is outputting before header. That's why their code isn't working. – Funk Forty Niner Feb 01 '16 at 23:00
  • Yes that works. So the session has acess to previous element names on another page even if you go to another one? that's weird. But the point was to send the data only trough a session variable. This way a post is used. – Ibil Feb 01 '16 at 23:01
  • Anyway, it's an interesting case if the session_start() is written only on the second page then the first page won't have any memory of a Session, if the session_start() is written on the first page but not on the second, then it might happen that the session values on the first page will differ from the one on the second page. So, you're right, best practice is to put session_start () on both pages, so that the session variable will comunicate between pages. Thank you, for your observation. – Bogdan Haidu Feb 01 '16 at 23:18
  • This server may have session auto_start enabled. http://php.net/manual/en/session.configuration.php#ini.session.auto-start – Jeff Feb 01 '16 at 23:25
  • The new edit answer always stays in the first page and never goes to the second. (or maybe it goes to the second but goes back to the first too quickly). – Ibil Feb 02 '16 at 00:17
  • Did you checked if the second file path is correct or the action value to be the same with the first file name? If it still doesn't work and you really wan't to do this without using the action value from the form you might try jquery or ajax $.post. here is a test link https://demo-project-haidu-bogdan.c9.io/stackoverflow/New%20Folder/php_sessions.php – Bogdan Haidu Feb 02 '16 at 00:46
  • I re-checked it and it works now. Only needs a few namepath fixes. The header is just Location: addp.php I Assume it works because when the server receives the second get php_sessions.php due to the action atribute on the form, it runs the php code since it comes before the http tags and imediatly prepares the new addp.php page with the header command and doesn't even read the rest of the code. If what i said is right, then the code causes extra traffic having to request 3 pages in total instead of 2. Isn't it just better to do what i did initially? Thanks! – Ibil Feb 02 '16 at 19:00
  • Hi, I think that extra traffic shouldn't be a problem. Personally, where I still have my doubts is the fact that you don't wan't to use action="link" in the form, although you wan' t to redirect the user to a new page. Not using that option makes your code a bit too complex. But, this shouldn't be an issue. Glad, I could help. All the best! – Bogdan Haidu Feb 03 '16 at 09:03