Recover URL variable and pass on in AJAX

Asked

Viewed 2,713 times

2

I wonder if there is a way after recovering a variable from the URL pass to another page and return the result in a load. I’m using the following script that normally recovers the variable, but does not move to the next page. It is just to better understand what I need.

In the URL http://noticias.php?categoria=23, take the variable normally but in the load comes the information that the page resposta.php did not receive the variable. I would like to move to another page before displaying the result on load. What would be the right way?

$(document).ready(function()
{
    $('#teste').html("<img src='js/load.gif'>");
    var variaveis=location.search.split("?");
    var quebra = variaveis[1].split("=");
    var categoria = + quebra[1];
    var URL='resposta.php';
    var dataString = 'categoria=' + categoria ;

    $.ajax({
        type: "POST",
        url: URL,
        data: dataString,
        cache: false,
        success: function(html){
            $('#teste').load('resposta.php');
        }
    });
});
</script>
  • 1

    use the window.Location to receive the parameters

  • receive I need to resend before showing the result in load

2 answers

1

The correct way to send to the parameter data this is how.

Jquery

$.ajax({
    type: "POST",
    url: URL,
    data: categoria,
    cache: false,
    success: function(html){
       $('#teste').load('resposta.php');
    }
});

or in the format of a Array

Jquery

$.ajax({
    type: "POST",
    url: URL,
    data: { meuVar : categoria },
    cache: false,
    success: function(html){
       $('#teste').load('resposta.php');
    }
});

And in PHP would receive this way

PHP

$categoria = $_POST["meuVar"];

1

I don’t know if this will help, but you can pass the amount by get with the $.ajax like this:

var URL='resposta.php?'+dataString;

$.ajax(
{
url: URL,
cache: false,
success: function(html){
    $('#teste').load('resposta.php');
}
});

If I’m not mistaken, you can remove the $.ajax type pq by default it already works with GET.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.