Results 1 to 13 of 13

Thread: Session

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Session

    I have done the following session code. There is no error while I am running it. But I cannot retrieve the session values. Please help.

    session1.php
    ------------
    <?php

    session_start();
    $_SESSION["name"]="$_POST[name]";
    $_SESSION["date"]="$_POST[date]";
    $_SESSION["month"]="$_POST[month]";
    $_SESSION["year"]="$_POST[year]";
    $_SESSION["country"]="$_POST[country]";
    $_SESSION["question"]="$_POST[question]";

    ?>

    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Session Demo</title>
    </head>

    <body>

    <p align="center"><font size="5" color="#800080"><b>MB Free Tarot Reading
    Software</b></font></p>

    <p align="center">&nbsp;</p>
    <form method="POST" action="<?php echo "$_SERVER[PHP_SELF]"; ?>" name="tarot" onsubmit="return validate(this)">
    <p>&nbsp;</p>
    <div align="center">
    <center>
    <table border="0" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111" width="50%" id="AutoNumber1">
    <tr>
    <td width="50%">
    <p align="left"><b>* Name</b></td>
    <td width="50%">&nbsp;<input type="text" name="name" size="42" value="test"></td>
    </tr>
    <tr>
    <td width="50%">
    <p align="left"><b>* Date of Birth</b></td>
    <td width="50%">&nbsp;<select size="1" name="date">
    <option selected>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
    <option>20</option>
    <option>21</option>
    <option>22</option>
    <option>23</option>
    <option>24</option>
    <option>25</option>
    <option>26</option>
    <option>27</option>
    <option>28</option>
    <option>29</option>
    <option>30</option>
    <option>31</option>
    </select>
    <select size="1" name="month">
    <option selected>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December</option>
    </select> <select size="1" name="year">
    <option selected>1970</option>
    <option>1971</option>
    <option>1972</option>
    <option>1973</option>
    <option>1974</option>
    <option>1975</option>
    <option>1976</option>
    <option>1977</option>
    <option>1978</option>
    <option>1979</option>
    <option>1980</option>
    <option>1981</option>
    <option>1982</option>
    <option>1983</option>
    <option>1984</option>
    <option>1985</option>
    <option>1986</option>
    <option>1987</option>
    <option>1988</option>
    <option>1989</option>
    <option>1990</option>
    <option>1991</option>
    <option>1992</option>
    <option>1993</option>
    <option>1994</option>
    <option>1995</option>
    <option>1996</option>
    <option>1997</option>
    <option>1998</option>
    <option>1999</option>
    <option>2000</option>
    <option>2001</option>
    <option>2002</option>
    <option>2003</option>
    <option>2004</option>
    <option>2005</option>
    <option>2006</option>
    <option>2007</option>
    <option>2008</option>
    <option>2009</option>
    <option>2010</option>
    <option>2011</option>
    <option>2012</option>
    <option>2013</option>
    <option>2014</option>
    <option>2015</option>
    <option>2016</option>
    <option>2017</option>
    <option>2018</option>
    <option>2019</option>
    <option>2020</option>
    <option>2021</option>
    <option>2022</option>
    <option>2023</option>
    <option>2024</option>
    <option>2025</option>
    <option>2026</option>
    <option>2027</option>
    <option>2028</option>
    <option>2029</option>
    <option>2030</option>
    <option>2031</option>
    <option>2032</option>
    <option>2033</option>
    <option>2034</option>
    <option>2035</option>
    <option>2036</option>
    <option>2037</option>
    <option>2038</option>
    <option>2039</option>
    <option>2040</option>
    <option>2041</option>
    <option>2042</option>
    <option>2043</option>
    <option>2044</option>
    <option>2045</option>
    <option>2046</option>
    <option>2047</option>
    <option>2048</option>
    <option>2049</option>
    <option>2050</option>
    </select></td>
    </tr>
    <tr>
    <td width="50%">
    <p align="left"><b>* Country</b></td>
    <td width="50%">&nbsp;<input type="text" name="country" size="42" value="test"></td>
    </tr>
    <tr>
    <td width="50%">
    <p align="left"><b>* Question</b></td>
    <td width="50%">&nbsp;<textarea rows="2" name="question" cols="32">test</textarea></td>
    </tr>
    </table>
    </center>
    </div>
    <p align="center">&nbsp;</p>
    <p align="center"><input type="button" value="Continue" name="continue" onclick="Continue()">
    <input type="reset" value="Reset" name="reset"></p>
    </form>

    </body>

    <script language="javascript">

    function Continue()
    {
    location.href="session2.php";
    }

    function validate(thisform)
    {
    if(document.tarot.name.value=="")
    {
    alert("Name cannot be Blank");
    document.tarot.name.focus();
    return false;
    }
    else if(document.tarot.country.value=="")
    {
    alert("Country cannot be Blank");
    document.tarot.country.focus();
    return false;
    }
    else if(document.tarot.question.value=="")
    {
    alert("Question cannot be Blank");
    document.tarot.question.focus();
    return false;
    }
    }

    </script>

    </html>



    session2.php
    ------------
    <?php

    session_start();
    echo $_SESSION["name"];
    echo $_SESSION["date"];
    echo $_SESSION["month"];
    echo $_SESSION["year"];
    echo $_SESSION["country"];
    echo $_SESSION["question"];

    ?>

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Session

    Can you use the PHP tags.

    Why

    PHP Code:
    $_SESSION["name"]="$_POST[name]"
    Can you not just do

    PHP Code:
    $_SESSION["name"] = $_POST["name"]; 

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Re: Session

    Quote Originally Posted by john tindell
    Can you use the PHP tags.

    Why

    PHP Code:
    $_SESSION["name"]="$_POST[name]"
    Can you not just do

    PHP Code:
    $_SESSION["name"] = $_POST["name"]; 

    Sorry Sir, this code is not working. I changed the syntax according to you. But I would not get the result. Please do something.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Session

    Your session is probably not being save. Put the following line at the top of the script BEFORE session start. Do you get any errors?
    Code:
    error_reporting(E_ALL);
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Session

    You are absolutely correct. I am getting the following errors. The lines in errors are the lines where I stored the values in the $_SESSION. Please help me to cope up the situation.


    Notice: Undefined index: name in /home/amitnet/public_html/tarot.php on line 5

    Notice: Undefined index: date in /home/amitnet/public_html/tarot.php on line 6

    Notice: Undefined index: month in /home/amitnet/public_html/tarot.php on line 7

    Notice: Undefined index: year in /home/amitnet/public_html/tarot.php on line 8

    Notice: Undefined index: country in /home/amitnet/public_html/tarot.php on line 9

    Notice: Undefined index: question in /home/amitnet/public_html/tarot.php on line 10

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Session

    Which of the two files is tarot.php?

    Looks like the POST variables aren't submitted.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Session

    After looking at your messy un formatted I realised that you are never actually submitting the form. HTML allows you to define and image/submit button to submit form data. Granted, you have one but it redirects the page using javascript to the next page. Hence the form variable are not submitted.
    Code:
    <input type="button" value="Continue" name="continue" onclick="Continue()">
    
    function Continue()
    {
        location.href="session2.php";
    }
    You need to change type attribute to submit and remove the onclick event handler. The form will then automatically submit when the button is clicked.

    Like I have suggested in the past. You should learn HTML.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Session

    Please take a look into following two simple most files of php. Still the session is not working. What should I do now ?


    [php]
    <?php
    // file - test.php
    session_start();
    $_SESSION["name"]=$_POST["name"];
    $_SESSION["question"]=$_POST["question"];
    // I have got the following error - What does it mean ?
    // PHP Notice: Undefined index: name in C:\_PHP\Runes Reading\test.php on line 4
    // PHP Notice: Undefined index: question in C:\_PHP\Runes Reading\test.php on line 5
    ?>

    <html>
    <head>
    <title>SESSION</title>
    </head>
    <body>
    <form method="POST" name="f" action="test1.php">
    Name <input type="text" name="name" size="20"><br>
    Question <input type=text" name="question" size="50"><br>
    <input type="submit" name="continue" value="Continue">
    </form>
    </body>
    </html>
    [php]

    [php]
    <?php
    // file - test1.php
    session_start();
    echo $_SESSION["name"];
    echo $_SESSION["question"];
    ?>
    [php]

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Session

    You are submitting the form to the wrong page. You should be submitting it to test.php not test1.php.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Session

    I do not understand you visualAd. Why should I need to do this ? I am storing the session values in test.php and after click on submit button I am redirecting it to text1.php and would like to see the session values in test1.php. Can you please tell me ?

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Session

    Mentally trace through the code step by step from test.php through the post action to test1.php and then you should see the issue.

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Session

    The action="test1.php" tells me that you are posting the form to test1.php. You do not need to change this value to test.php, but seen as you are picking up the post value in test.php, it might be a good idea .

    You should check that the variables exsit before trying to reference them (this will eliminate the notices). If they do not then it means that the form has not been submitted and you need to dispaly it. If they do exist, save the variables in the session and redirect to test1.php using the HTTP location header:
    PHP Code:
    /* check the variables have been posted */
    if (isset($_POST['name'], $_POST['question'])) {
        
    session_start();
        
    $_SESSION["name"]=$_POST["name"];
        
    $_SESSION["question"]=$_POST["question"];

        
    header('Location: test1.php'); // this is where you redirect
    }
    ?> 
    Remember that relies on the action attribute of the form being set to test.php and not test1.php.

    P.s: to show PHP code use [php]<?php echo("php code here") ?>[/php], if you don't use a closing [/php] tag the code won't get formatted
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Re: Session

    Thank you very very much visualAd. The things really help me out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width