0

I have a php query like so:

<?php
    $query = "SELECT * FROM " . $usertable . " ORDER BY fname;";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        echo '<option value="' . $row['pkid'] . '">' . $row['fname'] . ' ' . $row['lname'] . '</option>';
    }
?>

Within this same .php file I have some javascript. All I would like to do is return $row['fname'] as a a javascript variable.

Is this possible?

Nubtacular
  • 1,367
  • 2
  • 18
  • 38
  • **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 16 '15 at 19:00
  • Just a codebase took over, but I will look into prepared statements and PDO, thanks. – Nubtacular Jan 16 '15 at 19:02

1 Answers1

2

If you want to output the PHP variable in a JavaScript variable you could do something like this -

echo '<script>var name+' . $row['pkid'] . ' = ' .$row['fname'] . ';</script>';

This would give you a uniquely named variable for each row.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119