douwu8060 2017-05-20 17:47
浏览 57
已采纳

将变量连接到PHP中的链接

I would like to link a variable to a link. The variable looks something like this which I grab from json. The json variable for $movieSeat is : A1,A2,B3,V4

Below is the code that I've tried out. The issue that I am currently having is, the link doesn't seem to be working. It seems like something to do with . and +. I'm also not too sure when to use . and + for the linking.

<?php
    echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=" . $movieSeat. "'; } </script>";       
    for ($i = 0; $i < $jsonCounter; $i++) {
    $movieSeat =  $jsonValue[$i]->seatID;      
    echo "<button onclick = movieBTN(".$movieSeat.")> Movie Seat </button>";        
    }

    ?>
  • 写回答

1条回答 默认 最新

  • dongshuzui0335 2017-05-20 17:57
    关注

    First of all, if you're echoing the function from PHP, make sure you use JS variables and not PHP's:

    echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; } </script>";
    

    So it will output:

    <script> 
        function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; } 
    </script>
    

    Next, you need to wrap the variable passed to that function with ':

    echo "<button onclick = movieBTN('".$movieSeat."')> Movie Seat </button>"; 
    

    So it will output as:

    <button onclick = movieBTN('A1')> Movie Seat </button>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题