Freitag, 3. August 2007

Ajax Project - Form Entry Suggest

(vgl. http://www.crackajax.net/suggest.php)


ajax.js


var url = "myajaxfunctions.php?param=";

function get_spot_id(idValue)
{

//var idValue = document.getElementById("agtel").value;
var myRandom = parseInt(Math.random()*99999999); // cache buster
http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}


function handleHttpResponse()
{
if (http.readyState == 4)
{
document.getElementById("hiddenDIV2").innerHTML=http.responseText;
document.getElementById("hiddenDIV2").style.width="200px";
//alert(document.getElementById("response_table_font").size);
var l = document.getElementById("response_table").rows.length;
document.getElementById("hiddenDIV2").style.height= l*25 +"px";
document.getElementById("hiddenDIV2").style.visibility="visible";
}
}

function loadrecord(record) {
//document.forms[1].telno.value = record;
document.getElementById("agtel").value = record;
document.getElementById("hiddenDIV2").innerHTML='';
document.getElementById("hiddenDIV2").style.height="0px";
document.getElementById("hiddenDIV2").style.width="0px";
document.getElementById("hiddenDIV2").style.visibility="hidden";
}

myajaxfunctions.php

if(strlen($_REQUEST['param'])>0)
{
/*$result = mysql_query("SELECT * FROM contact WHERE ContactTel LIKE '$_REQUEST['param']%'");
if(mysql_num_rows($result)>0)
{
while($myrow = mysql_fetch_array($result))
{
$agenttel = $myrow["ContactTel"];
$agentid = $myrow["ContactID"];
$textout .= "<table><tr style="border: 0px solid black"> <td style="border: 0px solid black; padding: 0px 10px 0px 10px"> <a href="javascript:loadrecord('".$agenttel."')">" .$agentid."</a></td> <td style="border: 0px solid black; padding: 0px 10px 0px 10px">" .$agenttel."</td></tr></table>";
}
}
else { $textout=""; }
*/

$textout .= "<table width='50px'>";
$textout .= "<tr style="border: 0px solid black"> <td style="border: 0px solid black; padding: 0px 10px 0px 10px"> <a href="javascript:loadrecord('112')">112</a></td> <td style="border: 0px solid black; padding: 0px 10px 0px 10px">Tom</td></tr>";
$textout .= "<tr style="border: 0px solid black"> <td style="border: 0px solid black; padding: 0px 10px 0px 10px"> <a href="javascript:loadrecord('100111')">100111</a></td> <td style="border: 0px solid black; padding: 0px 10px 0px 10px">Rainer</td></tr>";
$textout .= "</table>";

$textout='';
$textout .= "<font id='response_table_font' size='2'>";
$textout .= "<table id='response_table'>";
$textout .= "<tr > <td > <a href="javascript:loadrecord('112')">112</a></td> <td >Tom</td></tr>";
$textout .= "<tr > <td > <a href="javascript:loadrecord('112')">112</a></td> <td >Tom</td></tr>";
$textout .= "<tr > <td > <a href="javascript:loadrecord('112')">112</a></td> <td >Tom</td></tr>";
$textout .= "<tr > <td > <a href="javascript:loadrecord('112')">112</a></td> <td >Tom</td></tr>";
$textout .= "<tr > <td > <a href="javascript:loadrecord('112')">112</a></td> <td >Tom</td></tr>";
$textout .= "</table></font>";

}
echo $textout;


page.html


Wo?<br />
<input id="agtel" type="text" name="telno" onKeyUp="javascript:if(this.value.length > 3) get_spot_id(this.value);">

<div id="hiddenDIV2" name="hiddenDIV2" style="position:relative; top: 2px; left: 2px;width: 200px; visibility:hidden; background-color:white; border: 1px solid black;"> </div>



Editieren mit Browser - edit.php

<?php


$_REQUEST['dateiname'] =='' ? $file = "./index.php" : $file = $_REQUEST['dateiname'];


if(isset($_GET['action']) AND $_GET['action'] == 'update' AND $_POST['update_txt']!='') {
$fp = fopen($file, "w");
if(fwrite($fp, stripcslashes($_POST['update_txt']))) {
echo "Update erfolgreich.";
} else {
echo "Fehler beim Update.";
}
}
// else {
$content = file_get_contents($file);
echo '<form action="'.$_SERVER['PHP_SELF'].'?action=update&file='.$file.'" method="post" >';
echo 'Zu öffnende Datei: <input name="dateiname" type="text" size="50" maxlength="50" value="'.$file.'"><br>';
echo '<textarea name="update_txt" cols="70" rows="30" >'.$content.'</textarea><br/>';
if($file==''){ echo '<input type="submit" value="Datei laden" /></form>';}

else{ echo '<input type="submit" value="Update" /></form>';}
//}

function listdir($start_dir='.') {

$files = array();
if (is_dir($start_dir))
{
$fh = opendir($start_dir);
while (($file = readdir($fh)) !== false)
{
# loop through the files, skipping . and .., and recursing if necessary
if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
$start_dir == "." ? $filepath = $file : $filepath = $start_dir ."/". $file;
if ( is_dir($filepath) )
$files = array_merge($files, listdir($filepath));
else
array_push($files, $filepath);
}
closedir($fh);
}
else
{
# false if the function was called with an invalid non-directory argument
$files = false;
}

return $files;

}

echo "Vorhanden sind folgende Dateien:<br>";
$files = listdir();
foreach($files as $f)
echo " <a href='./edit.php?dateiname=".addslashes($f)."' >".$f."</a>,";
?>

Donnerstag, 2. August 2007

Ajax Tutorial - Kurzeinführung


Hier eine von mir angepasste Ajax Einführung von Rasmus.

I find a lot of this AJAX stuff a bit of a hype.  Lots of people have
been using similar things long before it became "AJAX". And it really
isn't as complicated as a lot of people make it out to be. Here is a
simple example from one of my apps. First the Javascript:

function createRequestObject() {
var req = null;
try{
req = new XMLHttpRequest();
}
catch (ms){
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (nonms){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed){
req = null;
}
}
}
return req;
}

var http = createRequestObject();

function sndReq(action) {
http.open('get', 'myajaxfunctions.php?ajaxaction='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;

var update = new Array();

if(response.indexOf('|' != -1)) {
update = response.split('|');

document.getElementById(update[0]).innerHTML = update[1];
}
}
}


This creates a request object along with a send request and handle
response function. So to actually use it, you could include this js in
your page. Then to make one of these backend requests you would tie it
to something. Like an onclick event or a straight href like this:

<div id="my_ajax_div">
</div>
<a href="javascript:sndReq('search_spots')">doit</a>

That means that when someone clicks on that link what actually happens
is that a backend request to myajaxfunctions.php?ajaxaction=search_spots will be sent.

In myajaxfunctions.php you might have something like this:

<?php
switch($_REQUEST['ajaxaction']) {
case 'search_spots':
echo "my_ajax_div|Search done.";
break;
}
?>

Now, look at handleResponse. It parses the "my_ajax_div|Search done." string and
splits it on the '|' and uses whatever is before the '|' as the dom
element id in your page and the part after as the new innerHTML of that
element. That means if you have a div tag like this in your page:

<div id="my_ajax_div">
</div>

Once you click on that link, that will dynamically be changed to:

<div id="my_ajax_div">
Search done.
</div>

That's all there is to it. Everything else is just building on top of
this. Replacing my simple response "id|text" syntax with a richer XML
format and makine the request much more complicated as well. Before you
blindly install large "AJAX" libraries, have a go at rolling your own
functionality so you know exactly how it works and you only make it as
complicated as you need. Often you don't need much more than what I
have shown here.

Expanding this approach a bit to send multiple parameters in the
request, for example, would be really simple. Something like:

function sndReqArg(action,arg) {
http.open('get', 'myajaxfunctions.php?ajaxaction='+action+'&arg='+arg);
http.onreadystatechange = handleResponse;
http.send(null);
}

And your handleResponse can easily be expanded to do much more
interesting things than just replacing the contents of a div.

-Rasmus