0

I have been struggling with page reloads

I have a form that will submit data regardless of the line on the table that is updated.

When the form reloads, I can either get the page to reload the whole page or jump to an anchor, but not both.

The first part is
form
hidden value (with anchor) this works

On button click call function

function is in php with :

// this code will show editWonLost.php#123456 but does not reload
echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; document.location = 'editWonLost.php'+location.hash</script>"; 

// this code will show editWonLost.php#$NeedToUpdate
echo '<script type="text/javascript">location.hash="$NeedToUpdate"; document.location = "editWonLost.php"+location.hash</script>'; 

// this code will refresh the page but not got to anchor location
echo '<script type="text/javascript">document.location = "editWonLost.php"</script>';

The goal is to try get the page to reload and then jump to the location

mplungjan
  • 169,008
  • 28
  • 173
  • 236
REVHavoc
  • 11
  • 4

2 Answers2

0

Calling window.location.reload() after setting the hash value should work. Your code would then be:

echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; location.reload();</script>";

Update:

If your goal is to redirect the user to another page after submitting the form, try something like the following (untested!):

header('Location: editWonLost.php' . $NeedToUpdate);

Obviously, you have to check if the form has been submitted, and only redirect the user if it has.

Max Klein
  • 468
  • 1
  • 6
  • 22
  • Hi Maxkl I have tried the code, It reloads and stays at the location where it should be, but goes in a constant loop. page keeps refreshing. I had this before when using location.reload. – REVHavoc Mar 02 '17 at 12:26
  • Well, you should only reload if the user isn't on the page you are redirecting to. A better solution would probably be to do a redirect directly in PHP, I'll update my answer for more info. – Max Klein Mar 02 '17 at 13:12
  • Only solution working for me is to Redirect to a new page that the redirects back to my page. The data is then reloaded on the page load and then the jump to function works afterwards – REVHavoc Mar 13 '17 at 10:25
0

Take a look at the javascript documentation for location.reload()

https://www.w3schools.com/jsref/met_loc_reload.asp

So, your PHP could look like:

echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; document.location = 'editWonLost.php'+location.hash;  window.location.reload();</script>"; // this code will show editWonLost.php#123456

You can see this related question, as well:

How to reload a page using JavaScript?

Community
  • 1
  • 1
Courtney G
  • 11
  • 5
  • Hi maxkl. I have tried the code and it is reloading the page every second. – REVHavoc Mar 02 '17 at 13:50
  • I have tried the following from your link. window.locations.replace() will reload the page to the location but does not refresh my data. i then tried with windows.locations.assign(). same thing. If i click on my link multiple times after the second link it shows the change previously. – REVHavoc Mar 02 '17 at 13:53