1

I need to pass JavaScript(jQuery) variable through url, so I can catch it in $_GET with php. Here is what i have tried already:

$(document).ready(function(){
    var click=0;
    $('#next').click(function(){
        var count=$('#next').val();//ovdje imamo koliko je maksimalna proslidjena vrijednost
        var data = {};
        click++;
        if(click>=count){
            click=count;
        }
        data.clicks=click;

        window.location.replace('https://www.servis-racunara.net/pages/notifications_page.php?click='+click);
    });
});

Doesn't work.

$(document).ready(function () {
    var click = 0;
    $('#next').click(function () {
        var count = $('#next').val();//ovdje imamo koliko je maksimalna proslidjena vrijednost
        var data = {};
        click++;
        if (click >= count) {
            click = count;
        }
        data.clicks = click;
        $.get("notifications_page.php",
            { page: clicks },
            function () {

            });
    });
});

Doesn't work.

$(document).ready(function(){
    var click=0;
    $('#next').click(function(){
        var count=$('#next').val();//ovdje imamo koliko je maksimalna proslidjena vrijednost
        var data = {};
        click++;
        if(click>=count){
            click=count;
        }
        data.clicks=click;
        $.ajax({
            url: "notifications_page.php",
            type: "GET",
            dataType: "JSON",
            data: data,
            async: true,
            success: function (data) {

            }
        });
    });
});

This works, but strangely. It sends parameter, but I cannot catch it with $_GET in PHP. Please help if you have some idea how to handle this. UPDATE: When I look at Inspect tool under Network here is what I see: enter image description here

Everything works fine but I don't know how to catch this parameter because in my page here is how $_GET looks like: enter image description here

I think that page should be reloaded in order for $_GET to catch parameter. What do you think?

SECOND UPDATE: I found a way to put this variable click in url with this:

$(document).ready(function(){
        var click=0;
        $('#next').click(function(){
            var count=$('#next').val();//ovdje imamo koliko je maksimalna proslidjena vrijednost

            click++;
            if(click>=count){
                click=count;
            }

            $('a').attr('href','?id='+click);
});
        });

This last line does it all. But now my counter doesn't work, due to reloading of page(href attribute). Can you please help me now just to fix that?

10
  • 1
    Remove dataType: JSON ;) Commented Mar 29, 2016 at 10:34
  • can you please share the relevant part of notifications_page.php? Commented Mar 29, 2016 at 10:39
  • check your script properly go to "notifications_page.php" or not. If its work means first test echo msg. after use $_GET['clicks'] Commented Mar 29, 2016 at 10:41
  • @briosheje relevant part meaning what? Commented Mar 29, 2016 at 10:42
  • @Ognj3n : the part where you actually acquire the data ;) (the one with the $_GET) Commented Mar 29, 2016 at 10:42

2 Answers 2

0

You are passing a JS object as data. Perform a JSON.stringify on your data before passing it to the AJAX call and it will work. Example:

$.ajax({
        url: "notifications_page.php",
        type: "GET",
        dataType: "JSON",
        data: JSON.stringify(data),
        async: true,
        success: function (data) {

        }
});

Reference: Passing js object as json to jquery?

Sign up to request clarification or add additional context in comments.

Comments

0

try this structure in your ajax part,

    $.ajax({
      type: "GET",
      data: {
        "count": count
      },
      url: "notifications_page.php",
      success: function(data) {
        alert("Success");
      },
      error: function(data) {
        alert("fail");
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.