6 Replies - 489 Views - Last Post: 28 April 2016 - 11:24 AM Rate Topic: -----

#1 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Problem with embedding JS code in PHP

Posted 28 April 2016 - 10:13 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with embedding JS code in PHP

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 10:24 AM

You use a HEREDOC statement.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,836
  • Joined: 08-August 08

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 10:41 AM

View Postktsirig, on 28 April 2016 - 12:13 PM, said:

(before you ask, it HAS to be in PHP, can't write PHP and HTML separately):

That's insane.

Javascript should be referenced in the HTML, like CSS. Using a heredoc will work, but if I couldn't reference it, I'd file_get_contents() it.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 10:44 AM

That would be cleaner and easier to manage in the long term. On a daily basis I deal with a mess of code with embedded html and javascript thru a legacy system.

I think the "IT HAS TO BE" could be replaced with a more telling statement. I have people over me that see nothing wrong with how it was done. The same people have a 20,000 line html template for an entire application. Yes, it can work, but maintaining it becomes a problem long term.
Was This Post Helpful? 0
  • +
  • -

#5 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 10:59 AM

Hi all and thanks for the help!
I tried:

$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;



but it does not work, only if I remove:

 href="javascript:showonlyonev2('newboxes1-2');" 



it does work
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 11:05 AM

Define 'does not work'? When I run your posted code. I get this error:

Quote

ReferenceError: showonlyonev2 is not defined
showonlyonev2('newboxes1-2');


Are there any errors in your browser tools?
Was This Post Helpful? 0
  • +
  • -

#7 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Problem with embedding JS code in PHP

Posted 28 April 2016 - 11:24 AM

View Postktsirig, on 28 April 2016 - 06:13 PM, said:

(before you ask, it HAS to be in PHP, can't write PHP and HTML separately)

Could you explain why? I'm simply curious.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1