1

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>
7
  • What's wrong with your first method? I'm not understanding. Commented Jun 30, 2016 at 18:39
  • Mixing three languages on one line of code is just begging for syntax problems. Commented Jun 30, 2016 at 18:40
  • the first method thinks the value of my variable is in fact a variable. 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. 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. Commented Jun 30, 2016 at 18:56

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

9 Comments

i tried to $test = json_encode($contact['idContact']); and then echo '<td><a href="#" onclick="delete('.$test.')"> Delete </a></td>'; but still give me error.
Even echo '<td><a href="#" onclick="delete('.json_encode($contact['idContact']).')"> Delete </a></td>'; give me error.
Uncaught SyntaxError: Unexpected token }
several of your examples look like they would work since (i assume) it's an id number not a string.. the error is most likely from somewhere else..
yea, you've got a } spmewhere where it doesn't belong.. this could just mean that you forgot a semicolon at the end of a line somewheree.. php expected a semicolon and got a curly instead.
|
1
<html>
<head>
<script src="https://ajax.googleapis.com/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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.