lundi 2 mars 2015

Passing a PHP variable into Javascript returns NULL


I know there are a lot of questions covering something similar, but I've tried the answers without any luck.


Snippet of PHP:



$usernum_query = "SELECT numPersonID FROM tbllogins WHERE txtUserName='$currentuser'";

if(!$usernumber = $db1->query($usernum_query)){
die('There was an error running the usernumber query [' . $db1->error . ']');
}

while($row = $usernumber -> fetch_assoc()) {
$userIDnum = $row['numPersonID'];
$userIDnum = utf8_encode($userIDnum);
}


Snippet of Javascript:



$(function(){

$("#0").click(function(){

var userIDnum = <?php echo json_encode($userIDnum); ?>;
alert(userIDnum);


The most common answer I've come across says to UTF_encode my variable, which I think I've done correctly. I've also tried:



var userIDnum = <?php echo $userIDnum; ?>;


Which doesn't work.


In my HTML outside of the script,



<?php echo json_encode($userIDnum); ?>


return "90" (with the quotes)



<?php echo $userIDnum; ?>


returns 90 (without the quotes).


Within the script, I get null and no alert box, respectively.


Any ideas as to why the variable isn't passed into the script? Thanks!


edit: tried with quotes and got the same result





Aucun commentaire:

Enregistrer un commentaire