mvasco
mvasco

Reputation: 5101

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.

Upvotes: 2

Views: 559

Answers (3)

mega6382
mega6382

Reputation: 9396

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();
    });
});

This submits a dynamically created form instead of sending 2 separate requests like redirect and ajax.

Upvotes: 5

Lars Peterson
Lars Peterson

Reputation: 1508

The problem is that you are both redirecting and requesting information from the same URL . So I believe it works originally, but after the redirection, it clears the value.

A better solution would be to simply add the information to the URL you are redirecting to like this:

window.location = "ondemand_driver.php?id=" + id;

If, of course, you don't mind the user seeing that.

If you do need secrecy, you could store the value in localStorage with a mask, and retreive it after the page redirect.

Upvotes: 2

Pankaj Makwana
Pankaj Makwana

Reputation: 3050

Try like this, I have done it before and it's working

$(document).on("click", ".btndriver", function () {
    var id = $(this).attr("id");
    var nombre = $(this).attr("nombre");
    var url = "ondemand_driver.php";
    swal.queue([{
            title: "Select Driver?",
            text: "Select Driver? : " + nombre + " ?",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!',
            preConfirm: function () {
                return new Promise(function (resolve) {
                    var value = {
                        'id': id
                    };
                    $.ajax({
                        type: "POST",
                        data: value,
                        url: "ondemand_driver.php",
                        success: function () {
                            window.location = url;
                        },
                        error: function () {

                        }
                    })
                })

            }
        }])
})

Upvotes: 1

Related Questions