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!