Anyone help me to add javascript to index.php file?

6 replies
This is script I want to add, like this
<?php echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';

It works. But I want to add multiple script to my php files.
It is not working when I put like this,

<?php echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';
'<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';

How to put two script?? Help me guys.

Thanks.
#add #file #indexphp #javascript
  • Profile picture of the author danmart
    Originally Posted by JerryAdler View Post

    This is script I want to add, like this
    <?php echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';

    It works. But I want to add multiple script to my php files.
    It is not working when I put like this,

    <?php echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';
    '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';

    How to put two script?? Help me guys.

    Thanks.
    I think I see the problem. You have the echo on the first script, then you terminate the command with the ; The second script does not have an echo command. If you put "echo" in front of the second script it will work.

    <?php echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';
    echo '<script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>';
    {{ DiscussionBoard.errors[1985014].message }}
    • Profile picture of the author Jeremy Morgan
      You can break out of php and then output the javascript:

      PHP Code:
      <?php

      // php stuff
      ?><script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>
      <script type="text/javascript" src="http://forms.example.com/form/86/1985011386.js"></script>
      <?php 

      // more php stuff

      ?>
      Or, if you really want to echo it out, you have to escape the quotes, like this:


      Sorry for posting an image, but the PHP parser on this forum strips characters crucial to the code.
      Signature
      Jeremy Morgan, Software Developer / SEO
      Learn AI Powered Content Creation
      at Prompt and Prosper
      {{ DiscussionBoard.errors[1985438].message }}
  • Profile picture of the author wayfarer
    Why do you believe that you need to echo the script to put it on a PHP page? PHP allows you to break from PHP and just enter normal HTML as you would as if it weren't a PHP page:
    PHP Code:
    <?php
    //do server scripting here
    ?>
    <!--HTML-->
    <?php
    //more server scripting here
    ?>
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1985414].message }}
    • Profile picture of the author JerryAdler
      Originally Posted by wayfarer View Post

      Why do you believe that you need to echo the script to put it on a PHP page? PHP allows you to break from PHP and just enter normal HTML as you would as if it weren't a PHP page:
      PHP Code:
      <?php
      //do server scripting here
      ?>
      <!--HTML-->
      <?php
      //more server scripting here
      ?>
      As I mentioned above, the script is working well for the first script. Then when I try to add more script, then it stop working. My question is how to add more script to the php page. Yes! Of course it need "echo" .If I did not put this, it will not works.

      Thanks.
      {{ DiscussionBoard.errors[1985425].message }}
      • Profile picture of the author wayfarer
        Originally Posted by JerryAdler View Post

        As I mentioned above, the script is working well for the first script. Then when I try to add more script, then it stop working. My question is how to add more script to the php page. Yes! Of course it need "echo" .If I did not put this, it will not works.
        It seems you're just speaking without actually trying to implement my suggestion. Trust me, an echo is not "needed". Look at Jeremy Morgan's example if my simpler example is confusing to you.
        Originally Posted by Jeremy Morgan View Post

        Sorry for posting an image, but the PHP parser on this forum strips characters crucial to the code.
        A pain, isn't it? You can scroll to the bottom in "advanced mode" of the editor, then uncheck the option "Automatically parse links in text" and your PHP won't get messed up when you post it.
        Signature
        I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
        {{ DiscussionBoard.errors[1988270].message }}
        • Profile picture of the author Jeremy Morgan
          Originally Posted by wayfarer View Post

          It seems you're just speaking without actually trying to implement my suggestion. Trust me, an echo is not "needed". Look at Jeremy Morgan's example if my simpler example is confusing to you.

          A pain, isn't it? You can scroll to the bottom in "advanced mode" of the editor, then uncheck the option "Automatically parse links in text" and your PHP won't get messed up when you post it.
          Thanks for the tip!
          Signature
          Jeremy Morgan, Software Developer / SEO
          Learn AI Powered Content Creation
          at Prompt and Prosper
          {{ DiscussionBoard.errors[3170543].message }}

Trending Topics