1

What a weird question. If anyone has a better title for this, please go ahead and edit it; I can't think of what else to call it. I am printing a button from PHP using echo. The purpose of a button is to show a form by calling a JavaScript function that uses document.getElementById() to change the style attribute of the form tag to visible so the form is visible to the user. The problem I'm having is that the echoed string has to have quotes around it, the onclick event has to have quotes around it, and the parameter passed to the JavaScript function has to have quotes around it. I tried escpaing the quotes for the parameter with backslashes but that didn't work. Can anyone help me?

Here is my code:

echo "<input type = 'button' onclick = 'showform(\'psswdform\')' value = 'Change password'>";//this shows the form and needs quotes

JavaScript function:

function showform(id)
{
document.getElementById(id).style.visibility = "visible";
}
2
  • 1
    You can use single quotes where possible but you have to escape your double quotes in for instance showform(\"psswdform\") Commented Apr 25, 2013 at 9:55
  • 1
    You can't have single quotes inside single quotes... When the parser encounters the second single quote it thinks that's it. Unless you're not aware that PHP will escape those single quotes. Commented Apr 25, 2013 at 10:03

4 Answers 4

3

This works fine for me? (working example)

<?php

echo "<input type = 'button' onclick = 'showform(\"psswdform\")' value = 'Change password'>";//this shows the form and needs quotes

Generates:

<input type = 'button' onclick = 'showform("psswdform")' value = 'Change password'>
Sign up to request clarification or add additional context in comments.

Comments

2

Try concatenating the string using a combination of single and double quotes like this:

echo '<input type="button" onclick="showform(' . "'psswdform'" . ')" value="Change password">';

Comments

1

Try this:

echo "<input type = 'button' onclick = 'showform('psswdform')' value = 'Change password'>";

Additionally, you could try jQuery, a super popular JS Library that makes life easier.

I would make the following CSS class.

.hide {
   display:none;
}

Than I would add the following jQuery:

$("#show-password-reset").click(function(event){
 event.preventDefault();
 $("#passwdform").show();
});

And finally include your show button:

<input id="show-password-reset" type = 'button' value = 'Change password'>

Make sure to use class="hide" on the passwdform element so its hidden when page loads.

Comments

0

Inside PHP \ is already an escape character. If you want to output "\" you need to echo "\\".

2 Comments

I see you already got your answer, just thought I can elaborate a bit. What the accepted example did was change the inner single quotes to double quotes that are then escaped in the php string. My suggestion (although written with not much explanation) was to keep using the single quote. So by doing echo "onclick = 'showform(\\'psswdform\\')'" your output will be onclick = 'showform(\'psswdform\')' and that will again be at execution time parsed as showform('psswdform'), so you end up with the correct result.
oh, I see now. I'm sorry for my comment, I just didn't understand

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.