mvasco mvasco - 23 days ago 9
PHP Question

No data pass to URL after Ajax function

This is the script I am using to pass a parameter to another PHP page using Ajax.

$(document).on( "click",".btndriver", function() {

var id = $(this).attr("id");

var nombre = $(this).attr("nombre");
var url = "ondemand_driver.php";
swal({
title: "Select Driver?",
text: "Select Driver? : "+nombre+" ?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "GO",
closeOnConfirm: true },
function(){
var value = {
'id': id
};
$.ajax(

{
url : "ondemand_driver.php",
type: "POST",
data : value,
success: function() {

window.location=url
}
});
});
});


But now the url is not receiving any parameter.

To check it, I have included:

<?php echo $_POST['id'];?>


at ondemand.driver.php, and no value is shown.

I have also checked the value for var id, and it is 45.

Thank you in advance.

Answer Source

Try the following solution:

$(document).on( "click",".btndriver", function() {

    var id = $(this).attr("id");

    var nombre = $(this).attr("nombre");
    var url = "ondemand_driver.php";
    swal({   
    title: "Select Driver?",   
    text: "Select Driver? : "+nombre+" ?",   
    type: "warning",   
    showCancelButton: true,   
    confirmButtonColor: "#DD6B55",   
    confirmButtonText: "GO",   
    closeOnConfirm: true }, 
    function(){   
      var value = {
        'id': id
      };
        var newForm = $('<form>', {
            'action': url,
            'target': '_top',
            'method': 'POST'
        }).append($('<input>', {
            'name': 'id',
            'value': id,
            'type': 'hidden'
        }));
        $(document.body).append(newForm);
        newForm.submit();
    });
});

submit a dynamically created form instead of sending 2 requests. e.g redirect and ajax.