6
Contributors
5
Replies
33
Views
7 Years
Discussion Span
Last Post by canterorist
2

PHP AJAX is a brilliant tool
What were you intending to use it for?
Here is the base code:
javascript for head

<script type="text/javascript">
function showData(str)
{
if (str.length==0)
  { 
  document.getElementById("data").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("data").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","file.php?file="+str,true);
xmlhttp.send();
}
</script>

and put this where you want the data to appear

<div id="data"></div>

And your text field:

<input type="text" onkeyup="showData(this.value)" size="15" />

this will work like googles smart search
--i think they call is SOAP
By getting the page 'file.php' setting variable q to the text search.

file.php

<?PHP
include (connection.php');
if($q){
mysql_select_db($database_connection, $connection);
$maxquery = "SELECT max(id) FROM table";
$max = mysql_query($maxquery, $connection) or die(mysql_error());

for($i=0;$i<=$max;$i++){
mysql_select_db($database_connection, $connection);
$query_Recordset1 = "SELECT DISTINCT FROM table WHERE column LIKE '$q' AND id='$i'";
$Recordset1 = mysql_query($query_Recordset1, $connection) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if($i){
    if(!$results){
    $results = $row_Recordset1['column'];
    }
    else{
    $results .= ', '.$row_Recordset1['column'];
    }
}
}
}
echo $results;
?>

You obviously will need to reconstruct this
You can also use This to reload parts of a page by swapping the div with input from another page

Is this What you wanted to know?
Hope it helped :)

0

You'll have to be a little more specific, what exactly are looking to do with AJAX?

I suggest looking at JQuery, it's a popular javascript library that makes AJAX very simple.

(For instance, it handles all the browser compatibility code itself)

0

Hi, Agree 100% with Danny, use JQuery it would enhance development as well as ease of use.
Here is the very basic code to understand you may trick it for your requirements.

FORM.HTML

<html>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn1").click(function() {
        $.ajax({
            type: "POST",
            url: "backend.php",
            data: "name="+$("#txt1").val(),
            success: function(msg){alert(msg);}                 
                });
        })
}); 
</script>
<input type="text" id="txt1"/>
<input type="button" id="btn1" value="Call Ajax"/>
</html>

BACKEND.PHP

<?php
    echo $_POST["name"];
?>

Be sure to download the jquery.js file in the folder. you may get it from jquery.com

Edited by Reverend Jim: Fixed formatting

0

hi! i agree with danny and guarang.I think jquery is popular today. you only need a file like jquery-1.4.2.min.js, it's free.

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.