SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast irbrian's Avatar
    Join Date
    Jul 2004
    Location
    Cache Valley, USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HTTP Header to tell Browser to Stay Put?

    Is there such a thing as an HTTP header that causes the browser NOT to reload the page?

    We've got a simple form in the sidebar on one of our pages (for name and e-mail address). Originally when the user clicked "submit" the PHP handler script sent back a file download. The headers we were sending to force the download resulted, of course, in the browser popping up its default file download window. From the user's perspective, the browser never navigated away from the page.

    Now we want to send an e-mail instead... but once the form is submitted, and the e-mail sent by the handler script, obviously the browser believes it's supposed to go somewhere.

    So I'm wondering if maybe there's a header type that tells the browser to "stay put." I mean, you've got your Location header which tells the browser where to go; you've got the force-download and content type headers, which tell the browser its supposed to download a file instead of load a web page. Is there something I can use to cause the browser to send the form contents to the handler script, but not load the resulting page?

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could try Ajax.

    G for
    super ajax programming seed

    once you understand what is happening you can do amazing things with it.

    it sends a http request to an external php script page without a browser refresh.

    hth

    cheers
    lorenw

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by irbrian View Post
    So I'm wondering if maybe there's a header type that tells the browser to "stay put." I mean, you've got your Location header which tells the browser where to go; you've got the force-download and content type headers, which tell the browser its supposed to download a file instead of load a web page. Is there something I can use to cause the browser to send the form contents to the handler script, but not load the resulting page?
    Not really - when the browser makes a request, it has already decided to navigate away. The problem, you describe, is normally solved by giving a Location header back to the same page.

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    point the form action back to itself and at the top of the page, have some sort of flag like
    if ($_POST['submit']) handle the form and send email.

  5. #5
    SitePoint Enthusiast irbrian's Avatar
    Join Date
    Jul 2004
    Location
    Cache Valley, USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm familiar with Ajax. I've written an extension of cool & convenient javascript functions to the SACK library for one of our other projects (I call it HandySACK! ).

    This is such a simple event, though (client-side anyway -- type your e-mail, then click submit to receive a message) that I didn't want to involve Ajax. But its a possibility I'm still considering.

    Wonshikee's suggestion about handling the form on the same page is not an option; we're having to use multiple layers of dynamic content loading on this particular page through several different components. The form itself is a drop-in bit of html content loaded from a separate database table and script. I'd have to create a whole other component to handle this -- again, overkill.

    The most obvious and simple solution would of course be to send a Location header and redirect the page back to itself. Our heavy use of URL rewriting unfortunately makes this more challenging as well; I'll have to capture the user-viewable URL through a series of checks in the page request engine.

    Its something that would be highly useful elsewhere though, so I'll probably go this route.

    Thanks for the suggestions.

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by irbrian View Post
    I'm familiar with Ajax. I've written an extension of cool & convenient javascript functions to the SACK library for one of our other projects (I call it HandySACK! ).

    This is such a simple event, though (client-side anyway -- type your e-mail, then click submit to receive a message) that I didn't want to involve Ajax. But its a possibility I'm still considering.

    Wonshikee's suggestion about handling the form on the same page is not an option; we're having to use multiple layers of dynamic content loading on this particular page through several different components. The form itself is a drop-in bit of html content loaded from a separate database table and script. I'd have to create a whole other component to handle this -- again, overkill.

    The most obvious and simple solution would of course be to send a Location header and redirect the page back to itself. Our heavy use of URL rewriting unfortunately makes this more challenging as well; I'll have to capture the user-viewable URL through a series of checks in the page request engine.

    Its something that would be highly useful elsewhere though, so I'll probably go this route.

    Thanks for the suggestions.
    As soon as you hit submit, if there's no javascript - it's going to reload regardless of whether you use a Location header or not, if PHP is parsing - it means the browser left the page.

    If you don't want the page to load, I believe an iframe or ajax is your best option.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by irbrian View Post
    The most obvious and simple solution would of course be to send a Location header and redirect the page back to itself. Our heavy use of URL rewriting unfortunately makes this more challenging as well; I'll have to capture the user-viewable URL through a series of checks in the page request engine.
    That sounds like a deeper issue with your design. I think you would benefit from sorting that out, so you could use the redirect. See also http://en.wikipedia.org/wiki/Post/Redirect/Get


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
  •