Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: form behavior ?

  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    form behavior ?

    Sorry but this query involves more than html/css. But it is about browser behavior
    and there doesn't seem to be a forum here specifically for browser behavior.
    It involves javascript an php to handle posts.

    I have an html/php form that submits to itself.
    In this form I have numerous buttons (generic with ids)
    each button is assigned an event listener by javascript.
    when one of the buttons is clicked, the button id is assigned
    to a hidden field. On the server side the php looks at the
    hidden field name submission and decides what to do based
    on the value that it received.

    Since I am incrementally developing the page, it do a lot of edits and refinements.
    Each time I want to reload the page to view the html and css edits, as well as test
    revisions to javascript code (which must be reloaded to view the result of the edits)

    So, the QUESTION. How can I prevent the browser from resubission on reload.
    If I am working on a part of the form which removes a directory on the server.
    I test that function and it works. Then I do some edits to html and or javascript,
    I want to reload the page. The browser resubmits the last post request, which fails
    because the directory has already been removed. I have tried using javascript to
    reload the page with location.reload() and window.location = <same file>
    but this will also cause a resubmission.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,450
    Thanks
    23
    Thanked 631 Times in 630 Posts
    I would not do my programming on the internet. I would get Wampserver and put a web development environment on your computer. (It's really only needed for php) WampServer, the web development platform on Windows - Apache, MySQL, PHP

    Another way is to not remove the directory, but rename it. Then restore the name as the first command in your program or write a separate program,which can be on the server or your computer, to do it.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    anotherJEK (03-28-2014)

  • #3
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    If you don't want your browser to resubmit or reload I would look into AJAX as a potential solution.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post
    I use Mac OSX and it doesn't matter if the server is remote live or local dev server. You still have to test with
    a browser, at least locally. This is something I've always done.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post
    As for ajax, that is a good suggestion, as I think about it. but unless I use a separate script to process the ajax requests on
    the server, I would have trouble with it. I have tried to get ajax requests to be processed by a self processing form page.
    ( a php empowered html page that submits to itself). But this is more a subject for the php forum.

  • #6
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,343
    Thanks
    11
    Thanked 288 Times in 287 Posts
    Use a session key in the form, and test for it in PHP. If the key's not the same, you're processing an old form. So the general flow looks like:

    session_start();
    if ($_POST['key'] == $_SESSION['key'])
    .... process the post stuff

    $_SESSION['key'] = mt_rand(1,1000);
    <form action=....>
    <input type='hidden' name='key' value='".$_SESSION['key']."'>
    </form>

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    The approach I came up with

    As I have been doing development since around 2003, I believe I had come up with this in the past and forgot about it.

    There are two methods, one with javascript and one with php that involves automatic redirect to a dummy.php script
    file that, in turn, redirects back to the form page.

    Javascript uses location.replace('<path to dummy script>');

    in php
    it is header('location: <dummy script>') which would be called when the submission
    processing script was done successfully.

    then in dummy script

    header('location: <originating script file>');
    I can also add get queries to the file references: like...
    <dummy script>?reload=true
    then in the dummy script: header('location: <originating script>?reload='.$_GET['reload']);
    Last edited by anotherJEK; 03-29-2014 at 02:00 AM. Reason: saw syntax error in header redirect


  •  

    Posting Permissions

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