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 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    Delhi
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to solve refresh page problem?

    Hi,
    I am facing a problem, Below is the code
    HTML Code
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form id="abc" name="abc" action="" method="post">
    <table>
    <tr>
    <td>
    Name:&nbsp;<input id="name" name="name" type="text" />
    </td>
    <td>
    Id:&nbsp;&nbsp;<input id="id" name="id" type="text"
    </td>
    </tr>
    <tr>
    <td>
    <input type="submit" name="save" id="save" value="Save" />
    </td>
    <td>
    </td>
    </tr>
    
    </table>
    </form>
    </body>
    </html>
    PHP Code:
    <?php  
    if(isset($_POST['save']))  
    {  
    $name=$_POST['name'];  
    $id=$_POST['id'];  
     echo 
    "<script type='text/javascript'>alert('check message: $name :- $id');</script>";   
    }  
    else  
    {  
    }  

    ?>
    Step 1- Fill form and press save.
    Step 2- Get a alert message. press ok
    Step 3- Now Press F5 (Refresh).

    Problem- Every time when i press F5 for refresh i am getting the old alert message, even before i fill the form and press save button.
    Can anyone tell me why this happening or is there any solution to get out of this?
    Thanks

  • #2
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    The problem your having is because the Post variables are still set because you are refreshing the returning page. When you press refresh your php script re-evaluates the if block at the top and because post variables are still set, the if block will run the JavaScript again.

    What purpose do you need to press F5 for and what are you expecting to happen when you press it? Your post is unclear as to the reason for pressing F5.

  • #3
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Also it's kind of unclear as to the reason for your PHP. I get the impression that you are trying to do error checking although your code is incomplete. If it's error checking you are trying to achieve, you are really over complicating error checking. You are effectively using both PHP and JavaScript for your error checking (PHP is checking for empty fields and JavaScript is informing the visitor of a problem).
    You could do the whole error checking with just one language.
    Using JavaScript you could have a script that checks form fields 'before' the page is submitted. Alternatively you could post the form, have PHP checks for errors and then incorporate some text next to each form field that has a problem, stating the issue with each field.

  • #4
    New Coder
    Join Date
    Jun 2012
    Location
    Delhi
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Fehnris,

    If i only press F5 in Firefox it don't give any message,

    but i do CTRL + F5 in Firefox it show the message
    Confirm dialogue box,
    "To Display this page, Firefox must send information that will repeat any action ( such as a search or order confirmation ) that was performed earlier.

    And In case of chrome it give message that is.
    "Confirmation message.
    The page that you're looking for used information that you entered.
    Returning to that page might cause any action that you took to be repeated.
    Do you want to continue?"

    Is there any way to prevent this to happen?

    Thanks

  • #5
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Rajeshkr,

    I'm sorry but I don't understand what your asking.

    Quote Originally Posted by rajeshkr View Post
    The page that you're looking for used information that you entered.
    Returning to that page might cause any action that you took to be repeated.
    Thanks
    The reason your getting the dialog boxes is because of the POST variables still being valid because you are refreshing the page after you have submitted the form. You will get these dialogs when you try to refresh a page that you have sent information to.

  • #6
    New Coder
    Join Date
    Jun 2012
    Location
    Delhi
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fehnris View Post

    The reason your getting the dialog boxes is because of the POST variables still being valid because you are refreshing the page after you have submitted the form. You will get these dialogs when you try to refresh a page that you have sent information to.
    Hi,
    I just want to know that what i can do to remove this to happen.

  • #7
    New Coder
    Join Date
    Jun 2012
    Location
    Delhi
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi all,

    i just change in the code and found working.

    old code
    Code:
    echo "<script type='text/javascript'>alert('check message: $name :- $id');</script>";
    New code
    Code:
    echo "<script type='text/javascript'>alert(''check message: $name :- $id''); window.location.href='same page';</script>";
    thanks all for suggestion.

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    The issue with your problem is If you submit data in a form and then hit f5 to refresh the page, it resends the data again. Browsers should stop this, or at least prompt if you want to resend the data, but the best way to prevent this from happening is either use jquery to submit the data or do as you did with the javascript page refresh (although I prefer jquery, it's much cleaner IMO).


  •  

    Tags for this Thread

    Posting Permissions

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