Can i add javascript in php file?

Hi i am not a php developer, ive never touched it before. but i have been asked to add a google shopping cart tracking code to a website. when someone completes an order then get sent to finishorder.php. when i go the finishorder.php file it looks like this:

include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();

which just looks like server script to me (coming from a .net background), so i presume i cannot add the javascript here, how does php decide get the layout for this page? how can i add the javascript code to this page.

asked May 4, 2011 at 10:59

1

You can do this:

include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();

echo '<script type="text/javascript">YOUR JS HERE</script>';

OR

<?php
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
?>
<script type="text/javascript">YOUR JS HERE</script>

Hmm?

But I think that HandlePage() method will do something with our page so I'd look inside this method Class ISC_ORDER->handlePage() what it does... You can then echo Your within this method on appropriate place...

EDIT:

<?php
echo '<script type="text/javascript">//<!--
        alert("Hello to multiline JS script");
        alert("Do You get it?");
    //--></script>';
?>

Antony

14.8k10 gold badges44 silver badges73 bronze badges

answered May 4, 2011 at 11:03

shadyyxshadyyx

15.6k6 gold badges54 silver badges94 bronze badges

2

You can add javascript inside a php code as

<?php echo "<script> alert('this is a javascript code')</script>"; ?>

answered May 4, 2011 at 11:02

Can i add javascript in php file?

VaradaVarada

15.1k13 gold badges47 silver badges67 bronze badges

You can add script in PHP page by 2 ways

The first way is to add it in PHP tags

<?php 
//PHP CODE
if($_POST['submit']){
echo '<script>alert('Hello')</script>';
}
?>

The second way is to add it after PHP code

<?php 
//PHP CODE
?>
<script>
alert('Hello');
</script>

answered May 31, 2017 at 19:07

Can i add javascript in php file?

Hetal1311Hetal1311

3643 silver badges13 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    JavaScript is the client side scripting language and PHP is the server side scripting language. JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.

    Example 1: Write JavaScript code within PHP code

    <?php 

    echo '<script type="text/JavaScript"

         prompt("GeeksForGeeks");

         </script>'

    ;

    ?>

    Output:

    Can i add javascript in php file?

    Example 2: Write JavaScript code outside of PHP code (in same PHP file)

    <?php

    ?>

    <script type="text/javascript">

        alert('GeeksforGeeks!');

    </script>

    Output:

    Can i add javascript in php file?

    Example 3: JavaScript Function – DOM Manipulation (in same PHP file)

    <?php

        echo "<div id='demo'></div>";

    ?>

    <script type="text/JavaScript">

    var x = myFunction(11, 10);   

    document.getElementById("demo").innerHTML = x;

    function myFunction(a, b) {

        return a * b;             

    }

    </script>

    Output:

    110

    JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    Can you mix JavaScript and PHP?

    Besides, PHP and JavaScript similarities, these two languages are a powerful combination when used together. Large numbers of websites combine PHP and JavaScript – JavaScript for front-end and PHP for back-end as they offer much community support, various libraries, as well as a vast codebase of frameworks.

    Does JavaScript work with PHP?

    JavaScript can also talk with your PHP code on the web server whenever it needs to update something (either on the server or on the web page).

    How can I enable JavaScript in PHP?

    You can try with 2 metod:.
    setting cookies with JS and detecting them from PHP..
    creating a form with a hidden field and an empty value; and then assigning some value to it with JS, if the field gets the value – JS is ON, otherwise it's off..