最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: identifier starts immediately after numeric literal in Firebug - Stack Overflow

programmeradmin5浏览0评论

I'm getting that error when I call this javascript function:

function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });  
}

this "kickUser" function is generated for every user connected to my chat box, like this

$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>&nbsp;
<a href="#" class="BtnKick" onClick="kickUser('.$rowUsers['id_user'].')">Kick</a></br>';

and the action "kick" is just an update to my database where I remove the user from my chatUsers table

If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to: ReferenceError: 'userName' is not defined (i changed the real name of the user for 'userName' just for this example).

I'm getting that error when I call this javascript function:

function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });  
}

this "kickUser" function is generated for every user connected to my chat box, like this

$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>&nbsp;
<a href="#" class="BtnKick" onClick="kickUser('.$rowUsers['id_user'].')">Kick</a></br>';

and the action "kick" is just an update to my database where I remove the user from my chatUsers table

If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to: ReferenceError: 'userName' is not defined (i changed the real name of the user for 'userName' just for this example).

Share Improve this question edited Feb 19, 2013 at 19:57 Filburt 18.1k12 gold badges82 silver badges140 bronze badges asked Feb 19, 2013 at 19:51 MolloMollo 7233 gold badges7 silver badges25 bronze badges 3
  • 5 Why is this tagged java? Do you mean javascript? – anoopelias Commented Feb 19, 2013 at 19:52
  • 1 yeah i meant javascript. This is my first time asking – Mollo Commented Feb 19, 2013 at 20:25
  • No Problem mate. Wele aboard. – anoopelias Commented Feb 22, 2013 at 16:53
Add a ment  | 

2 Answers 2

Reset to default 16

Identifiers in JavaScript can't begin with a number; they must begin with a letter, $ or _.


I'm guessing it's ing from this:

onclick="kick_user('.$rowUsers['id_user'].')">Kick</a>

If you mean to pass a string, then you need to quote the value being passed.

onclick="kick_user(\"'.$rowUsers['id_user'].'\")">Kick</a>

I don't know PHP, so maybe you need different escaping, but this should give you the idea.

The resulting JavaScript code will be

kickUser(userName)

…and obviously there is no js variable userName. You want to pass a string instead:

kickUser('userName');

So add the quotes/apostrophes to the output, and don't forget to escape the $rowUsers['userName'] properly. It's quite the same for $rowUsers['id_user'], which seems to have output even an invalid identifier.

发布评论

评论列表(0)

  1. 暂无评论