Forum Moderators: coopster

Message Too Old, No Replies

Problem with embedding JS code in PHP

         

ktsirig

5:10 pm on Apr 28, 2016 (gmt 0)

10+ Year Member



Hi all,

I have the following string as part of my PHP code (before you ask, it HAS to be in PHP, can't write PHP and HTML separately):


$submission_form ="
<p style=\"font-size:20px; padding: 15px;\">
<u>Please select the type of analysis you wish to perform</u>
</p>
<form name=\"analysis_form\" method=\"post\">
<div>
<a id=\"myHeader1-2\" href=\"javascript:showonlyonev2(\"newboxes1-2\");\" >
<input type=radio name=\"criterion\" value=\"single_genotype\">
Single-study analysis using genotypes
</a>
</div>


Everything works fine, except the JS part...Because it should have single quotes, but this I can't do it inside the PHP string...
Any ideas?

Andy Langton

5:21 pm on Apr 28, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use "heredoc" syntax to avoid all the escaping:


$str = <<<EOD
<html code goes here>
EOD;

ktsirig

5:55 pm on Apr 28, 2016 (gmt 0)

10+ Year Member



Hi, yes, I tried that but still I get error:


$submission_form = <<<EOD
<p style="font-size:20px; padding: 15px;"><u>Please select the type of analysis you wish to perform</u></p>
<div>
<form enctype="multipart/form-data" name="analysis_form" method="post" action="single_analysis_gen.php">
<a id="myHeader1-2" href="javascript:showonlyonev2('newboxes1-2');" >
<input type=radio name="criterion" value="single_genotype">
Single-study analysis using genotypes
</a>
</div>
</form>
EOD;

It works OK if I remove the
 href="javascript:showonlyonev2('newboxes1-2');" 
part...

Andy Langton

6:01 pm on Apr 28, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What error do you receive? The above works fine for me. Which PHP version is it?

robzilla

7:35 pm on Apr 28, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Because it should have single quotes, but this I can't do it inside the PHP string

You can if you enclose your variable's value with "double quotes", as you do, or vice versa.

$variable = "<a id=\"myHeader1-2\" href=\"javascript:showonlyonev2('newboxes1-2');\">";

OR

$variable = '<a id="myHeader1-2" href="javascript:showonlyonev2(\'newboxes1-2\');">';