Jump to content

fill multiple textboxes from ajax selection


inamul70

Recommended Posts

I am having two tables. Order and grademaster.. In grademaster table i have two fields 'packing' and 'condition'.. I am using php mysql and ajax. If a person select grade value of packing and condition should be autopopulate in the textboxes.. I have successfully loaded value of one textbox through php ajax but the second text box cannot accept values. I have tried calling two functions from onchange() but no result.. Please suggest me how can i populate two text boxes from one ajax selection. my code below

 

//javascript

 

<script type="text/javascript">

function showUser(str)

{

if (str=="")

  {

  document.getElementById("txtHint").innerHTML="";

  return;

  }

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET","getgradepacking.php?q="+str,true);

xmlhttp.send();

}

</script>

 

 

 

 

        <div>

               

                <label>Packing <strong>*</strong></label>

                <textarea id="txtHint" name="txtpacking" id="txtpacking" cols="42" rows="4"></textarea>

               

            </div>

           

 

 

 

//getgradepacking.php

 

<?php

$q=$_GET["q"];

 

include('includes/connect.php');

               

mysql_select_db($Db, $link);

 

$sql="SELECT * FROM grademaster WHERE gradeid = '".$q."'";

 

$result = mysql_query($sql);

 

$row = mysql_fetch_array($result);

 

$packing = $row['packing'];

 

echo $packing;

 

?>

 

In your php file you have to combine the two columns into a single echo. We're going to do this with a implode command. I'm using '/' to separate the two pieces of information. You can use anything you want as long as it is not used in either of the two resources.

PHP:

...
$row = mysql_fetch_array($result);
$combo = implode('/', $row);
echo $combo;

 

In the javascript section we have to separate them. Where we once had:

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

 

We put:

pieces = xmlhttp.responseText;
number1 = pieces.search('/');
first = pieces.substr(0,number1);
second = pieces.substring(number1+1);

document.getElementById("txtHint").innerHTML= first;

document.getElementById("nextDiv").innerHTML= second;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.