简体   繁体   中英

Calling a javascript function while passing PHP variables

I am trying use a javascript function while passing php variables in it. For example:

onclick="alert(<?echo $row['username']?>)";

Now this does not work, instead gives an error- ReferenceError: Can't find variable:right_username (here the right_username is the answer i expect in the alert).

However if instead of username i use EmpID :

onclick="alert(<?echo $row['EmpID']?>)";

EmpID being an int in the database works just fine.

Because $row['username'] is a string, you need quote it, or the javascript will think it as a variable. $row['EmpID'] is a number, so it shows.

onclick="alert('<?echo $row['username']?>')";

您忘记了报价:

onclick="alert('<?echo $row['username']?>')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM