admin管理员组文章数量:1323694
I'm trying to use PHP to echo a set of buttons (buy & sell) in a stock trading game. The buttons will eventually have an onclick= event handler that calls a JavaScript function to populate some fields on a form, but I'm just trying to get an alert() with one piece of data to work. I just can't get the results from my echo statement to print the alert correctly, so it doesn't even trigger. I've also used print_r ($stockInfo) to ensure my array is properly filled with test data.
Here's my array:
$stockInfo = array(
array('name' => 'Company 1', 'description' => 'Description 1', 'price' => '100', 'yield' => '.05'),
array('name' => 'Company 2', 'description' => 'Description 2', 'price' => '89', 'yield' => '.06'),
array('name' => 'Company 3', 'description' => 'Description 3', 'price' => '110', 'yield' => '.03')
);
And my PHP code that attempts to generate a button:
<?php
echo <<<EOT
<button id="buy" class="btn btn-primary btn-small" onclick="alert('$stockInfo[0]["name"]');">Buy</button>
EOT;
?>
The result of that code is:
<button id="buy" class="btn btn-primary btn-small" onclick="alert('Array["name"]');">Buy</button>
I just can't figure out why I can't get even a simple alert to fire. Running:
echo $stockInfo[0]["name"];
Correctly prints out "Company 1", but it doesn't print that text in the argument of the alert().
I've read Stackoverflow, lots of web articles, and experimented for the past day and I just can't figure this out. If anyone can provide any help, I'd GREATLY appreciate it.
Thanks!
I'm trying to use PHP to echo a set of buttons (buy & sell) in a stock trading game. The buttons will eventually have an onclick= event handler that calls a JavaScript function to populate some fields on a form, but I'm just trying to get an alert() with one piece of data to work. I just can't get the results from my echo statement to print the alert correctly, so it doesn't even trigger. I've also used print_r ($stockInfo) to ensure my array is properly filled with test data.
Here's my array:
$stockInfo = array(
array('name' => 'Company 1', 'description' => 'Description 1', 'price' => '100', 'yield' => '.05'),
array('name' => 'Company 2', 'description' => 'Description 2', 'price' => '89', 'yield' => '.06'),
array('name' => 'Company 3', 'description' => 'Description 3', 'price' => '110', 'yield' => '.03')
);
And my PHP code that attempts to generate a button:
<?php
echo <<<EOT
<button id="buy" class="btn btn-primary btn-small" onclick="alert('$stockInfo[0]["name"]');">Buy</button>
EOT;
?>
The result of that code is:
<button id="buy" class="btn btn-primary btn-small" onclick="alert('Array["name"]');">Buy</button>
I just can't figure out why I can't get even a simple alert to fire. Running:
echo $stockInfo[0]["name"];
Correctly prints out "Company 1", but it doesn't print that text in the argument of the alert().
I've read Stackoverflow, lots of web articles, and experimented for the past day and I just can't figure this out. If anyone can provide any help, I'd GREATLY appreciate it.
Thanks!
Share Improve this question asked Mar 19, 2012 at 21:21 Kyle CarlsonKyle Carlson 8,1735 gold badges38 silver badges44 bronze badges3 Answers
Reset to default 5To use variables in heredoc syntax, you have to put curly brackets around them:
<?php
echo <<<EOT
<button id="buy" class="btn btn-primary btn-small" onclick="alert('{$stockInfo[0]["name"]}');">Buy</button>
EOT;
?>
Ideally, you don't want to put variables directly into your output like that because if there's just a single '
in that string your JavaScript will screw up.
Instead, avoid both problems by not using HEREDOC and using the proper method:
echo "<button id=\"buy\" class=\"...\"
onclick=\"alert(".htmlspecialchars(json_encode($stockInfo[0]['name'])).");\">Buy</button>";
json_encode
essentially makes any PHP variable (except resources) able to be dumped directly into JavaScript code.
htmlspecialchars
makes the result safe to use in an HTML attribute.
echo <<<EOT
<button id="buy" class="btn btn-primary btn-small" onclick="alert({$stockInfo[0]["name"]});">Buy</button>
EOT;
?>
本文标签: Echo JavaScript alert of a PHP array value on an HTML buttonStack Overflow
版权声明:本文标题:Echo JavaScript alert of a PHP array value on an HTML button? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136115a2422378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论