首页 > 解决方案 > 如何使用 onclick 使用 PHP 调用 javascript 函数

问题描述

我需要将 PHP 中定义的变量传递给 javascript 函数。JS 函数需要通过按钮 onclick 事件调用。

<button type="button" style="width: 70px;" class="btn btn-light btn-sm Center"
                  onclick="<?php  echo '<script>purchase('{$user}','{$painting1_Title}','200')</script>'?>">Purchase</button>

我尝试了单引号和双引号的不同含义,但出现解析错误:

解析错误:语法错误,意外的 '','' (T_CONSTANT_ENCAPSED_STRING),需要 ',' 或 ';' 在第 303 行的 /Applications/XAMPP/xamppfiles/htdocs/ProjectWebsite/Mainpage2.php

所以我的问题是如何在 onclick 事件中使用 PHP 调用 javascript 函数?

标签: javascriptphponclick

解决方案


您始终可以在其他引号内转义引号,以防止引号嵌套的噩梦。

"This is how you would \"fix\" certain \"quotation issues\""

我的看法是使用在双引号内转义的单引号,即使通常单引号在双引号内工作。

"This is how you would \'fix\' certain \'quotation issues\'"


推荐阅读