doukun0888 2010-12-25 22:24
浏览 36
已采纳

无法根据PHP值进行HREF更改

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=" . $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

  • 写回答

4条回答 默认 最新

  • dongwei2610 2010-12-25 22:35
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题