Delphi For PHP Forums    

Go Back   Delphi-PHP Forums > Programming > Javascript and Ajax
Forum Jump Register FAQ Members List Downloads Search Today's Posts Mark Forums Read

Javascript and Ajax Questions and information about working with Ajax and Javascript in Delphi for PHP.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 13th November 2008, 22:50
405hp's Avatar
Firebug Fanatic
 
Join Date: Dec 2007
Location: State of Confusion
Posts: 2,556
405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute405hp has a reputation beyond repute
Default Favorite Functions to add to common.js

EDIT this thread http://sourceforge.net/mailarchive/f...php-developers is where I'm trying to get this included into the vcl. If you like the idea please jump in and help out.
================================================== ==============

Cut and paste this code block at the end of common.js (you will find it at vcl/js/common.js)
Code:
vcl=
{
$:function ()
{
var elements = new Array();
for (var i = 0; i < arguments.length; i++)
{
var element = arguments[i];
if (typeof element == 'string')
element = findObj(element);
if (arguments.length == 1)
return element;
elements.push(element);
}

return elements;
},
int:function(obj){ return parseInt(vcl.$(obj).value);},
real:function(obj){ return parseFloat(vcl.$(obj).value);},
escape:function(obj){ return escape(vcl.$(obj).value);},
unescape:function(obj){ return unescape(vcl.$(obj).value);},
focus:function(obj){ vcl.$(obj).focus();},
check:function(obj){ vcl.$(obj).checked=true;},
uncheck:function(obj){ vcl.$(obj).checked=false;},
hide:function(obj){ vcl.$(obj).style.display = 'none';},
show:function(obj){ vcl.$(obj).style.display = '';},
find:function(obj){ return vcl.$(obj);},
text:function(obj){ return vcl.$(obj).value;},
value:function(obj){ return vcl.$(obj).value;},
setOpacity:function(incoming,value)
{
theObj=vcl.$(incoming);
theObj.style.opacity = value/10;
theObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}
}

function createHTTPrequest()
{ /* Firefox, Opera 8.0+, Safari */
try {return new XMLHttpRequest();}
catch (error) {}
/* newer IE */
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
/* older IE */
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Your browser does not have AJAX support!");
}
function basicAjax (url,parameters,callback)
{
xmlHttp=createHTTPrequest();
if (callback) {xmlHttp.onreadystatechange=callback;}
else
{
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if (xmlHttp.status == 200)
{eval(xmlHttp.responseText);}
else
{alert("Sorry, received a server error ="+xmlHttp.statusText);}
}
}
}
xmlHttp.open("POST",url,true);
if (typeof(basicAjaxAsyncFalse) != 'undefined' && basicAjaxAsyncFalse==1) xmlHttp.open("POST",url,false); //this will force a wait for return
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59GMT");
xmlHttp.send(parameters);
}
We will add to this as more are found.

descriptions:

createHTTPrequest() the base function for basic ajax


function basicAjax (url,parameters,callback) a general all purpose ajax routine as explained in Basic Ajax for d4php

url = "ajax_update.php";
parameters = "code=12345&qty=2&user=me";
callback can be left blank to just run eval or if it exist it will be the function that will be called upon return.
sample files here http://forums.delphi-php.net/attachm...hp-ajax_v2.zip
Code:
 //call basicAjax but override the eval function 
basicAjax("ajax_server.php","value1=Bob",myfunction); 

//call basicAjax using the built in eval function 
var params='value1='+vcl.$('Edit1').value+'&value2='+vcl.$('Edit2').value;
basicAjax("ajax_server1.php",params);
vcl.$() replacement for document.getElementById() and findObj()

PHP Code:
// Sample Usage:
               function Button2JSClick($sender, $params)
               {

               ?>
alert(vcl.$('Edit1').value);//just get one object

//these 3 are identical
var obj1 = document.getElementById('Edit1');
var obj2 = vcl.$('Edit1');
var obj3= FindObj('edit1');

 vcl.$('Edit1').disabled;

//this gets an array of objects!! (includes obj we all ready had found also
// and real objects like LabeledEdit)
var elements = vcl.$('Edit2','Edit4','Edit6',obj1,obj2,'Edit5','Edit3');
for (var i=0;i < elements.length; i++) elements[i].value='';
//we just blew through a bunch of edits and cleared them.
// fyi this wouldn't work this way with the labeled edits though - they need setValue('')
               <?php

               
}
visibility switcher - shows and hides
Code:
// Sample Usage:
vcl.hide('Button1');
vcl.show('Button1');
And many more
vcl.focus('Edit7');
//fade out 5=50%
//fwiw I did this one with a for loop to slowly fade in something
vcl.setOpacity('image1',5);
//convert an edit into a number
var total=vcl.int('Edit7')*vcl.real('Edit6');
//put a check into a checkbox or radio button
vcl.checked('checkbox1');
myvalue=vcl.text('Edit7');

Last edited by 405hp; 6th December 2008 at 18:29. Reason: A new revision :)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +1. The time now is 09:07.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright © 2004 - 2009, G&J Solutions Ltd. All Rights Reserved. terms of use