开发者

Modify the server side functions using jquery

开发者 https://www.devze.com 2022-12-28 19:10 出处:网络
I am developing one website using cakephp and jquery technologies. Server-side there are some functions which handles SQL queries.

I am developing one website using cakephp and jquery technologies.

Server-side there are some functions which handles SQL queries.

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 advance


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.


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.


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);
        }    

    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号