0

I need to fill a text-box when another text-box field is having a value. in JavaScript i face a problem ,it wont work perfectly.

First, let's see the html code for the form itself:

<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
    <label>Product Segment</label><label style="color: #F00;">*</label>
</div>
<div class="ui-widget"
    style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
    <input type="text" name="productsegment" id="productsegment" onkeyup="getagentids();" value="<?php echo $productsegment;?>" />
</div>
<!--Row3 end-->

<!--Row3 -->
<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
    <label>Product Group</label><label style="color: #F00;">*</label>
</div>
<div
    style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
    <input type="text" name="productgroup" id="productgroup" value="<?php echo $productgroup;?>" />
</div>

Jscript Code:

<script type="text/javascript"> 
var url = "productgroupautofill.php?param=";

function GetHttpObject() {
    if (window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}
function getagentids() {
    httpobject = GetHttpObject();

    if (httpobject != null) {
        var idValue = document.getElementById("productsegment").value;
        var myRandom = parseInt(Math.random() * 99999999);
        // cache buster

        http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true); // from
        // here
        // it
        // wont
        // work
        http.onreadystatechange = handleHttpResponse;
        http.send(null);

    }
}
function handleHttpResponse() {
    if (http.readyState == 4) {
        results = http.responseText;
        alert(results);
        document.getElementById('productgroup').value = results;
    }
}            
</script>

productgroupautofill.php file content .

<?php
require_once 'Mysql.php';
$dblink = new Mysql();
$dblink->__construct();

if(strlen($param)>0)
{
    $result = mysql_query("select productgroup from productsegmentmaster where productgroup LIKE '$param%'");
    if(mysql_num_rows($result)==1)
    {
        while($myrow = mysql_fetch_array($result))
        {
            $agentname = $myrow["productgroup"];
            $textout .= $agentname;
        }
    }
    else
    {
        $textout=" , , ,".$param;
    }
}
echo $textout;
6
  • Define won't work perfectly. Commented Nov 29, 2012 at 9:41
  • it is going into the function till the http.open state...then it s not working...:( Commented Nov 29, 2012 at 9:44
  • ... and the error message is? Commented Nov 29, 2012 at 9:45
  • no error message..it doent work further...:( Commented Nov 29, 2012 at 9:47
  • i think the http.open connection does not establish... Commented Nov 29, 2012 at 9:49

4 Answers 4

1
var url = "productgroupautofill.php?param=";
var httpobject;
function GetHttpObject() {
    if (window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}
function getagentids() {
    httpobject = GetHttpObject();

    if (httpobject != null) {
        var idValue = document.getElementById("productsegment").value;
        var myRandom = parseInt(Math.random() * 99999999);
        // cache buster

        httpobject.open("GET", url + escape(idValue) + "&rand=" + myRandom, true); // from
        // here
        // it
        // wont
        // work
        httpobject.onreadystatechange = handleHttpResponse;
        httpobject.send(null);

    }
}
function handleHttpResponse() {
    if (httpobject.readyState == 4) {
        results = httpobject.responseText;
        alert(results);
        document.getElementById('productgroup').value = results;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...for the reply...but its working perfectly...few mistakes i made..:P...and yours is the correct one..:)
0

please try with following code in productgroupautofill.php

require_once 'Mysql.php';
$dblink = new Mysql();
$dblink->__construct();
$param=$_GET['param'];
if(strlen($param)>0)
{ 
$result = mysql_query("select productgroup from productsegmentmaster where productgroup LIKE '$param%'"); 
if(mysql_num_rows($result)==1) 
{
 while($myrow = mysql_fetch_array($result))
 {
  $agentname = $myrow["productgroup"]; 

  $textout .= $agentname; 
  } 
  } 
  else 
  { 
  $textout=" , , ,".$param; 
  } 
  } 
  echo $textout;

Comments

0

You define httpobject as:

httpobject = GetHttpObject();

but then you try to use http:

http.open( ... )

Change your definition to this, because you use http throughout the code:

http = GetHttpObject();

You also might want to declare it in the global scope:

var url = "productgroupautofill.php?param=";
var http;

3 Comments

<body><h1>Object not found!</h1><p> The requested URL was not found on this server. The link on the <a href="output.php">referring page</a> seems to be wrong or outdated. Please inform the author of <a href="localhost:81/producttypemaster.php">that page</a> about the error. </p><p>If you think this is a server error, please contactthe <a href="mailto:postmaster@localhost">webmaster</a>.</p><h2>Error 404</h2><address> <a href="/">localhost</a><br /> <span>Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7</span></address></body></html>
My query is not executing...properly...:(
Does the PHP work alone? Try calling it from a browser, not Javascript. When the PHP is working, move on to debug the ajax.
0

You cannot use your tectout variable outside of the loop. Try using echo inside your loop and you should see some results coming through, In fact you don't need that variable at all. Just echo $myrow["productgroup"]; if condition is true and assign ' , , , '$param to anothher variable and echo if false

1 Comment

is productgroupautofill.php in the same directory that the file calling it is in? also you will need to split the array after it is sent back. If there is more than one you can tell javascript that the values are split with ' , ' and it will put the values into the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.