1

I'm creating links with Mysql+PHP looping, but I need to add a javascript var into url href, like:

The javascript var is in a jquery cookie: $.cookie('limit')

urls.php:

<a href='page.php?id=1&limit=$.cookie('limit')'>1</a>
<a href='page.php?id=2&limit=$.cookie('limit')'>2</a>
<a href='page.php?id=3&limit=$.cookie('limit')'>3</a>

Put the javascript var into a hidden input doesn't work on this case.

In my page.php I need to use both vars (id and limit) on a mysql query. So insert this javascript var in a hidden input in page.php won't work anyway.

I tried to remove limit var from href url and add this on my page.php but it didn't work:

if(!empty($_REQUEST['limit']){
    $_REQUEST['limit'] = "<script type='text/javascript'>document.write($.cookie('limit'))</script>";
}
3
  • I don't quite understand what you're trying to achieve. Could you clarify? Commented Apr 5, 2012 at 22:04
  • How does JavaScript interact with the HREF of your links? Does it create them? Does it read them? Commented Apr 5, 2012 at 22:04
  • Right, will try to explain better. Thanks for correction Josh Moore. Commented Apr 5, 2012 at 22:23

3 Answers 3

2

You did not put the variable correctly into php. With your code you just wrote limit inside a string. You need to connect variable to a string like this:

if(!empty($_REQUEST['limit']){
    $limit = "<script type='text/javascript'>document.write('".$_REQUEST['limit']."')</script>";
}
2
  • I didn't understand how it would work, but I think that was me who made a bad question. Edited my question. Thanks for help anyway! Commented Apr 5, 2012 at 22:27
  • Well, you had some string in $_REQUEST['limit'] variable, and you wanted it in your string (which would client side be executed as JS). In your case JS would be: ...document.write($.cookie('limit'))..., but I told you how to implement the value od the variable into JS (for instance if the value of $_REQUEST['limit'] is 'limit_value' it would be: ...document.write($.cookie('limit_value'))...) Commented Apr 6, 2012 at 20:38
1

If the limit's being passed into the page that you're constructing links on, then you could grab that number through the $_REQUEST variable like you mentioned. You could then write a for loop in the logic in that page to create the number of links that you want. In that loop you could construct something like this to echo the url onto the page:

echo "<a href='page.php?id={$i}&limit=jscriptVar'>{$i}</a>"
1
  • Thanks Josh Moore for correction. I edited my question to try to explain better what I'm trying to do. Commented Apr 5, 2012 at 22:28
0

Change links to this:

<a href='page.php?id=1' class='changeMe'>1</a>
<a href='page.php?id=2' class='changeMe'>2</a>
<a href='page.php?id=3' class='changeMe'>3</a>

Add a javascript like this:

$(document).ready(function(){
    $('a[class="changeMe"]').each(function(){
        var newHref = $(this).attr("href") +"&limit="+ $.cookie('limit');
        $(this).attr("href", newHref);
    });
});

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.