Hardwarez Posted June 12, 2008 Share Posted June 12, 2008 How do I pass two variables from php to java script? I need to call a php page passing data in the url. i.e. myurl.com?data=123 Then The php page needs to return two variables back to the calling javascript. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 13, 2008 Share Posted June 13, 2008 I'm assuming this is AJAX? I typically just concatenate the values together with a delimiter and then send them to javascript as a single string. Then the javascript parses the string based on the delimeter. Just be sure to pick a delimeter that would not be included in the return values. PHP <?php $var1 = "foo"; $var2 = "bar"; $returnVal = $var1 . '|' . $var2; echo $returnVal; ?> JavaScript xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var varArray = xmlHttp.responseText.split('|'); document.myForm.var1.value=varArray[0]; document.myForm.var2.value=varArray[1]; } } Quote Link to comment Share on other sites More sharing options...
Hardwarez Posted June 13, 2008 Author Share Posted June 13, 2008 I barely know what AJAX is. I do not seem to be able to make this work. Quote Link to comment Share on other sites More sharing options...
Hardwarez Posted June 13, 2008 Author Share Posted June 13, 2008 This is the code I have set up to test with. Please tell what I am doing wrong. It returns undefined in the text box. <script language="LiveScript"> function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(action) { http.open('get', 'dynamic.php?job='+action); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; } } } function emailcheck(form) { /*.form.email.value */ var thing; thing=sndReq(form.email.value); document.myForm.email.value = thing; } </script> <?php echo '<form name="myForm"><B>Enter your Email address :</b><BR> <input type=text name="email" value="" onBlur="emailcheck(this.form)"> <BR></form> '; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 13, 2008 Share Posted June 13, 2008 Well, an AJAX implementation has many steps tothe process and if any fail the result will fail. Add some debuggin code to validate that values are what you expect at each step. The first thing I would do is add the following alert: function handleResponse() { if(http.readyState == 4){ var response = http.responseText; alert('AJAX Response: ' + response); var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; } } } That will tell you if the correct value is being returned from AJAX. If not, then you need to look further up in the process. Aslo, you coul do a quick check by going directly to the AJAX page in our browser: http://www.yourdomain.com/dynamic.php?job=testvalue Is the correct response displayed inthe browser? If not, the PHP page is malfunctioning. Quote Link to comment Share on other sites More sharing options...
Hardwarez Posted June 13, 2008 Author Share Posted June 13, 2008 Ok I am getting the data I wanted however the problem I seem to be having is splitting the two variables out. How to I access each var? Here is the code I have now: <script language="LiveScript"> function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(action) { http.open('get', 'dynamic.php?job='+action); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ var response = http.responseText; /*document.myForm.email.value=response;*/ var varArray = xmlHttp.responseText.split('|'); document.myForm.email.value=varArray[0]; /*document.myForm.email.value=varArray[1];*/ } } function emailcheck(form) { /*.form.email.value document.myForm.email.value = varArray[0]; */ if (form.email.value !=""){ sndReq(form.email.value); } } </script> <?php echo '<form name="myForm"><B>Enter your Email address :</b><BR> <input type=text name="email" value="" onBlur="emailcheck(this.form)"> <BR></form> '; ?> It should return the first of two vars into the form I typed in, but it does not Quote Link to comment Share on other sites More sharing options...
Hardwarez Posted June 13, 2008 Author Share Posted June 13, 2008 Never mind I got it. I was using a split that I pasted in from somewhere else. Thank you for your help!!!!!!!!!!!!!!!! Quote Link to comment 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.