2

I'm making a next page system in which you click on 'next' and it takes you to the next set of elements.

This is the button in php:

    <input id="nextbtn" type="button" value="Next"></input>

This is the jquery function:

$('#nextbtn').click(function(){

});

The URL is: www.domain.com?var1=0&var2=20

I want to increment var1 and var2 by 20 everytime the 'next' button is clicked. Please tell me the code I should write in the jquery function so that this works.

3
  • jQuery is not needed for that. You can retrieve the values with PHP ($_GET array) and calculate the next URL based on that. Commented Jan 19, 2014 at 18:11
  • Considering you mentioned that you're using PHP, you won't even need to do this calculation in JavaScript at all, but rather in PHP. Commented Jan 19, 2014 at 18:28
  • You are right but the url will change on button click, the page will refresh and the variables $var1 = $_GET['var1'] will get the new values. I need to know how to change the query string on button click. Commented Jan 19, 2014 at 18:36

1 Answer 1

1

You can get query string current value:

function getQueryStringParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Now call this function in your next button click and add increment value:

var var1 = parseInt(getQueryStringParameterByName('var1'),10)+20;
var var2 = parseInt(getQueryStringParameterByName('var2'),10)+20;

and set it to url and redirect.

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

3 Comments

ok, the variable var1 and var2 are to be placed in the click function. Since my page also consists of php, where do I place the function getQueryStringParameterByName(name)?
It worked. To set the variables in the url, I used: var hrf="www.domain.com/x.php?var1=" + var1 + "&var2=" +var2; document.getElementById("a_link1").href=hrf;
what if domain can be both http and https

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.