0

I am trying to echo some HTML using PHP in a div. Everything was working until I tried to pass a parameter to the onclick=\"loadpuzzle()\" function. It ran fine when I tried onclick=\"loadpuzzle(4)\" and the function took the parameter and executed normally, but what I need is to pass the value from $row['puzzlename'] as the parameter, which I assume is a string (and so I enclosed it in quotation marks).

echo "<td><input id=\"".$row['puzzlename']."\" type=\"radio\" name=\"puzzle-set\"     class=\"puzzle_selector\"  onclick=\"loadpuzzle(\"".$row['puzzlename']."\")\"/>  
        <label for=\"".$row['puzzlename']."\" class=\"puzz_label\">".$row['puzzlename']."</label></td>";

I am getting a weird error in Safari when I try to run this. "Unexpected token: '}' ".

Does anyone know what I am doing wrong here? Why won't it take this parameter?

Thank you!

6
  • maybe you could post the codes around this piece of code too Commented Jun 8, 2014 at 3:06
  • Can you provide an example of the generated markup that's being echoed? Commented Jun 8, 2014 at 3:07
  • Sorry? the codes around what piece of code? I'd be happy to, I'm just not sure what you mean :) Commented Jun 8, 2014 at 3:07
  • What do you mean a markup? I'm sorry! Haha I'm new to PHP/Javascript. Commented Jun 8, 2014 at 3:08
  • it says Unexpected token: }. clearly this isn't the only code in your php right? maybe you could post more as it is incomplete and cant be diagnosed further. Commented Jun 8, 2014 at 3:10

3 Answers 3

2

You could try escaping all of your double quotes like your doing or perhaps switch to single quotes for readability.

echo '<td><input id="' . $row['puzzlename'] . '" type="radio" name="puzzle-set" class="puzzle_selector" onclick="loadpuzzle(\'' . $row['puzzlename'] . '\')"/>
      <label for="' . $row['puzzlename'] . '" class="puzz_label">' . $row['puzzlename'] . '</label></td>';
Sign up to request clarification or add additional context in comments.

1 Comment

Oh... I thought double quotes were required in some of those places. Thank you! I'll have to go read up on the difference.
2
echo "<td><input id=\"".$row['puzzlename']."\" type=\"radio\" name=\"puzzle-set\"         class=\"puzzle_selector\"  onclick=\"loadpuzzle(\"".$row['puzzlename']."\")\"/>  
<label for=\"".$row['puzzlename']."\" class=\"puzz_label\">".$row['puzzlename']."</label></td>";

Since you are using onclick=\"loadpuzzle(\"".$row['puzzlename']."\") it will render into onclick="loadpuzzle("puzzlename")". Instead of sending parameters using double quotes use single quotes.

1 Comment

Thank you! Sorry, the last person beat you to it, but I appreciate your answer nonetheless :)
1

Your first double quote for the function parameter is actually closing the opening double quote for the onclick definition. Use a single quote instead like this:

echo "<td><input id=\"".$row['puzzlename']."\" type=\"radio\" name=\"puzzle-set\" class=\"puzzle_selector\"  onclick=\"loadpuzzle('".$row['puzzlename']."')\" /><label for=\"".$row['puzzlename']."\" class=\"puzz_label\">".$row['puzzlename']."</label></td>";

I would also think about rewriting this entire output to use WAY less escaping. It becomes increasingly unreadable and ahem error prone.

2 Comments

You're the best! This worked :) darn, I thought the \ tokens would take care of that... I guess not. Thank you!
Also, I will definitely consider writing this output to be more readable and less error prone. I think ANY programmer would. I just don't have the know-how and right now I'm at the stage where if it works, it works. Thank you for the suggestion though.

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.