How to reload page with javascript without sending POST information again - javascript

I have a page, to which I POST information via form method="post". I would like to relaod it with JavaScript, but location.reload(true) and location.reload() makes browsers ask if I want to send the POST data again.
I would like to make JavaScript reload the page with GET instead of POST to skip the POST data.
How can I achieve that?

window.location.href = window.location.href
try

To reload a page without post data using javascript the easiest way I've found is to use an extra form to submit - this works regardless of whether the url is # or anything.
<form name="reloadForm">
<button type="submit">
Continue
</button>
</form>
<script>
document.reloadForm.submit() ;
</script>
That will show a button briefly on screen that the user can click continue on but the script will automatically submit it - this method sorts out a number of problems - the form is empty so it will not submit any post data (the button isn't named so even that won't submit a value). It works even if the url is set to #, and finally it gives a backup in case the user has disabled javascript (or the javascript doesn't work for some reason) they've still got a continue button available to click.

Related

To prevent user from enabling button using inspect element

We are using a submit button in a timesheet which gets disabled once the month is finished. User cannot submit on next month.
Problem some user go to inspect element and remove disabled and submit the form.
<a id="submit_time_sheet_id" href="javascript:void(0);"
onclick="isAllDaysPunched()" class="btn btn-xs btn-primary"
disabled="disabled">FinalSubmit</a>
User removes disabled="disabled" and form gets submitted.How to prevent user from modifying code
This will always be possible. You can't prevent anyone from using the developer tools to manipulate your form. That's why you always have to check data sent to the server server side.
“How to prevent user from modifying code?”
The answer is: you can't. How your website is opened is absolutely only decided by the respective user. Any person may download the HTML/JS/CSS source code of your website and modify it according to their needs. Or they directly invoke the form submit using tools like cURL.
The only way to prevent the submitting of data in a specified period, is to check the state – whether a user can submit data or not –on the server-side.
According to this Answer
you can recheck your validations in the submit action.

how to go back to previous page on click and reload with previously posted form data with php or js

I have a flow like this
Order Form page (index.php)
Confirmation page (where email is entered) (place-order.php)
Success page (register.php)
All pages have the order form with display:none and can be called on click to display.
In the success page, if the email is empty, it says 'go back and retry'.
Now when I use history.go(-1) or window.location.replace("place-order.php"); or
window.history.back(); it goes to the previous page. But the browser asks the user to reload page as the webpage has expired.
window.location=url will not work as it will go the url and all previously posted form data to that page will be lost
I want the browser to go back to the page and automatically refresh the page without the browser asking you to do it retaining the form values as when passed on from order form
What would be the best approach ?
I realized that the answer is very simple using the _SESSION Variable !
I stored the POST variable to SESSION['place-order'] variable and used
window.location:place-order.php
in the register.php page to redirect and added the following code to the place-order.php page
if(isset($_POST['clothessub']) || $_SESSION['placeorder']){
if(empty($_POST)){$_POST=$_SESSION['placeorder'];}
//Do my stuff
}

Submit form in background and use information in background

Basically my user inputs code. I can't check whether this code is valid or not(algorithm is a secret). Therefore I send(with form submit) this code to website that checks it (as $_POST variable). I have managed to do that. But how do I:
-Not display this webpage (user must stay on my webpage!)
-Get part of html from the website, that should not show and use it to fill variables (I know exactly which part of html I am looking for)
Can anyone point in the right direction with this problem..
jQuery has a simple function to add an event handler for submition and a function for posts, if you use the preventDefault function for the submit event, the form's action doesn't get performed, i.e. the side doesn't get reloaded. the html code you want to receive has to be fetched on server side, and transmitted back to the client
$('form').submit(function(e){
e.preventDefault();
$.post("example.com/somefile.php", sentData, function(receivedData){
useTheResponseData(receivedData);
});
});

Replicate f5 function to link

Is it possible to replicate f5 action to link using jquery? I need to refresh the page and resend data using this link, I have tried :
location.reload(true); or document.location.reload(); or $.f5();
But did not satisfy me.
Steps to reproduce using f5 key :
1. fill form then submit
2. press f5 - confirm box will appear
The page that you're looking for used information that you entered.
Returning to that page might cause any action you took to be repeated.
Do you want to continue?
try using
window.location.reload(true);
if it wont resubmit the form. you have two more options.
Submit the form using ajax after once submitted and resubmit the form and second time refresh the page or clear the form
Or you can keep the post data in some session or in the post array [server side code] and refill the form and resubmit,
Make sure you keep the track of numbers of submit you made since it will cause you trap in some recursion.
BUT why you want to resubmit the form you already have data you can perform both actions,
And if you have to post the form to some other action for second submit you can do the same may be using CURL at server side.
Try this:
window.location.reload(false);
// If we needed to pull the document from
// the web-server again (such as where the document contents
// change dynamically) we would pass the argument as 'true'.
source

How to submit form with data before logging someone out?

I'm using the document.form.submit() function for a rather large input form (hundreds of fields, it's an inventory application). I'm calling this after the user has been idle for a certain amount of time and I would like to save any data they've typed. When I try this the page reloads (the action is #) but any new text typed in the fields is not passed in the REQUEST, so I don't get to put it in the DB. Is there some fundamental reason why this happens or is my code just not playing nice together (I'm using the EXTJS grid view to show the form and a library for tracking idle time)?
Thanks,
Robert
I guess I put the answer here. What I found was that doing this:
setTimeout('frm.submit();', 2000);
caused the page to reload but didn't submit the form. When I did this:
frm.submit();
The form was submitted and the data was passed. I don't know why the first way didn't work, but I don't need to know that:)
Might the server be voiding out the input values. Say if your page on the server looks like this:
<form action="/page.cgi">
...
<input name="Fieldx" value=""/>
</form>
I think it'll void out the field. Or this the server action might be setting it indirectly. In JSF, something like this.
<input name="Fieldx" value="#{bean.nullProperty}"/>
What do you have on the server and what's your browser?
I would try to catch the HTML post request to see if the input fields are included. If they are then your server has problem.
But regarding what you said, I think it's because there's conflict in the way your browser handles JavaScript DOM. This may be the case if you leave out the submit button on your form and it works.
The submit method of HTMLFormElement objects should just submit the form, as if the user had clicked the submit button. So, if the action attribute of the form is set to #, it would just seem to refresh the page, because it’s sending the form data to the same page.
Strange that it still does it when you set the action attribute to another page though.
Is the method attribute of the form set to get or post?

Categories

Resources