Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Javascript function in PHP

Tags:

javascript

php

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!

like image 640
robobobobo Avatar asked Jul 13 '26 10:07

robobobobo


1 Answers

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.

like image 197
Paul Avatar answered Jul 14 '26 23:07

Paul