Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

dhtmlxAjax in Smarty Template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
dmm2020
Smarty Rookie


Joined: 29 Nov 2009
Posts: 27

PostPosted: Mon Oct 08, 2012 5:58 pm    Post subject: dhtmlxAjax in Smarty Template Reply with quote

Below is an example of dhtmlajax that I am using in PHP/HTML templates
but want to convert it to use in my Smarty templates in the script I am developing. Question is, what do I have to do to get the below to work in a Smarty template?

Code:
    <script>
      function sendRequest(){
         var url = setURL();
         var loader = dhtmlxAjax.getSync(url);
         var response = loader.xmlDoc.responseText;
         document.getElementById("errorbox").innerHTML ="<div class='success'>Custom code successfully updated!</div>";
         if (response=='OK') {                 
            parent.dhxAccord.cells('a4').setText('Custom Code - UPDATED!');
            document.getElementById("errorbox").innerHTML ="<div class='success'>Custom code successfully updated!</div>";
         } else {
               parent.dhxAccord.cells('a4').setText('Custom Code FAILED!');
            document.getElementById("errorbox").innerHTML ="<div class='error'>"+response+"</div>";
         }
      }
      
      function setURL(){
         var seturl = "process/process.ads-banner.php?"+encodeURI(getFormValues());
         return seturl;
      }   
      
      function getFormValues(){
         var strparams;
         var strlinkurl = document.getElementById("linkurl").value;
         var strimgpath = document.getElementById("imgpath").value;
         var stralttag = document.getElementById("alttag").value;
         var a = document.getElementById("newwindow").checked;
         if (a==true){
            var strnewwindow = 1;
         }else{
            var strnewwindow = 0;
         }
          if(strnewwindow<1){strnewwindow=0;}
         var strimgupload = document.getElementById("imgupload").value;
         var strcodebefore = escape(document.getElementById("codebefore").value);
 
         var strcodeafter = escape(document.getElementById("codeafter").value);
         //strparams = 'code='+strcode+'&codebefore='+strcodebefore+'&codeafter='+strcodeafter+'&adkey='+<?=$adkey;?>;
         strparams='linkurl='+strlinkurl+'&imgpath='+strimgpath+'&alttag='+stralttag+'&newwindow='+strnewwindow+'&imgupload='+strimgupload+'&codebefore='+strcodebefore+'&codeafter='+strcodeafter+'&adkey='+<?=$adkey;?>;
         return strparams;
      }
      
</script>
Back to top
View user's profile Send private message
dmm2020
Smarty Rookie


Joined: 29 Nov 2009
Posts: 27

PostPosted: Sat Oct 13, 2012 4:01 pm    Post subject: Reply with quote

No response? I read in similiar posts that ajax is supposed to work in Smarty as it does in PHP but it does not.

Seems some posts get selectively ignored, this one well over 72 hours, but that's beside the point. What we really could use is WORKING examples of using Ajax with Smarty templates.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Oct 13, 2012 9:37 pm    Post subject: Reply with quote

Here is an article how Smarty and Ajax can work together http://www.ibm.com/developerworks/opensource/library/wa-aj-smarty/index.html

Or google for "Smarty ajax" you will find lots of articles and examples

But I myself have no experiance with it
Back to top
View user's profile Send private message
dmm2020
Smarty Rookie


Joined: 29 Nov 2009
Posts: 27

PostPosted: Sun Oct 14, 2012 6:13 am    Post subject: Reply with quote

Actually, none of the examples I saw with the SE query you suggested panned out. I use 5.3 PHP on top of that and many used obsolete PHP on top of that. I figured it out through trial and error. Below is my working example:
Code:

   <script type="text/javascript">
   {literal}
 
      function sendRequest(){
         var url = setURL();
         var loader = dhtmlxAjax.getSync(url);
         var response = loader.xmlDoc.responseText;
         
         if (response=='OK') {                 
            
             alert('Test OK');
         } else {
               alert(response);
         }
      }
      
 
      function setURL(){
         var seturl = "/templates/ajaxfiles/forgotpassword.ajax.php?"+encodeURI(getFormValues());
         return seturl;
      }   
      
      function getFormValues(){
         var strparams;
         var strusername = document.getElementById("username").value;
         var stremail = document.getElementById("email").value;
         strparams='username='+strusername+'&email='+stremail;
         return strparams;
      }   
      {/literal}
   </script>


The URL in the script above has to be set relative to the application root. If you are not getting a response at all, meaning you click the submit button and nothing happens, try just echoing any string in the ajax processing file. If that don't work, check your URL path. It's then the culprit.

ANY errors above will stop the javascript from loading. Make sure the parameter string is correct and each of the elements are pointing to a valid form element. If the script can not find the form element, the script dies. If this is suspect, build your URL parameters and string one at a time.

You can edit the success or failure response to anything you want.


Last edited by dmm2020 on Sun Oct 14, 2012 6:34 am; edited 2 times in total
Back to top
View user's profile Send private message
dmm2020
Smarty Rookie


Joined: 29 Nov 2009
Posts: 27

PostPosted: Sun Oct 14, 2012 6:23 am    Post subject: Reply with quote

Below is the form example:
Code:

<form method="post">
               
               {$lang_forgotpassword.instructions}
                    <input type="hidden" name="{$hidden_field}" value="eVe941Ku4bqC49a" />
               <label for="username">{$lang_common.username}</label><input type="text" name="username" id="username" /><br />
               <label for="email">{$lang_common.email}</label><input type="text" name="email" id="email" /><br />
               <button class='floatright' type='button' id='submit' onclick='sendRequest();'>{$lang_buttons.button_submit}</button>
               </form> 
               <p style="text-align: left;margin: 10px 15px;">{$lang_forgotpassword.disclaimer}</p>


In the header you need to load dhtmlxcommon.js such as I do below
Code:

   <script type="text/javascript" src="/includes/modules/dhtmlxAjax/codebase/dhtmlxcommon.js"> </script>


You can find dhtmlxajax for free at http://dhtmlx.com. Click on Products, then towards the bottom of the tabbed menu on the page, find the link for Ajax and then click it. My example in the ajax example above this post is for a GET method of return. It's possible to do POST and json types, even using XML but I found my method easiest.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP