2

so I have this php code

$listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>";

$id is an integer and works just fine, key is a reference from a for loop and the value translate well in the html code however the key value table is recognised as a variable instead of a string.

HTML: onclick="updateValue(3, table)"

I have tried evey trick I know (creating new variable with quotes, adding single quotes in the onclick and (string)$key ) to convert it as a string but nothing is working so far. any idea please ? is it even possible ?

1
  • scape the quotes Commented Jan 24, 2018 at 19:40

2 Answers 2

7

You need to add quotes to tell javascript that it's a string. Since you're inside an echo you'll need to escape the quotes:

$listing .= "<button onclick='updateValue(".$id.", \"".$key."\")'>change value</button>";
Sign up to request clarification or add additional context in comments.

5 Comments

i think he's saying the key is a variabel
He can also do ' ". $key ." ' where the single quotes makes it a JS string and the double quotes escape it from PHP.
@LeviSchmidt No, the single quotes are already used to wrap the onclick action.
@Iwrestledabearonce. I read it as "if $key has the value table, javascript sees a variable table instead of the string 'table'"
thats it, thanks a bunch, I learned something ! will accept asap
2

Try this way

$listing .= "<button onclick='updateValue(".$id.", \"".$key."\")'>change value</button>";

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.