Jump to content
Sign in to follow this  
Nati Aizikovitz

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

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.

Share this post


Link to post
Share on other sites

Your PHP code is calling an undefined function, so it's showing an error message which is not valid JSON.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...