Can't make HREF change based on PHP value
I want to retrieve the user's location and then show a link that points to an URL that changes according to that location. I just want to place the user's city name at the end of the HREF.
I need this work on my wordpress site, on a static page. I use a plugin called Exec-php which let's me run PHP in pages.
I have a plugin that provides me the user's city through the shortcode "[mmjs-city]".
I tried to make it work through different paths but I never get the link to work.
Here I tried assigning that shortcode to a value,
<?php
$city= "[mmjs-city]";
echo $city;
echo "<a href='?s开发者_如何学Python=" . $city ."'>Search for your city</a>";
?>
I added the first two lines to check whether the shortcode is working or not and if it's correctly assinged to the value $city. That part works. Then it creates the link and put's the value $city at the end of it. But when trying it instead of taking me to:
/?s=new+york
It takes me to:
/?s=%3Cscript%20language=%22javascript%22%3Edocument.write(geoip_city());%3C/script%3E
I have no idea what to do. I would be really thankful for any info on how to make it work, it's really an important feature for my site. Please ask for any further info or idk anything.
Also this is where I tested that code:
http://chusmix.com/?page_id=1129
Thanks
The problem is that the $city actually outputs a piece of javascript. The browser then executes the function and this writes the user's city to the screen.
<script language="javascript">document.write(geoip_city());</script><br/><br/><a href='?s='>Search for your city</a>
If you use this in your href, this will literally link to <script language="javascript">document.write(geoip_city());</script>
which causes the URL as you show.
You are using Javascript and PHP through eachother. If you want to create a link with the city, have the link created (or be modified by) javascript: locate the link (give it an ID or find it in the DOM) and assign the href, or print the link itself using javascript (instead of the A):
<script language="javascript">
document.write('<a href="?s=' + geoip_city() + '">Search for your city</a>');
</script>
If you check the HTML-code of the page you'll probably see the
<script language="javascript">document.write(geoip_city());</script>
which is interpreted as JavaScript-code by the browser to show your city correctly. However, the browser won't change it in the link as it's inside quotes there causing the browser to print or use the string as-is.
In other words, the JavaScript-code is not executed in the link.
Hey Liso, This is because the short-code gets manipulated by the WP plugin and is outputted as a javascript. So, when you access the shortcode via the above method, the actual value being assigned is this javascript.
Your problem is that the location is not from PHP. All the shortcode does is put in some javascript that goes to get the location and then prints it.
<script language="javascript">document.write(geoip_city());</script>
What you can do is use some other method of finding the location, based on IP address. Something like this: http://ipinfodb.com/ip_location_api.php
Or you can write some javascript that uses the geoip_city()
function that your plugin supplies and change the links of the URLs that need to be changed. If the URL that needed to be changed was in a link with class location_link
(and you are using jQuery), you could do:
$('.location_link').each(function(i,v) {
$(v).attr('href', $(v).attr('href') + geoip_city());
});
The link would initially have to be just ?s=
for this to work. This code would need to be done after all the page elements have loaded - you can use jQuery's ready
function for that: http://api.jquery.com/ready/
精彩评论