2

I have this simple php code that displays all users' posts with all of them having a unique 'like' button:

$sql = "SELECT * FROM posts";
$result = mysqli_query($con,$sql);

while($row=mysqli_fetch_assoc($result)){
  $post_content = $row['content'];
  $post_id = $row['id'];

  echo $post_content ."<br/>";
  echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id )>Like</a>";
}

And my javascript:

function like_add(post_id){
  alert(post_id); //to test if the link is working
}

I got this error however:

ReferenceError: 2dg7c is not defined

Here 2dg7c is an id of a post in which I clicked LIKE.
Did I missed something?

12
  • did you try "like_add('$post_id')" ?? Commented Jul 11, 2017 at 12:06
  • Actually, I should throw a syntax error, right? Something like: SyntaxError: identifier starts immediately after numeric literal. Commented Jul 11, 2017 at 12:08
  • echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id )>Like</a>"; in this line $post_id is a string and you are passing without quotes so it will not work. you have to use `\` Commented Jul 11, 2017 at 12:10
  • 1
    Incidentally, you're not passing to jQuery, you're passing to JavaScript. Please, quit using alert() for troubleshooting., use console.log() instead. Commented Jul 11, 2017 at 12:24
  • 1
    it's working for me . i think you have some other problem @Dranreb Commented Jul 11, 2017 at 12:30

7 Answers 7

3

Change link code like this:-

echo "<a href='#' id='likebutton' onclick='like_add(\"{$post_id}\")'>Like</a>";
Sign up to request clarification or add additional context in comments.

4 Comments

I got an error like this bro ` expected expression, got '}'`
On the first one,, I got an illegal character error. The second one was my original code which is not working..
I followed the second one and still got this error : ReferenceError: 2dg7c is not defined ... Can you post a working snippet of this issue sir? Maybe something's just really wrong with my codes
@Dranreb glad to help you :)
3

You need to escape the double quotes using slash (\) like this

echo "<a href ='#' id ='likebutton' onclick='like_add(\"{$post_id}\")'>Like</a>";

2 Comments

Why should the OP "use like this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
I really would also remove the spaces around the =, but this works. Surprised at others not testing their code.There is a space on both sides of the =.
3

Always try to pass value in quotes.

echo "<a href = '#' id = 'likebutton' onclick = 'like_add('".$post_id."' )'>Like</a>";

1 Comment

it wil not work. Because onclick event started with using '. And you trying to do this -> onclick='like_add('2dg7c')'
2

Try: echo "<a href = '#' id = 'likebutton' onclick = 'like_add(\"$post_id\")'>Like</a>";

You should add " before passing the argument to function. In your state, javascript thinks you referencing a variable as argument. But you want to send string.

1 Comment

Try this : echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id )>Like</a>";
2

Try this :

echo '<a href = "#" id = "likebutton" onclick = "like_add({$post_id})">Like</a>';

Reason :

There is a problem of concatenation. Php variable is not binding in string. To bind it we can break the long string and concatenate by dot. or use {} or use heredocs.

5 Comments

Do or do not, there is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
Did not worked either man,, gave me an error.. I think it's because of the "
@JayBlanchard updating my answer
Your code returns this <a href = "#" id = "likebutton" onclick = "like_add({$post_id})">Like</a>. Please test before posting an answer. The variable does not get parsed.
inside the single quotes everything is consider as a string
2

You just missed to close single quote here

echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id)>Like</a>";

Just replace the above with

echo "<a href = '#' id = 'likebutton' onclick = 'like_add(\"$post_id\")'>Like</a>";

1 Comment

I got this error bro = expected expression, got '}'
0

This is how you should write

echo "<a href='#' id='likebutton' onclick=like_add('".$post_id."')>Like</a>";

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.