Why don't you do a json_encode
. json_encode
will convert your PHP variables into a variable that's usable by JavaScript. This also might remove the need to use html_entity_decode
on most occasions, since this is not really something you should be doing to convert something that will be used by JavaScript. mysql_real_escape_string
is not needed at all.
<script type='text/javascript'>
var postQuote = new Array();
postQuote[<?php echo json_encode($post['post_id']); ?>] = <?php echo json_encode($post['post_text']); ?>
</script>
I would also set them as separate variables so that they're easier to debug and keep track of:
<script type='text/javascript'>
var postQuote = new Array();
var postQuoteKey = <?php echo json_encode($post['post_id']); ?>;
var postQuoteValue = <?php echo json_encode($post['post_text']); ?>;
// See what the key and value are
console.log(postQuoteKey);
console.log(postQuoteValue);
postQuote[postQuoteKey] = postQuoteValue;
</script>
The following example works as expected:
<script>
// Number
console.log(<?php echo json_encode(1); ?>);
// String
console.log(<?php echo json_encode("hello"); ?>);
// Boolean
console.log(<?php echo json_encode(false); ?>);
// Boolean
console.log(<?php echo json_encode(true); ?>);
// Array
console.log(<?php echo json_encode(array(1,2,3)); ?>);
// Outputs a JavaScript object
console.log(<?php echo json_encode(array("a" => 345, "b" => '242', "c" => 'hello')); ?>);
</script>