[jQuery] Re: call a php function with onclick

2008-10-22 Thread Kevin Thorpe

stefano wrote:
 Hi, I would like to know how it is possibile to call a php function
 inside an onclick=function (), I try to explain me better
Hi Stefano. Your problem is that php is on the server and 
javascript/jQuery is in the web browser.
You need to get onclick to issue another page request to the server - 
that's AJAX. Look into
$.get for a solution.


[jQuery] Re: call a php function with onclick

2008-10-21 Thread RotinPain

What I did for having php executed at onClick event is using Ajax.
You must store your functions into an external php file.

Then use the jquery ajax methods to load, execute and receive back the
result of your php code without leaving the caller page.

Process will be invisible to users (the page won't reload or anything
like that).
You can pass variables or input values to your php page as you would
with a 'normal' form submission or link (POST/GET).

There are a lot of tut concerning ajax and jquery.
http://docs.jquery.com/Ajax for docs.

Here's a sample code i just wrote. I have not parsed the friend inputs
(to avoid XSS ...) but here's the way it works.

[HTML PAGE]
!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlns=http://www.w3.org/1999/xhtml;
head
meta http-equiv=Content-Type content=text/html;
charset=iso-8859-1 /
meta http-equiv=cache-control content=no-cache
meta http-equiv=pragma content=no-cache
meta http-equiv=expires content=mon, 22 jul 2002 11:12:01 gmt
titleadd / delete friends/title
script type=text/javascript src=scripts/jquery.js/script
script type=text/javascript
$(document).ready ( function() {

//add action to each input with type=button, action to execute is
based on the id of each one
$([EMAIL PROTECTED]'button']).each (
  function () {
$( this ).bind (
  click,
  function(){
$.ajax({
type: POST,
url: test-ajax-simple.php,
data: action=+$(this).attr(id)+friend=+$
(#friend).val(),
success: function(reponse){
alert(reponse);
  },//function success
error: function (){ alert('something wrong with ajax!') 
}
});//$.ajax
  }//function
);//bind
  }//function
);//each


});

/script
/head
body
  form id=form1 name=form1 method=post action=
onsubmit=return false
p id=buttons
  input type=text name=friend id=friend value=jQueryFriends
  input type=button name=add_friend value=add_friend
id=add_friend /
  input type=button name=delete_friend value=delete_friend
id=delete_friend/
/p
  /form
/body
/html


[PHP PAGE]
?php
header(Cache-Control: no-cache);
header(Pragma: nocache);

switch($_REQUEST['action']) {
case 'add_friend' :
add_friend($_REQUEST['friend']);
break;
case 'delete_friend' :
delete_friend($_REQUEST['friend']);
break;
default:
echo unknow action!\n;
}

function add_friend($fname) {
//sql here
echo Friend .$fname. added!\n;//output will be send to jquery
ajax object in response
return true;
}

function delete_friend($fname) {
//sql here
echo Friend .$fname. deleted!\n;
return true;
}
?



[jQuery] Re: call a php function with onclick

2008-10-21 Thread RotinPain

Here's a something based on links class name, the php code is in your
hand ;)

[HTML PAGE]
[JQUERY CODE]

//add an action to each A with class=friends,
//the action is based on the id of each A and passed as variable (see
DATA: )
$(a.friends).each (
  function () {
$( this ).bind (
  click,
  function(){
$.ajax({
type: POST,
url: test-ajax-simple.php,
data: action=+$(this).attr(id)+friend=+$
(#friend).val(),
success: function(reponse){
alert(reponse);
  },//function success
error: function (){ alert('something wrong with ajax!') 
}
});//$.ajax
  }//function
);//bind
  }//function
);//each


[ADD THIS BETWEEN BODY/BODY OF THE HTML PAGE]
p
  a href=#add class=friends id=add_friendAdd friend/a -
  a href=#del class=friends id=delete_friendDel friend/a
/p