get php boolean and change css border color depends on it?

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.

-------------Problems Reply------------

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

You need to return the variable and the text, so you're going to have to use JSON. You need to change this part in your PHP file:

$return = array('text' => '', 'exists' => false);
if($results_exist) {
//out jquery echo
//echo "We're sorry, that username is not available.";
$return['text'] = "We're sorry, that username is not available.";
//return fail change border to red
$return['exists'] = true;
}else{
//echo "username is available.";
$return['text'] = "username is available.";
//return true change border to grey
//$_exist = false;
}
echo json_encode($return);

Then for your 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?//
$.ajax(function() {
type: 'POST',
url: 'class/validation.php',
data: {'username':username_value},
dataType: 'JSON' //This tells the ajax we're expecting a JSON response.
})
.done(function(data) {
$("#user-result").html(data.text);
// get the return data from php , fail = red , true = grey
$("#username").css('border-color', data.exists ? 'grey' : 'red');
});
}
return;
}

So you get the either the values red or grey returned? In that case you should set the css in the following way:

$("#username").css('border-color',data);

Category:php Views:4 Time:2018-12-31
Tags: php css jquery

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.223 (s). 11 q(s)