Jump to content

[SOLVED] How do I pass two variables from php to java script?


Hardwarez

Recommended Posts

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];
  }
} 

Link to comment
Share on other sites

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>
';

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.