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

javascript - Pass PHP variable on OnClick method - Stack Overflow

programmeradmin4浏览0评论

I already tried all the methods i saw here in Stackoverflow but im getting errors.

Im trying to pass a php variable value to a javascript function, im trying to do both methods:

echo '<td><a href="#" onclick="delete('.$contact['idContact'].')"> Delete </a></td>';

This method above thinks the value of the idContact is actually a variable, so he is trying to get the value of the variable "105" (the value of idContact)

echo '<td><a href="#" onclick="delete("'.$contact['idContact'].'")"> Delete </a></td>';

This method gives me

Uncaught SyntaxError: Unexpected token }

What is the correct way to do it? Thanks in advance.

Here is a simple code to show my problem:

<html>

<head>
<script>

function eliminate(test)
{
    alert(test);
}

</script>
</head>

<?

$test = "name";

echo '<a href="#" onclick="eliminate('.$test.')"> Delete </a>';

?>

</html>

I already tried all the methods i saw here in Stackoverflow but im getting errors.

Im trying to pass a php variable value to a javascript function, im trying to do both methods:

echo '<td><a href="#" onclick="delete('.$contact['idContact'].')"> Delete </a></td>';

This method above thinks the value of the idContact is actually a variable, so he is trying to get the value of the variable "105" (the value of idContact)

echo '<td><a href="#" onclick="delete("'.$contact['idContact'].'")"> Delete </a></td>';

This method gives me

Uncaught SyntaxError: Unexpected token }

What is the correct way to do it? Thanks in advance.

Here is a simple code to show my problem:

<html>

<head>
<script>

function eliminate(test)
{
    alert(test);
}

</script>
</head>

<?

$test = "name";

echo '<a href="#" onclick="eliminate('.$test.')"> Delete </a>';

?>

</html>
Share Improve this question edited Jun 30, 2016 at 19:15 Adrian asked Jun 30, 2016 at 18:37 AdrianAdrian 3152 gold badges3 silver badges17 bronze badges 7
  • What's wrong with your first method? I'm not understanding. – showdev Commented Jun 30, 2016 at 18:39
  • Mixing three languages on one line of code is just begging for syntax problems. – David Commented Jun 30, 2016 at 18:40
  • the first method thinks the value of my variable is in fact a variable. – Adrian Commented Jun 30, 2016 at 18:48
  • I know theere isnt a } in my code but if i remove my variable it works. If i do onclick=delete() i dont get any error so the error is in this line. – Adrian Commented Jun 30, 2016 at 18:49
  • I believe "delete" is a reserved word in JavaScript. Try renaming your function to something else, like "deleteEntry". Example here. – showdev Commented Jun 30, 2016 at 18:56
 |  Show 2 more ments

3 Answers 3

Reset to default 2

gotta quote it if it's a string or else js will assume it's a variable

echo '<td><a href="#" onclick="delete(\''.$contact['idContact'].'\')"> Delete </a></td>';

note the escaped quotes on either side \'

a better way of passing variable from php to js is to json encode them, that will take care of quotes and things...

<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
   function eliminate(test){
     alert(test);
   }
<script>
</head>
<body>
<?php $test="name"; ?>
<a href="" onclick="eliminate('<?php echo $test; ?>')">delete</a>
</body>
</html>

to avoid confusion use heredoc for this

$button=<<<HTML
<a href="#" onclick="eliminate('$test')"> Delete </a>
HTML;
echo $button

Also remeber if you are using json as paramter make sure to escape them properly or it will not work

发布评论

评论列表(0)

  1. 暂无评论