Javascript 在AJAX中隐藏和显示

Javascript 在AJAX中隐藏和显示,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个名为login的Ajax函数 function Login(){ $.ajax({ url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/'+$("#loginusername").val(), headers: {authorization:window.btoa($("#loginusername").val()+':'+$("#loginpassword

我有一个名为login的Ajax函数

function Login(){
$.ajax({
    url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/'+$("#loginusername").val(),
headers:  {authorization:window.btoa($("#loginusername").val()+':'+$("#loginpassword").val())},
contentType: "text",
    dataType: 'json',
    success: function(data) {
        authorisation = window.btoa($("#loginusername").val()+':'+$("#loginpassword").val()); //create a variable to pass to the cookie
        createCookie('cookie',authorisation,0); //create our authorisation cookie for the client
        alert("Login Successful!");
        $("#loginbox").hide(); //hide the login button
        $("#logout").show(); //show the logout button
        $("#addreview").show(); //show the add a review button
        $("#adminpanel").show();//show the admin panel page
         $("#editreview").show();
           $("#deletereview").show();
        $("#loginusername").val(''); //clear the name box
        $.mobile.changePage("#home" ); //show the menu

    },
error: function (response) {
    var r = jQuery.parseJSON(response.responseText);

    alert("Error:" + r.error.text);
               }
        });
}
此时,它使用删除查看按钮,但在html中,由于用户未登录,它会将其隐藏, 我想让它做的是,当一个特定用户登录时,只在他发布的评论上显示删除按钮,而不在其他用户上显示删除按钮。我如何在Ajax中做到这一点 PHP代码web服务:

function deletereview_delete($id=null){{

    // authorization required by the user to post a review
        $headers = apache_request_headers();
        {   
            if (empty($headers['authorization'])) {
              $info->status = 'failure';
               $info->error->code = 15;
               $info->error->text = 'Authorization required';
               $this->response($info, 401);
         }
       }


        $string = base64_decode($headers['authorization']);
        list($username, $password) = explode(':', $string);{   // checking to see if the Authorization string is valid
            $this->load->database();
            $sql  = 'SELECT COUNT(userid) AS records FROM users '.'WHERE authorization = "'.$headers['authorization'].'";';

            $query = $this->db->query($sql);
            $data = $query->row();
            if ($data->records == "0") {
                $info->status = 'failure';
                $info->error->code = 19;
                $info->error->text = 'authorization string is not valid';
                $this->response($info, 401);
            } 
    }}
    {
if (!isset($id)){// check if the ID is specified in the URI
    $info->status = 'failure';
            $info->error->code = 11;
            $info->error->text = 'id not specified in URI';
            $this->response($info, 400);
}}
 {  // if the resource exist
        $this->load->database();
        $sql = 'SELECT COUNT(id) AS records FROM reviews WHERE id = '.$id.';';

        $query = $this->db->query($sql);
        $data = $query->row();
        if ($data->records == "0") {
            $info->status = 'failure';
            $info->error->code = 12;
            $info->error->text = 'id does not exist or have a resource';
            $this->response($info, 404);
        }
 }{   // checking to see if the Authorization string is valid
            $this->load->database();
            $sql  = 'SELECT username from reviews where id = "'.$id.'" and username= (select username from users where username ="'.$username.'" and authorization = "'.$headers['authorization'].'");';

            $query = $this->db->query($sql);
            $data = $query->row();
            if ( $data == false) {
                $info->status = 'failure';
                $info->error->code = 19;
                $info->error->text = 'You do not have permission to delete this review';
                $this->response($info, 401);
            } 
    }
$this->load->database();
$sql= 'SELECT * FROM reviews WHERE id= "'.$id.'";';
$query= $this->db->query($sql);
$data->record = $query->row();
$criteria = array('id'=>$id);
$this->db->delete('reviews', $criteria);
$data->rows = $this->db->affected_rows();
$data->message = 'Review has been deleted';
$this->response($data,200);
}

您必须获得一个服务器端标志或带有类似post的变量

 function Login(){
    $.ajax({
        url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/'+$("#loginusername").val(),
    headers:  {authorization:window.btoa($("#loginusername").val()+':'+$("#loginpassword").val())},
    contentType: "text",
        dataType: 'json',
        success: function(data) {
            authorisation = window.btoa($("#loginusername").val()+':'+$("#loginpassword").val()); //create a variable to pass to the cookie
            createCookie('cookie',authorisation,0); //create our authorisation cookie for the client
            alert("Login Successful!");
            $("#loginbox").hide(); //hide the login button
            $("#logout").show(); //show the logout button
            $("#addreview").show(); //show the add a review button
            $("#adminpanel").show();//show the admin panel page
             $("#editreview").show();
//here is the code
             if(data.text=='show')
                $('#deletereview').show();
                            else 
                            $('#deletereview').hide();

            $("#loginusername").val(''); //clear the name box
            $.mobile.changePage("#home" ); //show the menu

        },
    error: function (response) {
        var r = jQuery.parseJSON(response.responseText);

        alert("Error:" + r.error.text);
                   }
            });
    /*rough code*/

    }

如果您使用,也可以使用jsonspecify数据类型。

但“显示”是什么,在我的HTML中,此时删除评论是隐藏的,但显示当用户登录PHP代码时允许用户删除评论,如果他们是更新它的人,所以它在PHP中工作,但现在我想隐藏它,这是您从服务器端回显的东西。您正在向[链接]发送一些数据,对吗?adduser函数返回一个json,如我所见。如果你把show放在JSONO的文本字段中,或者创建另一个字段,那么它就会工作。哦,我在结果中回显show吗?但是它怎么知道那是用户呢。因为我希望登录的用户允许他删除他的评论,但我希望这些按钮只在他的帖子上可见。