0

I describe my problem with following simple example.

function show(str)
{



if (window.XMLHttpRequest){
  xmlhttp=new XMLHttpRequest();
  }
else
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","data.php?q="+str,true);
xmlhttp.send();
}

here,i am getting some responsetext or result from data.php file through my request.

In my responsetext, i've used some input fields, buttons. How can i add functionality to that fields, which are fetching from external file(eg: data.php) through ajax request.

Can anyone plz help me?

4
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by Sorcher
0

To add anything, you may add something to the code written either in Javascript or in PHP. So, you have to take into account type of the changes you want to make. You can't just say I want to add something and add it anywhere.
Please clarify what you intend to do so that we might help you.

Edited by Pro2000: n/a

0

Here is a simple ajax search...

<?php
require "../db_connect.inc.php";
$query = "SELECT username FROM login";
$result = mysql_query($query);
$a=array();
while($row = mysql_fetch_array($result)){
    $a[]=$row['username'];
}
//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;

?>

the ajax / html

<FORM ACTION='' NAME='SearchFriend' method="POST">
<INPUT type="text" NAME="SearchFriend" onKeyUp="showHint(this.value)"/>
<input type="submit" name="searchFriend" value="Search"/>
</FORM>
<p>Suggestions: <span id="txtHint"></span></p>
<script type="text/javascript">
function showHint(str){
if (str.length==0){ 
  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","path/to-php-code.php?q="+str,true);
xmlhttp.send();
}
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.