0

My work host has an old version of PHP and it's not supporting json_encode(), so I need another way to pass array of strings from PHP to JavaScript, can't figure out how.... please help.

This is my current working code that's needed "downgrade":

PHP:

$arr = array($first_name,$last_name, $phone, $email, $number);
echo json_encode($arr);

JAVASCRIPT (ignore the missing brackets):

function get_repeat_request_details(request_id){
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var arr = JSON.parse(xmlhttp.responseText);
            document.getElementById("tb_f_name").value = arr[0];
            document.getElementById("tb_l_name").value = arr[1];
            document.getElementById("tb_phone").value = arr[2];
            document.getElementById("tb_email").value = arr[3];
            document.getElementById("tb_number").value = arr[4];
5
  • 3
    why downgrade? implement your json_encode. It's not that difficult (I bet there are some implementations already) Commented Jul 23, 2012 at 14:26
  • php.net/manual/en/function.json-encode.php there is a custom implementation in the comments that works great Commented Jul 23, 2012 at 14:27
  • Just use XML. It's very standard in the industry. Commented Jul 23, 2012 at 14:27
  • possible duplicate of this question Commented Jul 23, 2012 at 14:28
  • Thanks, seems like a duplicate issue, couldn't use that first post's solution though. PHP upgrading or touching on host is not an option sadly... Commented Jul 23, 2012 at 14:37

1 Answer 1

1

What about using: http://www.dzone.com/snippets/jsonencode-alternative-php-4

It will detect if json_encode already exists, but if not, then provides an alternative.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.