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>";
}
-
I don't quite understand what you're trying to achieve. Could you clarify?user736788– user7367882012年04月05日 22:04:25 +00:00Commented Apr 5, 2012 at 22:04
-
How does JavaScript interact with the HREF of your links? Does it create them? Does it read them?Jasper– Jasper2012年04月05日 22:04:39 +00:00Commented Apr 5, 2012 at 22:04
-
Right, will try to explain better. Thanks for correction Josh Moore.Leo– Leo2012年04月05日 22:23:15 +00:00Commented Apr 5, 2012 at 22:23
3 Answers 3
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 Comments
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 Comment
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);
});
});