Reputation: 31
There is something wrong with the way I am printing MySQL and the javascript is not executing it. Here is an example.
square[1] = "asdfasdfadsf";
When I print "asdfasdfadsf
" from my MySQL database, the javascript does not work. However, if I simply type "asdfasdfadsf" in the static HTML it executes fine. I have tried as many PHP functions and charset conversions as I can. Please Help!
Upvotes: 3
Views: 109
Reputation: 270617
You need to add quotes around the string from the database.
// Add quotes around the call which prints the vale from PHP.
// this turns it into a JavaScript string.
square[1] = '<?php echo "asdfasdfadsf"; ?>';
//----------^^----------------------------^^
// Or...
square[1] = '<?php echo $row["value_from_your_db"]; ?>';
Note: json_encode()
is recommended if this is anything more than a simple string that may have internal quotes of its own requiring additional escaping.
Upvotes: 3
Reputation: 131
I suggest the json_encode
PHP function. Apart from properly printing the string, it also escapes all dangerous characters.
square[1] = <?php echo json_encode($my_string); ?>;
Upvotes: 5