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

Fr. Simon Rundell SCP simon at rundell.org.uk
Wed Apr 30 08:47:51 CEST 2008


Dear Jochen et al

Thank you! I have success using xajax! I eventually hacked up the
xajax tutorial to perform a database query. It's funny that virtually
all the AJAX examples are all to do with css style modifications and
say very little about asych data querying which is really its
strongest possibility.

For the record and the benefit of others starting this road, here are
the core parts of the proof of concept before I try and turn it into
something useful  :-)  -

Plugin Main function:
================

function main($content,$conf)	{
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj=1;	

		require_once (t3lib_extMgm::extPath('xajax') . 'class.tx_xajax.php');
		$this->xajax = t3lib_div::makeInstance('tx_xajax');
		$this->xajax->decodeUTF8InputOn();
		$this->xajax->setCharEncoding('utf-8');
		$this->xajax->setWrapperPrefix($this->prefixId);
		$this->xajax->statusMessagesOn();
		// Turn only on during testing
		$this->xajax->debugOn();
		// Register the names of the PHP functions you want to be able to
call through xajax
		// $xajax->registerFunction(array('functionNameInJavascript',
&$object, 'methodName'));
		$this->xajax->registerFunction(array('processFormData', &$this,
'processFormData'));
		$this->xajax->processRequests();
		$GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] =
$this->xajax->getJavascript(t3lib_extMgm::siteRelPath('xajax'));

	
$content .= '<h2>Ajax Development Test Plugin</h2>';
$content .= '
<form name="submittest" id="submittest"
action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'"
method="post">
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<10"><br>
<input type="button" onclick="' . $this->prefixId .
'processFormData(xajax.getFormValues(\'submittest\'))" value="Query
MySQL" >
</form>

<div id="results">
Results Here
</div>
	
		return $this->pi_wrapInBaseClass($content);
	
} // end of main

// now the two key functions in PHP

function sGetFormResult($field, $table, $criteria)	{
// could be any kind of database query, here is a generic wrapper

	$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) {
		return 'NOT FOUND'; }
	else {
	$count=0;
		while ($row = mysql_fetch_array ($result)) {	
			$return_object[$count]=$row;
			$count++;
		} // while
	}

return $return_object;

} // function sGetFormResult()

function processFormData($data)	{

	$this->piVars = $data[$this->prefixId];
	$content = $this->sGetFormResult($data["field"], $data["table"],
$data["criteria"]);
	$objResponse = new tx_xajax_response();
	$objResponse->addAssign('results', 'innerHTML',
t3lib_div::view_array($content));
	return $objResponse->getXML();

} // function processFormData($data)


I hope this helps someone.

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

2008/4/29 Jochen Rieger <j.rieger at connecta.ag>:
> Hi Simon,
>
> I'd propose two alternatives:
>
> 1) use the xajax extension... it's fairly easy to use. You will have to
> set up a response object which can call several functions eg to update
> your HTML code or popup an alert message etc. (the functions will be
> found in the xajax response class).
>
>




More information about the TYPO3-dev mailing list