locked
cookies or no? RRS feed

  • Question

  • Hey guys (and girls),

    I'm having trouble accessing data from an old window in a new window.
    Let me re-phrase this...
    The user of my site needs to click an 'open' button on the page to open a data record. The button opens the record in a new window. This is where the problem is, I need to get the information off of the initial page and use it in the new window.

    I've tried cookies, and that is not working.. here is what i did when i tried using cookies, maybe it's something simple that i'm not doing right (i'm new to this whole web programming thing)...


    This is when i set the cookie...
    [code]

      <form id="form1" name="form1" method="post" action="log.php">
      <p><h2>Notes</h2>
       <select name="notes" size="5" id="notes">
       <?php fill_notes($id); ?>
       </select><br />
       <input name="add_note" type="submit" id="add_note" value="Add" />
       <textarea rows="2" name="add_note_text" cols="50"></textarea>
       <br />
       <input name="edit_note" type="submit" id="edit_note" value="Edit" />
       <textarea rows="2" name="edit_note_text" cols="50"></textarea>
       <br />
       <input name="delete_note" type="submit" id="delete_note" value="Delete" />
       <br />
       <input name="open_note" type="button" id="open_note" value="Open" onclick=
        <?php setcookie("message", $_POST["notes"], time() + 86400); ?>"open_notes()" />
       </p>
    [/code]

    This is where i open the new window...

    [code]

    <head>
     <title>Note-Taker</title>
     <script type="text/javascript">
     <!--
      // Function:  open_notes()
      // Parameters: none
      // Returns:  none
      // Description: opens a note in the user's list in a full popup window
      function open_notes()
      {
       //Get the message
       var message = document.getElementById("notes").value;
     
       //Open the note in a new simple popup window
       window.open("message.php", "winlink", "height=400, width=800");
      }
     //-->
     </script>
    </head>

    [/code]

    And this is where I try to get the data in the cookie in the new window...
    [code]

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title>Message Viewer</title>
    </head>

    <body>
     <?php
      if(isset($_COOKIE["message"]))
      {
       $message = $_COOKIE["message"];
       echo '$message';
      }
      else
      {
       print("Sorry, an error has occured...");        //THIS ALWAYS PRINTS...
      }
     ?>
    </body>
    </html>

    [/code]

    Any insight would be greatly appreciated.
    If you need more background info, just let me know..

    Thanks again!

    Wednesday, December 6, 2006 3:57 AM

Answers

  • You can do this completely through javascript, bypassing cookies altogether, using the window.opener property to reference the original window and use the DOM to access the elements and their values on that page. 

    See: http://www.w3schools.com/htmldom/dom_obj_window.asp for more info on the DOM and how to get what you want from the original page.

    Wednesday, December 6, 2006 8:01 AM