views:

55

answers:

3

Hi, I am developing one website using cakephp and jquery technologies. Server-side there are some functions which handles sql queris.

As per requirement I want to modify server side functions on client side using jquery AJAX call.

E.g. : Below is the function on server side to modify users information.

function modifyUser(username,userid) {
  //update query statements
}

Then jquery AJAX call will be like this :

$.ajax({
  url: 'users/modiyUser',
  success: function() {
    alert("Updation done") or any statements.
  }
});

and I want to modify above i.e. server side function depending upon client input criteria.

$.ajax({
 function users/modiyUser(username,userid) {
    // I will write here any other statements which gives me some other output.
 }
});

Above AJAX call syntax may not present, but i think you all understood what I am trying to do I simply wants to modify/override server side functions on client side.

Please let me know is there any way to resolve above mentioned requirement.

Thanks in adavance

A: 

You cannot call a PHP functions from the client directly. You can only make an HTTP request to a URI.

The URI determines the PHP script run. Input can be taken via $_GET, $_POST, and $_COOKIE (among others, but those are the main ones).

You can either write separate scripts for each function or determine what to do based on the user input.

David Dorward
Thanks folks for your quick reply@Darin : Yes you are right here my requirement is bit strange.@David : Yes we can use get,post and etc.I know this is strange requirement, Is there a way to dynamically generate functions which will give me server database access so that i can directly write separate functions.As you mentioned server side function can not modify from client side, But can i write separate function which give me some result.
ant
If you put enough effort into it, it is possible to upload PHP-code to the server and execute it. This is a huge security risk. Please don't do it. Just keep your server-side code on the server only. Just like everybody else does.
edwin
A: 

You could have a server-side function in a separate PHP file to do this, and make an AJAX call call into that function first to perform the modification. But client-side changes to server-side code are just not possible.

I can't actually imagine why you would want to do this, though.

James Wiseman
A: 

why override a function???

can i suggest this?

in PHP

try {
  // functions here....
  function modifyUser($username,$userid) {
     //update query statements
     if(!is_string($username)) throw new Exception("argument to " . __METHOD__ . " must be a string");
     if(!is_string($userid)) throw new Exception("argument to " . __METHOD__ . " must be a string");
     // do some modification codes....
  }
  function delete($userid){
      // do stuff blah blahh...
  }

  // $_POST or $_GET etc. here
  if(isset($_GET["modify"])){ // I make use of get for simplicity sake...
     $username = $_GET['username'];
     $userid = $_GET['userid'];
     modifyUser($username,$userid);
     $ret = array();
     $ret["error"] = false;
     $ret["msg"] = "$username has been modified";
     echo json_encode($ret);
  } else if(isset($_GET["delete"])) {
     $userid = $_GET['userid'];
     delete($userid);
     $ret = array();
     $ret["error"] = false;
     $ret["msg"] = "$username has been deleted";
     echo json_encode($ret);
  }else {
 // the client asked for something we don't support
 throw new Exception("not supported operation");
  }

}
catch(Exception $e){
    // something bad happened
    $ret = array();
    $ret["error"] = true;
    $ret["msg"] = $e->getMessage();
    echo json_encode($ret);
}

in jQuery ajax

$.ajax({
    url: 'ajax.php',
    data : { modify : true, // sample for modify... can also be delete : true,
        username : $('#username').val(),
        userid : $('#userid').val() },
    type: 'GET',
    dataType: 'json',
    timeout: 1000,
    error: function(){
        alert('error in connection');
    },
    success: function(data){
        if(data.error)
            alert('Something went wrong: ' + data.msg);
        else {
            alert('Success: ' + data.msg);
        }    

    }
});
Reigel