3

I can't for the life of me figure out how to store this javascript function in a php variable. Basically I want to store this function as a standard string in a php variable, and then it get's printed out on a page. I know that I have to escape javascript to have it work with PHP, but the reason I'm stuck on this is because this particular Javascript and HTML combination seems to make use of both " and ' so I can't figure out how to escape it. Maybe you guys could help me out?

Here's the code I want to store in the php variable:

<a href='javascript:PopupContact_OpenForm("PopupContact_BoxContainer","PopupContact_BoxContainerBody","PopupContact_BoxContainerFooter");'><img src='/popup-contact-form.jpg' /></a>
<div style="display: none;" id="PopupContact_BoxContainer">
    <div id="PopupContact_BoxContainerHeader">
        <div id="PopupContact_BoxTitle">Contact Us</div>
        <div id="PopupContact_BoxClose"><a href="javascript:PopupContact_HideForm('PopupContact_BoxContainer','PopupContact_BoxContainerFooter');">Close</a></div>
    </div>
    <div id="PopupContact_BoxContainerBody">
        <form action="#" name="PopupContact_Form" id="PopupContact_Form">
        <div id="PopupContact_BoxAlert"> <span id="PopupContact_alertmessage"></span> </div>
        <div id="PopupContact_BoxLabel_Page"> Your Name </div>
        <div id="PopupContact_BoxLabel_Page"><input name="PopupContact_name" class="PopupContact_TextBox" type="text" id="PopupContact_name" maxlength="120"></div>
        <div id="PopupContact_BoxLabel_Page"> Your Email </div>
        <div id="PopupContact_BoxLabel_Page"><input name="PopupContact_email" class="PopupContact_TextBox" type="text" id="PopupContact_email" maxlength="120"></div>
        <div id="PopupContact_BoxLabel_Page"> Enter Your Message </div>
        <div id="PopupContact_BoxLabel_Page"><textarea name="PopupContact_message" class="PopupContact_TextArea" rows="3" id="PopupContact_message"></textarea></div>
        <div id="PopupContact_BoxLabel_Page"><input type="button" name="button" class="PopupContact_Button" value="Submit" onClick="javascript:PopupContact_Submit(this.parentNode,'/popup-contact-form/');"></div>
    </form>
    </div>
</div>
        <div style="display: none;" id="PopupContact_BoxContainerFooter"></div>

Hopefully you can see what I mean, I want to store it in my $button variable

Thanks!

4 Answers 4

4

You could put the whole thing in single quotes and then escape every single quote within it like \', but a much nicer approach would be to use PHP's nowdoc syntax:

$str = <<<'STR_HTML'
  // All your HTML goes here
STR_HTML;

If you're PHP version is earlier than 5.3 you can't use nowdoc, so you should use heredoc instead. The difference is like the difference between double quotes (heredoc) and single quotes (nowdoc).

See the PHP manual page on strings for more information.

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

1 Comment

That was it! Thank you! Can't believe I'd never heard of that before! To think I'd wasted hours on this! Thank You!
2

The best thing you can do, is to move your JavaScript functions to separate files. Mixing them into your PHP will create a lot of clutter.

In your PHP:

<html>
    <head>
        <script src="path/to/my/script.js"></script>
    </head>
    <body>
    ....
</html>

In the script file:

function PopupContact_OpenForm( ... ) {
    ...
}

This will make it much more easy to organize your source code and add more functions without mixing PHP and JavaScript.

(If you still want to keep everything in the PHP file, use HEREDOC as the others suggest.)

3 Comments

Yes I agree, but the javascript is actually in seperate files, I'm merely calling the functions in the php, most of the bulk there is just html stuff
@robobobobo Although I gave you an answer that works, this answer is very good advice. You should look into completely separating your code, so that you don't have any HTML or anything else that is not PHP in a PHP file.
@robobobobo It is really helpful to use a templating engine like Twig to design your HTML in separate template files.
1

heredoc syntax to the rescue!

Comments

0

It's really not good to have all of that in a variable mixing code and html.

To answer your question though, use a heredoc:

$bar = <<<LABEL
Nothing in here...
LABEL;

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.