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">
<title>add / 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;
}
?>

Reply via email to