Page 1 of 1

Multiple xmlHttp.responseText in Ajax

Posted: Wed Mar 11, 2009 12:19 pm
by ullasvk
I have a page where onclick i send a value to another page from which i get a response.
These things are working fine, but now the problem i want tob update two textbox on single click with different value. Can any one please help me. What i actually mean is i am able to update a single textbox through xmlHttp.responseText but i want to update 2 textbox(a,b)with a single click.
Here is the code. This is actually a representation of my code.

Code: Select all

 
 [b]Page:abc.php[/b]
<script>
var x ;
function new_bun_no(x)
{
var s = document.getElementById("s"+x).value;
var xmlHttp;
try
  {xmlHttp=new XMLHttpRequest();}
catch (e)
  {
  try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
  catch (e)
    {
    try
      {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch (e)
      {alert("Your browser does not support AJAX!");return false;}
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
        alert(xmlHttp.responseText);
      document.getElementById("a"+x).value =  xmlHttp.responseText;
      document.getElementById("b"+x).value =  xmlHttp.responseText;
              }
    }
  xmlHttp.open("POST","def.php?sl_no="+s,true);
  xmlHttp.send(null);
  }
  </script>
    
<form>
<input   name="s" id="s<?php echo"1"?>" type="text"  value="1233"  size="5"  >
<input type="button" class="but" value="Create" name="new"  align="center" id="new" onclick="new_bun_no(<?php echo"1"?>);"  />     
 
<input name="a" id="a<?php echo"1"?>" type="text"  value=""  size="5"  >
<input   name="b" id="b<?php echo"1"?>" type="text"  value=""  size="5"  >
 </form>
  
Page:def.php
 
<?php
$c = $_REQUEST["sl_no"];
$a = $c+1;
$b =$c+2;
echo $a;
echo $b;
 
?>
 

Re: Multiple xmlHttp.responseText in Ajax

Posted: Wed Mar 11, 2009 1:26 pm
by requinix
You need to find a way to separate the two different outputs in your PHP. It could be JSON or something as simple as a comma.
Then the JavaScript does what it needs to: decodes the output or splits the text into two parts.