通过button onclick调用PHP变量


Calling a PHP variable through button onclick

目标是在单击按钮时调用变量$url。有了这段当前代码,当按钮被点击时,什么也没有发生,错误提示是意外的令牌。

     <?php
        $url = "edit_beginningcakephp.php?title=$title&author=$author&isbn=$isbn
                &year=$year&pages=$pages&price=$price";
            >?
  <input type="button" value="Edit Book" onclick="window.location.href='<?= $url ?>'">

我想你没有正确编码变量的内容(称为XSS)

您需要对变量的内容进行urlencode。另外,&需要作为HTML实体&amp;输入。

试题:

$url = 'edit_beginningcakephp.php?title='.urlencode($title).'&amp;author='.urlencode($author).'&amp;isbn='.urlencode($isbn).'&amp;year='.urlencode($year).'&amp;pages='.urlencode($pages).'&amp;price='.urlencode($price);

PS: >?应该是?>,并确保URL不包含任何换行符

">? "这看起来可能是个问题,肯定会在本地抛出语法错误:)

虽然MrTux的回答非常好,他的建议当然应该遵循,但您的代码的实际问题是URL中的返回。url不能包含返回!
处理方法:删除返回。还有空格。请听从tux先生的建议。