Page reload in javascript - 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>

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.

Creating a link to an id that refreshes the page

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"]);
}
?>

Thank you alert upon form submission

I have a very basic form at http://www.happyholidaylites.com/contact.html and it is working great. When you submit the form, the user is brought to the index.html with no message that the form has been sent. I am looking to initiate an alert that says, "your form has been submitted" with an x button. My code looks like this:
<form method="post" id="myForm" action="dynaform.php">
<input type='hidden' name='rec_mailto' value='JBIRD1111#gmail.com'>
<input type='hidden' name='rec_subject' value='New Contact Form'>
<input type='hidden' name='rec_thanks' value='index.html'>
so on and so forth.....
The last line is what is telling the form what to do after the submit button is pressed, but I dont want it to point the browser to the index, rather I want a javascript popup with a success message. Any ideas?
Why not a simple onSubmit?
<form method="post" id="myForm" action="dynaform.php" onSubmit="alert('Thank you for your feedback.');" >
To be honest you are better re-directing to another page in order to avoid the user re-submitting the page on a refresh. Have a look at Post/Redirect/Get Pattern.
Popups can be extremely annoying on websites. You should create a page called "thank-you.html" that you can re-direct the user to on successful submission which has access to the site navigation options or even just do a re-direct back to the form page after a few seconds.
Instead of redirecting to index.html, redirect to thanks.html; your users will thank you because everybody hates popups!
Sounds like your PHP script handles the form submission by processing the input and redirecting the browser to the value in the rec_thanks field.
You can add something like onsubmit="YourJavaScriptFunction()" to the form tag to add client-side behavior prior to actually submitting the form. Within that function you can perform validation, use alert('Thank You!'), etc..

Categories

Resources