[TYPO3-dev] Trying to get my head around AJAX

Fr. Simon Rundell SCP simon at rundell.org.uk
Tue Apr 29 16:51:59 CEST 2008


Hello all, perhaps you can advise me on whether I am heading in the
right direction...

I am trying to write an AJAX-type extension, which will run a query in
the background and populate data on a page without a refresh. All of
my old extensions are a constant cycle of POST/RE-POST and I am sure
that going AJAX will speed things up quite a bit.

As far as I can see, I need to execute a separate PHP script which
does the query and is accessed by an XMLHTTP Request. If I do this,
the script has no database configuration information, and so assumes
it is apache at localhost trying to get the data, which of course is
rejected...

Questions:

1) Can I package the query inside the pi1 file, and access it
asychronously somehow, or must I always execute a separate script

2) What do I have to include at the head of this script to enable it
to access the typo3 database via the internal username/password.

If I crack (2) then it my proof of concept code will work. Is this the
right track?

This is where I am so far:


a) The main function of my plugin
=========================

	function main($content,$conf)	{
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj=1;	
	
$content .= '<h2>Ajax Development Test Plugin</h2>';
$content .= '

<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById("results");
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var field = document.getElementById("field").value;
	var table = document.getElementById("table").value;
	var criteria = document.getElementById("criteria").value;
	var queryString = "?field=" + field + "&table=" + table +
"&criteria=" + criteria;
	ajaxRequest.open("GET",
"'.t3lib_extMgm::siteRelPath('spr_jquery').'executequery.php" +
queryString, true);
	ajaxRequest.send(null);
}

//-->
</script>


<form name="submittest">
Field:<input type="text" name="field" id="field" size="20" value="surname"><br>
Table:<input type="text" name="table" size="30" id="table"
value="tx_pddiocesedatabase_person"><br>
Criteria:<input type="text" name="criteria" size="50" id="criteria"
value="uid=120"><br>
<input type="button" onclick="ajaxFunction()" value="Query MySQL" >
</form>

<div id="results">
Results =
</div>


';
		
		return $this->pi_wrapInBaseClass($content);
	
	} // end of main

b) the script I am executing (executequery.php)
==================================

<?php

// need to include some stuff here to get it to talk to typo3_db

$field=$_GET["field"];
$table=$_GET["table"];
$criteria=$_GET["criteria"];

	$sql="SELECT $field FROM $table WHERE $criteria";
	$result = mysql_query($sql) or die("Basic Lookup: <i>$sql</i> failed
with error: ".mysql_error());
	# make sure that we recieved some data from our query
	$num_of_rows = mysql_num_rows ($result);
	if($num_of_rows==0) {
		echo 'NOT FOUND'; }
	else {
		$row = mysql_fetch_array ($result);
		echo $row[$field];
		}	
?>

I will worry about passing the data as XML once I actually have got
something passing.

Any ideas or pointers gratefully received.




Fr. Simon Rundell SCP
Parish of S. Thomas the Apostle, Elson
The Vicarage, 21 Elson Road, Gosport, Hants, UK, PO12 4BL
Tel: 02392 582824  Mob: 07976 802123
email: simon at rundell.org.uk
web:   http://www.saintthomaselson.org.uk
blog:   http://frsimon.wordpress.com

"I came that you may have life - life in all its fullness"
                           John 10:10




More information about the TYPO3-dev mailing list