Topic: How to pass data object in xajax function in templete

Hi,

Can anybody help me out how to pass run time data object through xajax function in templete(.tpl file in Smarty).
And how to get this object in php file ?

I'm trying like this in smarty .tpl file :

xajax_testobj({$test[0]})

and in php file :

$obj = new xajaxResponse();
$obj->addAlert(print_r($_REQUEST,true));

I'm getting the result "object resource #9" in javascript alert box.

Thank you,
Vasu.

Re: How to pass data object in xajax function in templete

vasu,

You may need to quote the value you are going to send as the parameter for your xajax function...

xajax_testobj("{$test[0]}")

However, since your xajax function does seem to execute, that's probably not an issue in this case.

Now, try:

function testobj($sValue) {
    $obj = new xajaxResponse();
    $obj->addAlert($sValue);
    return $obj;
}

hth,

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: How to pass data object in xajax function in templete

No luck..I tried with double and single quotes also..same result.


Actually $test[0] is a record set or object of table.


php:

<?php

global $smarty;
$xajax = new xajax;
function testtest($data)
{
$obj = new xajaxResponse();
$obj->addAlert(print_r($data,true));
}

$xajax->registerfunction('testtest');
$xajax->processrequest();
$smarty->assign( 'xajax_javascript', $xajax->getJavascript() );


mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select * from mytable");
$i = 0;
while ($row = mysql_fetch_object($result)) {
    $test[$i++] = $row;
}

$smarty->assign_by_ref('test',$test);
$smarty->display('list.tpl');


?>




In list.tpl :

{section name="myloop" loop=$test}
   <a onclick="xajax_testtest('{$test[myloop]}')">{$test[myloop]->name}</a>
{/section}


Thanks,
Vasu.

Re: How to pass data object in xajax function in templete

Ahh, I see now.  You are trying to pass an array of data from PHP to javascript, then from javascript back to PHP.  You cannot (easily) use PHP to 'serialize' the array into a string as you have done.  Instead, you need to pass the array as a parameter to a javascript function, then the javascript function can do something useful with it (like assigning it a name on the browser side)... then your xajax function can get the local javascript copy of the array and send it back to the server.

Code: PHP

<?php



global $smarty;

$xajax = new xajax;

function testtest($data)

{

    $obj = new xajaxResponse();

    $obj->addAlert(print_r($data,true));

    return $obj;

}



function loadarray()

{

    mysql_connect("hostname", "user", "password");

    mysql_select_db("mydb");

    $result = mysql_query("select * from mytable");

    $i = 0;

    while ($row = mysql_fetch_object($result)) {

        $test[$i++] = $row;

    }



    $obj = new xajaxResponse;

    $obj->call('receiveArray', $test);

    return $obj;

}



$xajax->registerfunction('testtest');

$xajax->registerfunction('loadarray');

$xajax->processrequest();

$smarty->assign( 'xajax_javascript', $xajax->getJavascript() );



$smarty->display('list.tpl');

?>



In list.tpl :



{section name="myloop" loop=$test}

   <a onclick="xajax_testtest(myArray['{$myloop}'])">{$test[myloop]->name}</a>

{/section}



<script type='text/javascript'>

receiveArray = function(myArr) {

    myArray = myArr;

}

</script>

See, in the onload of the page, you can call xajax_loadarray(); The array is then sent to the browser from PHP -> js.  The array is then accessable to the js when xajax_testtest(myArray[x]); is called.  That portion of the array
will be sent to testtest (converted back to a php array)

Hope that helps a bit. smile

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: How to pass data object in xajax function in templete

Thanks for your help..our problem solved.

Vasu