duanluan2047 2017-01-03 19:47
浏览 54
已采纳

[PHP,HTML]链接到页面的按钮,php变量不起作用

I've been trying to use a button as a link to add some interactivity to my site, however for some reason it doesn't work.

I've been using this code for a search bar:

<span>
<input type="text" id="search" placeholder="Zoek naar een boek of auteur" name="search" method="get" autocomplete="off" style="width:25%;" value="<?php echo htmlspecialchars($search); ?>"></input>
        <button type='submit' style="vertical-align:middle" id='filterButton' onclick="<?php echo htmlspecialchars("'?search=$_GET[search]'");?>">
    <span>Zoek</span>
</button>
</span>

This works perfectly fine, when I click the button, I go to the search page with the variable. In the search results, I use the same method to make a "more info" button, redirecting the user to a page with, well, more information about a book. This is the code in this part

<span>
    <button type='submit' style="vertical-align:middle" id='infoButton' onclick="<?php echo htmlspecialchars("book.phph?id=$resultsid");?>">
    <span>Meer info</span>
</button>
</span>

But this doesn't work, the button does respond when I click it (I get the animation), but I don't get redirected to the page. I've tried a different method:

<span>
<form action="<?php echo htmlspecialchars('book.php?id=$resultsid');?>">
    <button type='submit' style="vertical-align:middle" id='infoButton' />
        <span>Meer info</span>
    </button>
</form>
</span>

This redirects me to "book.php?", but that's it. It ignored the id=$resultsid behinf the question mark. I can't get it to work, can somebody help please?

Thanks ~DagelijksGamer

  • 写回答

2条回答 默认 最新

  • dongtingxiao4697 2017-01-03 21:35
    关注

    Your last example may work, removing the parameter from the form action and adding an hidden input field:

    <form action="<?php echo htmlspecialchars("book.php");?>" method="GET">
        <input name="id" type="hidden" value="<?php echo $resultsid; ?>" />
        <button type='submit' style="vertical-align:middle" id='infoButton' />
                <span>Meer info</span>
        </button>
    </form>
    

    These answers explain why the part after "?" gets ignored in your last example:

    https://stackoverflow.com/a/1116026/3052648

    https://stackoverflow.com/a/9882750/3052648

    Note that you didn't specify a method in your form, and it defaults to GET.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择