Topic: example: smarty & xajax

Hi all!


Some users asked for an example on how to use smarty with xajax and so I made a short one smile

This example shows how to display a small login screen including validation error-messages and how to display new contents based on smarty templates afterwards.

Basically, I always use 3 main php files called:

- index.php (displays the inital template including html header/footer and javascripts)
- index.common.php (including smarty+xajax and configs)
- index.server.php (xajax response functions)


All you need is xajax 0.5b2 and smarty installed

index.php
[code=PHP]<?
    include("index.common.php");
       // assign the xajax javascript function to smarty
    $smarty->assign("xajax_javascript",$xajax->getJavascript('/xajax/'));
    $smarty->display("index.tpl");[/code]
index.common.php

[code=PHP]<?
require_once ("xajax/xajax_core/xajax.inc.php");

require_once("smarty/Smarty.class.php");

session_start();


$smarty = new Smarty;


// put in your server configuration here
$smarty->cache="/srv/www/htdocs/smarty/cache";
$smarty->compile_dir="/srv/www/htdocs/smarty/compile";
$smarty->template_dir="/srv/www/htdocs/smarty/templates";

// turn caching off for dynamic pages and/or development
$smarty->force_compile=1;
$smarty->caching=0;

$xajax = new xajax("index.server.php");
$xajax->registerFunction("initLayout");
$xajax->registerFunction("login");[/code]
index.server.php
[code=PHP]<?

include("index.common.php");

function login($aFormValues) {
    // new xajaxResponse Object
    $objResponse = new xajaxResponse();
   
    //clear error fields
    $objResponse->assign("error_userpass","innerHTML","

Re: example: smarty & xajax

thx for that one!

i´m not using smarty & xajax yet, but now i´m not afraid of it anymore ;-)

Re: example: smarty & xajax

Thank you very much! I will give it a second try. Hope it is a smooth process now with your "tutorial".

You can also add this tutorial to wiki.

Roman

Re: example: smarty & xajax

Hello
       Thanx for the above code.Its really helpfull.I started wth this ..Its working fine..
But on giving inputs i am getting alert with following message
Error:javascript file cold not be included. Perhaps the URL may be incorrect?
URL is /xajax/xajax_js/xajax.js

Actually I geve like this
$smarty->assign("xajax_javascript",$xajax->getJavascript('/xajax/'));
But when I  rewrite the code
$smarty->assign("xajax_javascript",$xajax->getJavascript('xajax/'));
like this no output is coming.......
Could you please help me to find the error........

Re: example: smarty & xajax

Do you have 0.5b2 installed?

$xajax->getJavascript('/xajax/')) <- the path has to be relative to the php script or absolute to the document root. When you have xajax installed in http://foo.bar/xajax/, the parameter would be /xajax/.

Re: example: smarty & xajax

Hello Thank You very much......
But when I run the code that you explained above....i got the following alert
Error:XML response that was returned by the server is invalid.
Received:
Shall I make any changes.....
I fetch l a template file from index.php and do these much codes..
$xajax->registerFunction("login");
$smarty->assign("xajax_javascript",$xajax->getJavascript('xajax'));
$xajax->processRequests();


in another file named lib.php i wrote the login function (this ifile is specified while i created the instance  $xajax=new xajax('libs/lib.php)
function login($aFormValues) {
    // new xajaxResponse Object
    $objResponse = new xajaxResponse();
   
    //clear error fields
    $objResponse->assign("error_userpass","innerHTML","

Re: example: smarty & xajax

belsonpkurian wrote:

But when I run the code that you explained above....i got the following alert
Error:XML response that was returned by the server is invalid.
in another file named lib.php i wrote the login function (this ifile is specified while i created the instance  $xajax=new xajax('libs/lib.php)
function login($aFormValues) {
...
}

Hey some gurus here quote me if I am wrong.

Your problem seems to be like the very same like I did.

Remember that you open your lib.php using xajax remote call.

So it's just like you did

<?php
include("lib.php");
return(login($formvalues));
?>

And ......... where do you create xajax object? Where do you connect to the database etc.????
You have to do this all in your lib.php too to make it work ;-)

Roman

Re: example: smarty & xajax

belsonpkurian, what version of xajax are you using? it looks like you're mixing and matching the v0.2 and v0.5 syntax

9 (edited by belsonpkurian 2007-05-19 2:33:56 PM)

Re: example: smarty & xajax

Hello I encountered a problem while using xajax
I created a login page and on the onclick event
<td colspan="2" align="center"><input type="button"  value="submit" onclick="submitlogin()"></td>
i wrote a  function ......like this .. in the header part of the script..

<head>
  <title>{$title}</title>
  <meta name="GENERATOR" content="Quanta Plus">
  <meta http-equiv="Content-Type" content="text/html; charset=">
  {$xajax_javascript}
  {literal}
    <script type="text/javascript">
        function submitlogin() {
             nam=document.getElementById("name").value;
             pass=document.getElementById("password").value;
             alert("name="+nam + "," +"pass="+pass);
            xajax_Login(nam,pass);
            return false;
        }
    </script>

{/literal}

</head>

and on the common.lib.php

$xajax=new xajax();
$xajax->registerFunction("Login");

function Login($user,$pass) {
$objResponse = new xajaxResponse();
$objResponse->alert($user);
$objResponse->alert($pass);
return $objResponse;
}
$xajax->processRequest();


I get everything and it  is working fine............i get  all the inputs in the Login function through alert() function


but the problem is...... i get the following when the page is loaded.

object(xajaxPluginManager)#2 (5) { ["aPluginFolders"]=>  array(1) { ["/techhome/belson/Projects/web/CharFace/xajax/xajax_core/plugin_layer"]=>  string(68) "/techhome/belson/Projects/web/CharFace/xajax/xajax_core/plugin_layer" } ["aResponsePlugins"]=>  NULL ["objRequestProcessorPlugin"]=>  &object(xajaxDefaultRequestProcessorPlugin)#3 (3) { ["aObjArray"]=>  NULL ["iPos"]=>  NULL ["_objXajax"]=>  object(xajax)#1 (20) { ["aFunctions"]=>  array(1) { ["Login"]=>  array(1) { ["callback"]=>  string(5) "Login" } } ["aCallableObjects"]=>  array(0) { } ["aEventCallbacks"]=>  NULL ["sRequestURI"]=>  string(34) "https://192.168.1.230/belson/TEST/" ["sWrapperPrefix"]=>  string(6) "xajax_" ["bDebug"]=>  bool(false) ["bUseUncompressedScripts"]=>  bool(false) ["bStatusMessages"]=>  bool(false) ["bExitAllowed"]=>  bool(true) ["bWaitCursor"]=>  bool(true) ["bErrorHandler"]=>  bool(false) ["sLogFile"]=>  string(0) "" ["bCleanBuffer"]=>  bool(false) ["sEncoding"]=>  string(5) "utf-8" ["bDecodeUTF8Input"]=>  bool(false) ["bOutputEntities"]=>  bool(false) ["iTimeout"]=>  int(6000) ["bAllowBlankResponse"]=>  bool(false) ["bAllowAllResponseTypes"]=>  bool(false) ["aEvents"]=>  array(4) { ["beforeProcessing"]=>  array(0) { } ["afterProcessing"]=>  array(0) { } ["onMissingFunction"]=>  array(0) { } ["onProcessingError"]=>  array(0) { } } } } ["objHeaderScriptPlugin"]=>  NULL ["objIncludePlugin"]=>  object(xajaxDefaultIncludePlugin)#4 (2) { ["_objXajax"]=>  NULL ["_aFunctions"]=>  NULL } }
 
Also when I check the error console it  is seen that

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/techhome

/belson/Projects/web/CharFace/libs/common.lib.php</b> on line <b>125</b><br />

<?xml version="1.0" encoding="utf-8" ?><xjx><cmd n="al">Germany</cmd><cmd n="al"></cmd></xjx>

but i declares everything in congfig file....I get the values also....since i Just printed those values there.....

Could u plz suggest me a solution.....

Re: example: smarty & xajax

belsonpkurian wrote:

i get the following when the page is loaded.

Is that the contents of the  {$xajax_javascript} smarty variable?

Re: example: smarty & xajax

can you make a sample code using smarttemplate + xajax, im having problems inserting the printjavascript inside the html head tag.

thanks

Re: example: smarty & xajax

Look at the first post... it shows everything you need for getting started smile

Re: example: smarty & xajax

Hello,

I've been searching the forum and haven't found an answer to this one yet... here's my setup:

xajax 0.5-b4
php5
apache2

site is in a virtual host, document root is defined for the vhost as ~/project_folder/html

here's how the project is laid out...

~/project_folder
/html
index.php

/include
index.common.php
index.server.php

/include/xajax
<xajax files>

/templates
index.tpl
layout.tpl
login.tpl

/templates_c
<smarty compiled templates>
/cache
<smarty cache files>

The code in the above files is pretty much the same as in the example except for the paths.

Here's the code:

index.php
[code]<?php
include("index.common.php");

// assign the xajax javascript function to smarty

// $smarty->assign("xajax_javascript",$xajax->getJavascript('/xajax/'));
// $smarty->display("index.tpl");

$smarty->assign("xajax_javascript", $xajax->getJavascript('../include/xajax/'));
$smarty->display("../templates/index.tpl");
?>[/code]
index.common.php
[code]<?php
require_once ("xajax/xajax_core/xajax.inc.php");
require_once("Smarty/Smarty.class.php");
session_start();
$smarty = new Smarty;

// put in your server configuration here
$smarty->cache="../cache";
$smarty->compile_dir="../templates_c";
$smarty->template_dir="../templates";

// turn caching off for dynamic pages and/or development
$smarty->force_compile=1;
$smarty->caching=0;


// $xajax = new xajax("index.server.php");
// $xajax->registerFunction("initLayout");
// $xajax->registerFunction("login");

$xajax = new xajax("../include/index.server.php");
$xajax->registerFunction("initLayout");
$xajax->registerFunction("login");

//    Tried this way too... same results.

// $xajax = new xajax();
// $xajax->configure('requestURI','../include/index.server.php');
// $xajax->register(XAJAX_FUNCTION, new xajaxUserFunction('initLayout','index.server.php'));
// $xajax->register(XAJAX_FUNCTION, new xajaxUserFunction('login','index.server.php'));
?>[/code]
index.server.php[code]<?php

// include("index.common.php");
include("../include/index.common.php");

function login($aFormValues) {
    // new xajaxResponse Object
    $objResponse = new xajaxResponse();
    //clear error fields
    $objResponse->assign("error_userpass","innerHTML","

14 (edited by dannic 2007-10-25 11:15:32 PM)

Re: example: smarty & xajax

putting this in your index.common.php file should work.

require_once ("includes/xajax/xajax_core/xajax.inc.php");

15 (edited by Arcanum 2007-11-30 7:33:50 PM)

Re: example: smarty & xajax

Hi all!

Sorry for my bad english, i'm from russia smile)

I have a two question for your:

1. Question One:
   Why error?
   
   $smarty->assign("xajax_javascript",$xajax->getJavascript(BASE_DIR.'class/smarty/xajax/'));  <---- Error.Javascript component could not be included
   $smarty->assign("xajax_javascript",$xajax->getJavascript('http://localhost/class/smarty/xajax/'));  <---- OK!! It's work!
   
   // show BASE_DIR
   echo BASE_DIR; // Result: x:/home/localhost/www/ (I'm use local server on windows platform)

   Why is error? Path to folder is absolutly correct...

2. Question two:
   
   test.php
   ----------------------------------
   $xajax = new xajax();
   $xajax->registerFunction("test");
   
   function test() {
     ...
     code
     ...
   }

  test.tpl
   ----------------------------------
   <a href="#" onClick="xajax_test();">Link</a>   All OK!! It's Work!

  // xajax_test() - call php FUNCTION!!! How i can call METHOD of CLASS??? show EXAMPLE



  test.php
  ---------------------------------
 
  include_once('class.users.php');
 
  $user = new Users();

  $user->show_all();


  $xajax = new xajax();
  $xajax->????????????  <---- HOW REGISTER METHOD show_all()??

  test.tpl
  ---------------------------------
  <a href="#" onClick="xajax_????????;">Link</a>  // <---- HOW WRITE CODE FOR CALL METHOD show_all()???

Re: example: smarty & xajax

plz help me
http://community.xajaxproject.org/viewtopic.php?id=6427