0

Possible Duplicate:
Javascript echo’d by PHP doesn’t run

In the following code I am parsing values from a php table into a javascript function, however I am getting an Uncaught ReferenceError: "the php text" is not defined (anonymous function)

<html>
    <script type="text/javascript">
        function draw(name ) {
            alert(name);
        }
    </script>

    <body>
        <canvas id="mycanvas" width=800 height=400></canvas>
    </body>
</html>

<?php
    $query = 'SELECT * FROM graph_table';
    $result = mysql_query($query);
    while($val = mysql_fetch_array($result)) {
        $name =  $val['test_name'];
             echo '<script type="text/javascript">    draw('.$name .');          </script>';
        }
?>
2
  • 1
    Have a look at the generated JavaScript code. I'm sure it looks like draw(sometext), where you actually want to have draw("sometext"). Commented Oct 12, 2012 at 10:35
  • Get the view source of the page Commented Oct 12, 2012 at 10:35

3 Answers 3

4

You need to add quote's between draw since you're passing text to it. draw(\''.$name.'\'); will make your problem go away.

Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thankyou so much, when I was parsing an int there wasn't a problem so I figured it must be a string thing but I didn't know how to fix it.
0

Try this

<script type="text/javascript">
    <?php
    $query = 'SELECT * FROM graph_table';
    $result = mysql_query($query);
    while($val = mysql_fetch_array($result)) {
        $name =  $val['test_name'];
             echo "draw('$name');";
        }
?>

</script>

Comments

0

Try the php script inside . Mainly you should add js script just before .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.