What I want to achieve: I need to use POST method then after success, it will locate to another url of ajax along with the POST data.
This is what I have done so far:
<script>
$("#btnSubmit").click(function(){
var ddate = $("#ddate").val();
var empid= $("#empid").val();
$.ajax({
type:'POST',
url:'../PaySlip/view_payslip.php',
data:{numnumnum:empid,dateito:ddate},
success:function(){
window.location ="../PaySlip/view_payslip.php";
}
});
});
Problem: Upon success, the view_payslip cannot gather the POST data that I have transferred. I know what I did was wrong. That is why I'm asking for help to how to get this right.
Thank you in advance.
What you need to do is to post dynamic form in success callback. I have also similar requirement in one of my project and for that I have created a method similar to shown below.
In this method you need to pass URL, windows option and params which is your data object, name is the window name, if you want to open in same window then instead of opening a new window you can pass blank in name param and omit the window.open() statement. You can modify it as per your requirement.
function submitDynamicForm(url, windowoption, name, params)
{
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", url);
form.setAttribute("target", name);
for (var key in params) {
if (params.hasOwnProperty(key)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = params[key];
form.appendChild(input);
}
}
document.body.appendChild(form);
window.open("", name, windowoption);
form.submit();
document.body.removeChild(form);
}