SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I pass data from PHP form to Javascript

    I have a form with 3 button.

    1. Submit button.
    2. Reset button.
    3. Preview button.

    When the user click on the submit button, the form data is submitted to another page.

    However, when the user click on the preview button, a new pop up window (using Javascript) will appear together with the data that has been entered into the form previously by the user.
    I managed to create a pop up window however, I do not know how to pass the data entered in the form to the new pop up window.

    I have used the $_POST[ ]; in the new pop up window file, but it could not get the data. Does anyone have any solutions?

    Thanks very much
    zoya

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Perhaps you can do something like this?

    HTML Code:
    <form action="process.php" method="post">
     <!-- put all your elements here -->
     <input type="submit" value="Submit" />
     <input type="reset" value="Reset" />
     <input type="button" value="Preview" onclick="this.form.action='preview.php';
       this.form.target='prewin'; window.open('preview.php', 'prewin', 'options');
       this.form.submit()" />
    </form>
    Edit:

    I had forgotten the code which submit the form; added it now.
    Last edited by lilleman; Feb 9, 2005 at 06:01.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Lileman,

    I have tried that coding. It ables to create a pop up window but the problem it how can I pass the information that the user has entered into the page let say (enter.php) into the preview.php page?

    Thanks

  4. #4
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Then submit the values:
    HTML Code:
    <form action="process.php" method="post"> 
    <!-- put all your elements here -->
    <input type="submit" value="Submit" />
    <input type="reset" value="Reset" /> 
    <input type="button" value="Preview"
      onclick="this.form.action='preview.php';
                      window.open('preview.php', 'prewin', 'options');
                      this.form.target='prewin';
                      this.form.submit(); "/>
    </form>
    Then you get everything in $_POST in preview.php
    Ó+
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  5. #5
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have tried the coding.
    And it comes up quite strange. When I press the preview button nothing appear. However, when I press the submit button, instead of inserting the data into the database, it preview the page.

    What should I do?

  6. #6
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try better:
    HTML Code:
    <input type="submit" value="Submit"
      onclick="this.form.action='process.php';
                      this.form.target='_self';
                      this.form.submit(); "/>
    <input type="button" value="Preview"
      onclick="this.form.action='preview.php';
                      this.form.target='_blank';
                      this.form.submit(); "/>
    Ó+
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  7. #7
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi toggg,

    Thank you very much. It is now working.

  8. #8
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You can also stay by the client if you want

    Hi,
    Just to mention, case it's enough by the client,
    I mean case you don't need any processing from the server to make the preview,
    You can do everything in js by the client without requesting the server:
    HTML Code:
    <input type="button" value="Preview" onclick=
       "window.open('javascript:buildPreview(this.form))', 'prewin');"/>
    where buildPreview is some javascript function taking the form as parameter and building out the preview document inclusive the html tags. This script buildPreview(frm) access each field thru frm.name.value where name is the name of the input tag.
    Ó+
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source


Bookmarks

Posting Permissions

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