Creating a link to an id that refreshes the page - javascript

I am working on a project that uses javascript to make tabs. It has 3 tabs.
Tab three has an id of tab3, what I need is a refresh button that will be on tab3 that will refresh the page. Problem I'm having is if you click on tab3 it will display tab3 data, but it does not change the url. So the refresh button isn't working.
I tried an onclick javascript that reloads the page using location.reload() which reloads the page but not to the tab3. So I put that on a link,
<a href="<?php echo $_SERVER['REQUEST_URI']; ?>#tab3" onclick="reloadPage();" >Refresh</a>
This will link to the correct location, the URL then has #tab3 at the end, then the Javascript runs and it reloads back to the url without the #tab3.
So I then tried this that I saw somewhere.
Go
Which links, but doesn't reload.
What is the best way to do this?

Try this-
<a href="#tab3" onclick="location.reload();" >Refresh</a>

Using window.location is the solution, however if the current location is already the same as the new url that is assigned, then browser will not re-load the page.
The only trick would be to make the new url look "new". This can be done by adding a random identifier as querystring parameter.
In the form:
http://hostname?<dynamic/random>#tab3
Example:
Go

In head put,
<script type='text/javascript'>
function goto(link) {
window.location = link
}
</script>
and do this to your button
<a onclick=goto("<?php echo $_SERVER['REQUEST_URI'] . '#tab3'; ?>"); >GO </a>
if you want to refresh the page and aslo goto same place this code will work
<form method=post action="">
<input type=hidden name=value value="#tab3" />
<input type=submit value=GO />
</form>
<?php
if (isset($_POST['value']))
{
header('Location: http://'. $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI] . $_POST["value"]);
}
?>

Related

Pass data to another page and submit form on that page

I'd like a button on page1.html. This button will pass data to page2.html which has a form on it, load that data into the form, and submit the form on page2.html. Is this possible? How would it be done? I can't seem to find any examples of this.
Thanks!
Of course the best thing to do is to use a backend... but I guess you wouldnt be asking if you had that option.
one thing you can do is use the anchor in the URI, like: /page2#?field=value&fld=val
Then on page load, check for an anchor tag and process it:
$(function(){
let hash = window.location.hash.substr(1);
})
Here's a plunkr to get you off the ground: https://plnkr.co/edit/u9fPGFZedXGmKbbdXVvy?p=preview
So once you have the hash you would parse it to JSON and configure your form with it.
It isn't obvious that it works because of how these fiddle-like sites operate. But it does work! To check it out, open the preview in separate window mode, and copy the url, open that url in a new tab, and add a sample hash on it, and press enter so it takes. then reload the page.
I ended up using PHP's Post method:
From originating page1.php page:
<form action="page2.php" method="post">
<input type="hidden" name="MyName" id="MyId" value="MyVal">
<input type="submit" value="Post to page2">
</form>
In receiving page2.php page:
<?php if (!empty($_POST)) : ?> // Check for Post data
<script>
document.getElementById("MyId").value = '<?php echo $_POST["MyName"]; ?>'; //update form on page2.php by field ID
document.getElementById('FormToSubmit').click();
</script>
<?php endif;?>
Works perfectly
You can simply add (target="_blank") to form tag and you can get data in specific action page
<form action="test.php" method="post" target="_blank">
<input type="text" name="test" />
<button type="submit" name="submit">Submit</button>
</form>

HTML form POST to direct print page

I have the page VIEW-SALE.PHP
on this page I have a submit button form to view the invoice in print format, there is the code:
<span style='display:inline-block'>
<form name='print' id='print' action='print-invoice.php'
target='_blank' method='post'>
<input type='hidden' name='invoice' value='$invoice'>
<input class='submit-all' type='submit'
value='Print the invoice'
onClick='window.print();return false'></form></span>
The code must just open the page PRINT-INVOICE.PHP and print it but when I click it it show the print dialog but of the current page I am VIEW-SALE.PHP so it does not show the PRINT-INVOICE.PHP
How does it can be done?
doesn't post just post data you may need a header redirect the redirect being php code
header("Location: blabla")
I found this you may find it of help
redirect after submit
the problem is the onClick='window.print();return false'. The window object is the current window containing the form. You should include an javascript event handler in your PRINT-INVOICE.PHP:
... <body onload="window.print()"> ...

Back to the form with back button

How does I can make the browser back button resend me the form? I have my page www.example.com/results where I see all results of $_POST search, when I click in one result I load it in www.example.com/result1 . I need to click on the back button and loads the form again without showing "Confirm form Resubmission" (F5). I do not care to do it with PHP, with JS, jQuery.
For example:
Index.html
<form method="post" action="results.html">
<input type="text" name="findSomething" />
</form>
In results.html show the results:
<a href="result1.html>Result1</a>
<a href="result2.html>Result1</a>
<a href="result3.html>Result1</a>
.......
In result1.html,resutlt2.html.... if a click on back button, brower says that I must to Resubmission the form.
It is default browser functionality ,you can change the setting using PHP only. You can use these syntax in your PHP config file before the session start:
<?php
session_cache_limiter ('private, must-revalidate');
$cache_limiter = session_cache_limiter();
session_cache_expire(60); // in minutes
?>
Now it will be not ask to re-submission the form.

Page reload in javascript

I have a login page which takes the user name and password and directs it to a controller. If the login information is correct, the controller sends some data to be displayed on the home page.
Now, I want to provide a link on the home page that enables the user to refresh the page on a click. This should resubmit the username and password and the page is reloaded, i.e. F5 functionality.
My current code :
Please <a href='#' onclick='location.reload(true);'> refresh the page </a>
is not exhibiting the form resubmission from the login page. How may I achieve this? Any ideas regarding this would be very helpful.
Nice cross-browser support.
To Reload the page.
window.location.replace(window.location.href);
To submit a form. Where <form id="myForm" ... >.
document.getElementById("myForm").submit();
Documentation
Replace / Href / Submit
You could do this:
Please <a href='#' onclick='document.getElementById("myForm").submit();'> refresh the page </a>
And just having a hidden form again, if the form is not present any more.
from http://www.w3schools.com/jsref/met_form_submit.asp
function reloadPage()
{
location.reload();
}
call this function on click of your link
onclick="reloadPage()"
I think this works .
Try one of following -
window.location.reload();
history.go(0);
window.location.href=window.location.href;
did you try to resend the form with hidden fields?
<form id="formId" action="#">
<input type="hidden" id="user" value="#{userValue}" />
<input type="hidden" id="pass" value="#{passValue}" />
</form>
Please <a href='#' onclick='$("#formId").submit();'> refresh the page </a>

Why should I click double on an 'a href' attribute to reload the page?

I have such a problem:
When I click on the element the url changes but PHP code doesn't run, but when I click it again or just push 'Enter' in the url, or push F5 the page reloads and PHP code runs
<a href="?action=out" title="Forget me!">
<img src="images/icons/exit.png" id="exit" class="ava">
</a>
Here was my html code.
Here is PHP one:
if($_GET['action']=='out')
{
//do smth. here
}
What is the problem, I just can't get it..
maybe I just have to use JS (location.reload() and so on), but I think that it will be not a good descision...
Thank you in advance!
I believe your href is wrong you have information in the href but no page assigned, if you are sourcing your own page try
<a href="<?php echo $_SERVER['PHP_SELF'].'?action=out'; ?>" title="Forget Me!">

Categories

Resources