dtgsl60240 2015-06-17 08:08
浏览 32
已采纳

得到php布尔值并改变css边框颜色取决于它?

php

public function livecheck($check_value, $value){
    global $connection;

    $query = "SELECT * FROM users WHERE $check_value = ? LIMIT 1";
    $results = $connection->select($query,$value);

    //return total count
    $results_exist = $results->rowCount();
    $results_exist = $results->fetch();

    //if value is more than 0, username is not available
    if($results_exist) {
        //out jquery echo
        echo "We're sorry, that username is not available.";
        //return fail change border to red
        $_exist = true;
    }else{
        echo "username is available.";
        //return true change border to grey
        $_exist = false;
    }
}

jquery

function checkusername(){
        var username_value = $("#username").val();

            if (username_value.length >= 4){
            //checking the database username exits
            // $("#user-result").html('<img src="imgs/ajax-loader.gif" />');
            // post value isit using it self?//
            $.post('class/validation.php', {'username':username_value}, function(data) {
            $("#user-result").html(data);
            // get the return data from php , fail = red , true = grey
            $("#username").css(data);
            });
        }
        return;
    }

this is a live validation checking my database username and my intention is use jquery to change my css border color depends on my php return Boolean , i was able to echo the Boolean data ,so php check if username exists , border = red , else border = grey.

i hope i explain it right , i cant figure it out where to start this.

  • 写回答

3条回答 默认 最新

  • dsk49208 2015-06-17 08:17
    关注

    Don't just send the message from PHP. Send JSON object. And depending on the value of exists inside object change the colour of the textbox.

    PHP:

    public function livecheck($check_value, $value){
        global $connection;
    
        $query = "SELECT * FROM users WHERE $check_value = ? LIMIT 1";
        $results = $connection->select($query,$value);
    
        //return total count
        $results_exist = $results->rowCount();
        $results_exist = $results->fetch();
    
        //if value is more than 0, username is not available
        if($results_exist) {
            //out jquery echo
            $response = array(
                "message"=> "We're sorry, that username is not available.",
                "exist"=> true
            );
            // ^^^^^^^^^^^^^^
        } else {
            $response = array(
                "message"=> "username is available.",
                "exist"=> false
            );
            // ^^^^^^^^^^^^^^
        }
        echo json_encode($response);
    }
    

    Javascript:

    function checkusername() {
        var username_value = $("#username").val();
    
        if (username_value.length >= 4) {
            //checking the database username exits
            // $("#user-result").html('<img src="imgs/ajax-loader.gif" />');
            // post value isit using it self?//
            $.post('class/validation.php', {
                'username': username_value
            }, function (data) {
                var usernameAvailable = JSON.parse(data);
                $("#user-result").html(usernameAvailable.message);
    
                var color = usernameAvailable.exist ? 'gray' : 'red';
    
                $("#username").css('border-color', color);
            });
        }
        return;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动