I’m trying to display a PHP variable using javascript/jquery but it’s displaying ‘null’.
if(mysql_num_rows($checkBan) > 0){
$bannedDate = $checkBan['banLength'];
if(preg_match('/[0-9]/',$bannedDate)){
list($yyyy,$mm,$dd) = explode('-',$bannedDate);
$date = $mm."-".$dd."-".$yyyy;
}
//$date = "test"; when this is uncommented it appears in the alert so I know the json_encode is working fine
?>
<script type ="text/javascript">
var bannedUntil= <?php echo json_encode($date); ?>;
alert('Your account has been banned until ' + bannedUntil +'. Please contant an administrator if you believe this is an error');
</script>
<?
}
The alert appears just fine, but the bannedUntil variable is null. However, when the second date variable is uncommented it appears in the alert. It’s not a separate function so I don’t see why the scope would be an issue.
I am seeing you use
$checkBanas a result resource inmysql_num_rows(), then attempt to access an array key from it without fetching. You appear to be missing a call tomysql_fetch_assoc():Another tip: It looks like you are getting a standard MySQL date format back as
YYYY-MM-DDand converting it with string operations in PHP toMM-DD-YYYY. Just retrieve it in that format in your query to begin with and avoid theexplode()andlist()calls in your PHP application code.