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;

 

?>

 

Link to comment
Share on other sites

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;

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.