Passing a variable from php to javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sjk1000
Forum Commoner
Posts: 26
Joined: Tue Nov 11, 2008 8:50 am

Passing a variable from php to javascript

Post by sjk1000 »

Hi all
I think I'm missing the point here. Why can't I pass the $hello string to a javascript alert function? If i replace the $hello with 'hello world' it's all good. I've only today started tinkering with javascript so go easy on me. If you know of a good tutorial (video would be nice) please let me know.
Thanks, Steve

Code: Select all

<?php
$seller_count = 5;
$basket_items = 10;
for ($row=0; $row <=$basket_items; $row++)
{
    echo "<TR>";
    for ($column=0; $column <= $seller_count; $column ++)
    {
    $to_display = $row."--".$column.":";
    $hello ='hello world';
    ?> <TD> <?php echo $to_display ?> <input type=radio name="a" onclick= "alert($hello);"></TD> <?php
    }
    echo "</TR>";
}
?>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Passing a variable from php to javascript

Post by Reviresco »

Code: Select all

<?php
$seller_count = 5;
$basket_items = 10;
for ($row=0; $row <=$basket_items; $row++)
{
    echo "<TR>";
    for ($column=0; $column <= $seller_count; $column ++)
    {
    $to_display = $row."--".$column.":";
    $hello ='hello world';
    ?> <TD> <?php echo $to_display; ?> <input type=radio name="a" onclick= "alert('<?php echo $hello; ?>');"></TD> <?php
    }
    echo "</TR>";
}
?>
sjk1000
Forum Commoner
Posts: 26
Joined: Tue Nov 11, 2008 8:50 am

Re: Passing a variable from php to javascript

Post by sjk1000 »

Thanks for modifying that Reviresco. It's been driving me nuts for the last hour
Cheers, Steve
Post Reply