Related:
Make a link use POST instead of GET
Making href (anchor tag) request POST instead of GET?
How can I make it so that, when a user clicks a link, he is redirected to a new page, along with whatever was the input on the previous page? I have to work with POST requests, and I do not want to use any hidden forms (or any additional forms for that matter), as was suggested in the linked (and many other SO) answers. Is this even doable?
My current setup (which I'm not allowed to change much) is as follows:
<form role="form" id="myForm" method="post" action="stuff.php">
<div class="form-body form-body-dark">
<div class="form-actions">
<div class="row">
<!-- various inputs go here -->
<input type="text" name="userID">
</div>
<div class="row">
<div class="col-md-6 col-xs-6">
<button type="submit" id="doStuff" class="btn btn-primary btn-lg btn-block"><i class="fa fa-check"></i> Submit</button>
</div>
<div class="col-md-6 col-xs-6">
<a href="index.php" id="cancel" class="btn btn-danger btn-lg btn-block"><i class="fa fa-refresh"></i> Cancel
</a>
</div>
</div>
</div>
</div>
</form>
Now, I need to add another way of submitting part of the same input, but to another php page. I'd like to do that with an anchor tag, and with POST request. The block of code which should that is inserted before the first button (Submit), but in the same form:
HTML
<div class="col-md-4 col-xs-4">
<a onclick="goToOtherPage()" id="details" class="btn btn-success btn-lg btn-block">
<i class="fa fa-table"></i> Details
</a>
</div>
jQuery
function goToOtherPage() {
var otherPage = "details.php";
$.post(otherPage, {
'userID': $('#userID').val()
}
}
Adding window.location.href = 'details.php'
inside the function, right after passing userID, opens the new page, but without the parameters I'd like to pass.
I've thought about setting and unsetting cookies, and bypassing the whole POST/GET issue, but that's not a viable option.
Instead of cookies, you can use SessionStorage API to save some data on one page, and access that on another. Take a look at sessionStorage MDN.
Simply in your goToOtherPage
callback save any data as such:
sessionStorage.setItem('formData', {});
And later on, on the next page, you can access the data saved on the previous page via:
sessionStorage.getItem('formData');