在AJAX中隐藏和显示

时间:2013-12-29 23:34:03

标签: 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").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);
}

1 个答案:

答案 0 :(得分:0)

你必须得到一个像

这样的帖子的服务器端标志或变量
 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*/

    }

您也可以使用json(如果使用,请指定dataType)。