Reputation: 243
I'm working on a user management table. It has a column of buttons of either "Validate" or "Revoke" based on if the user is already validated or not in database. e.g.
<td><button type="button" class="btn btn-primary btn-md" onclick= "Validate(this)" userID={{user.id}}>Validate</button></td>
When the user click on the button, it will redirect to the function below that call window.location.href to redirect a new route
function Validate(user) {
var id = user.getAttribute("userID")
window.location.href = ("/userManagement/validate/" + id), true
}
The problem is, window.location.href do the GET instead of POST. How do I modify it, or is there any alternative way to redirect to my another route with the POST request?
Thanks.
Here is what I tried based on the suggestion:
$('#inset_form').html('<form action="/userManagement/validate/' + id + '" name="validate" method="post" style="display:none;"><input type="text" name="api_url" value="' + Return_URL + '" /></form>');
document.forms['vote'].submit();
or
var url = "/userManagement/validate/" + id;
var form = $('<form action="' + url + '" method="post">' +
'<input type="text" name="api_url" value="' + Return_URL + '" />' +
'</form>');
$('body').append(form);
form.submit();
it does not redirect/reload the page, and I also don't think it access to the new route. Also, I tried,
var addr = "/userManagement/validate/" + id ;
var xhr = new XMLHttpRequest();
xhr.open('POST', addr, true);
xhr.send();
Same issue as above.
Upvotes: 2
Views: 14089
Reputation: 28196
As you actually want a redirection of the current route and not some kind of background dialogue (which would typically involve an Ajax post request) it seems to me that the approach involving a form and actually submitting it is the natural choice here, as already mentioned by @hugo411 in an earlier answer.
For that you should modify your approach a little:
method="post"
), but only once. Depending on which other input fields you also want to post with each submit you should wrap it around either your whole table or each individual row.action
property of the (parent) form before submitting it.Html:
<form class="myrouter" method="post">
<table>...
...<td><button type="button" class="btn btn-primary btn-md" userID={{user.id}}>Validate</button></td> ...
... </table>
</form>
edited:
JavaScript (in the jquery $(function(){ ... })
-section):
$('form.myrouter').on('click','button',function(ev){
// some debugging stuff, if necessary ...
console.log('current id:',this.userId);
// in the current context "this" points to the clicked button
$(this).closest('form').attr('action','/userManagement/validate/'+this.userId).submit();
// finds the form object, changes the "action" and submits it
}
In this edited version the id
is taken from the userId
attribute of each button. Binding the event-function with on()
gives you the flexibility of adding further buttons into the form dynamically. They will then automatically be bound to the same event function.
Upvotes: 1
Reputation: 346
You can use ajax in order to submit with POST
var id = user.getAttribute("userID")
$.ajax({
url: '/userManagement/validate/',
data: { userid : id },
type: 'POST',
success: function (response) {
alert(response);
},
error: function(e){
alert('Error: '+e);
}
});
But off course this will not refresh your page, you can handle the response and then redirect. If you want to stick with your stuff, you need a form submit around that button.
<form action="/userManagement/validate/" method="post">
<input type="hidden" id="userid" name="userid" value="{{user.id}}">
<button type="submit">
</form>
Upvotes: 0
Reputation: 3350
You will want to use jQuery AJAX: Asynchronous JavaScript and XML, if you want to submit a form without actually submitting it. It is possible to do this in traditional JavaScript, but jQuery makes things like this much easier. Have a look at this reference page. Here's the one for .post()
, which does virtually the same thing.
Upvotes: 0