jwk811 Posted December 11, 2008 Share Posted December 11, 2008 I need to make an array of content which I will get from the database to put into the javascript. How? If I use AJAX, how? Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/ Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 json_encode That would be the best way. I would also recommend using jQuery (do a google for it). It makes AJAX a ton easier to do, and has examples! Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/#findComment-712715 Share on other sites More sharing options...
jwk811 Posted December 11, 2008 Author Share Posted December 11, 2008 json_encode That would be the best way. I would also recommend using jQuery (do a google for it). It makes AJAX a ton easier to do, and has examples! doesnt work with php 4 Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/#findComment-712773 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Better to code for the future, I would add this in one of your included pages <?php if (!function_exists('json_encode')) { function json_encode($a=false) { if (is_null($a)) return 'null'; if ($a === false) return 'false'; if ($a === true) return 'true'; if (is_scalar($a)) { if (is_float($a)) { // Always use "." for floats. return floatval(str_replace(",", ".", strval($a))); } if (is_string($a)) { static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; } else return $a; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) $result[] = json_encode($v); return '[' . join(',', $result) . ']'; } else { foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v); return '{' . join(',', $result) . '}'; } } } ?> That will do the exact thing that json_encode does, and you can use the same name. Then when you upgrade to PHP5 all your code should still work without having to change it. Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/#findComment-712781 Share on other sites More sharing options...
jwk811 Posted December 11, 2008 Author Share Posted December 11, 2008 Better to code for the future, I would add this in one of your included pages <?php if (!function_exists('json_encode')) { function json_encode($a=false) { if (is_null($a)) return 'null'; if ($a === false) return 'false'; if ($a === true) return 'true'; if (is_scalar($a)) { if (is_float($a)) { // Always use "." for floats. return floatval(str_replace(",", ".", strval($a))); } if (is_string($a)) { static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; } else return $a; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) $result[] = json_encode($v); return '[' . join(',', $result) . ']'; } else { foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v); return '{' . join(',', $result) . '}'; } } } ?> That will do the exact thing that json_encode does, and you can use the same name. Then when you upgrade to PHP5 all your code should still work without having to change it. nice thank you, i looked up the function but i still don't understand how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/#findComment-712904 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Read up on AJAX. You would create a page then populate an array, then echo the array after it has been json_enocoded'd. I would point you to jQuery, google that it makes AJAX a ton easier, plus you are likely to find some really good, clean examples of how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/136544-puttin-info-in-a-javascript-using-php/#findComment-712927 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.