Jump to content

Pass an multi variables array from php file to javascript external file


Nati Aizikovitz

Recommended Posts

php code:

$array = array($variable-1, $variable-2, $variable-3, ….. variable-n);

echo jason_encode($array);

 

javascript code:

var ar = new XMLHttpRequest();

ar.onreadystatechange = function(){

if(this.readyState == 4 && this.status == 200){
 var obj = JSON.parse(this.responseText);
             document.forms["form1"]["fnameMsg"].value = obj[0];
             document.forms["form1"]["lnameMsg"].value = obj[1];
             document.forms["form1"]["mphoneMsg"].value = obj[2];

}

}

The above code create an error:

Uncaught SyntaxError: Unexpected token < in JSON at position 0

Can someone help me to understand what is wrong with the above code and how to correct it.

Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...