SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Execute function after form submitted/page refreshed

    Hi All
    I have a form within an iframe that I want to use to refresh the location of the parent. I am using the form to set session variables that are used within the parent.
    So in the iframe I have:
    Code:
    <form name="MyForm" method="POST" 
    action="<?php echo $_SERVER['PHP_SELF'] . '?' . SID; ?>">
    And to submit this form I'm using this link:
    Code:
    <a href="javascript: submitform()">Search</a>
    Which refers to this script:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function loadParent()
    {
    parent.location.href = "/search_results.php?";
    }
    
    function submitform()
    {
    document.MyForm.submit();
    loadParent();
    }
    //-->
    </SCRIPT>
    (I think!) my problem is this; it works fine if the MyForm action, i.e. ="<?php echo $_SERVER['PHP_SELF'] . '?' . SID; ?>", completes before loadParent() is executed but if there is a slight delay in the time it takes for the server to do this, loadParent() is executed too early and the input from MyForm is lost.
    What I need is to tell the script to wait until the MyForm action has completed, i.e. the page has refreshed, before running loadParent().
    I'm sure there are loads of different ways of doing this. Ideally I would like something that detects the page refresh within the iframe (caused by the MyForm action) and not something that relies on a timed delay loop.
    All ideas gratefully received.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page that receives the form - once it's done processing can you write out:

    <html>
    <body onload="window.parent.loadParent();"></body>
    </html>

    ?

    Then you can get rid of loadParent from your submitform function.

  3. #3
    SitePoint Member
    Join Date
    Nov 2004
    Location
    UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I'm afraid it didn't work.
    It just caused the parent page (search_results.php) to continually refresh itself. I.e. search_results.php is the page that receives the form, so the <body onload..> in it causes loadParent() to load search_results.php etc. etc.
    I'm not sure if this approach is going to be the answer because (here comes the info I left out in my original post ) every page in my site is going to have an iframe containing the same form so wherever you are on the site you will be able to run the form from within the iframe and when you hit submit the parent location needs to be changed to search_results.php.
    I think this means the relevant coding needs to remain within the iframe, unless maybe I add an "if statement" to your solution which is triggered by a variable passed from the form(?).
    I guess I could solve the problem by having search_results.php come up in another iframe but I really want to minimise my use of frames as much as possible.
    I'm thinking the solution may lie server side.

  4. #4
    SitePoint Member
    Join Date
    Nov 2004
    Location
    UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Solution

    What I've done is to add a hidden input field called updateSearch in MyForm and set it's value to "1" onsubmit.
    I then used php to dynamically write the loadParent() function like this:
    Code:
    <?php
    echo '
    <SCRIPT LANGUAGE="JavaScript">
    <!-- 
    function loadParent()
    {
    ';
    if ($updateSearch == "1")
    	{
    echo 'parent.location.href = "/search_results.php?";';
    $updateSearch = "";
    	}
    echo '
    }
    //-->
    </SCRIPT>
    '; ?>
    And included <body onload="loadParent();"></body> on the page containing MyForm.
    So the parent location href is only changed when the MyForm page refreshes after submission.
    I'm sure there are other (better?) solutions but this works so I can move on to my next headache!
    Thanks jimfraser for your <body onload..> inspiration.


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
  •