基本的php问题。将 javascript 添加到 .php 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5882341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
basic php question. adding javascript to .php page
提问by phil
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:
嗨,我不是 php 开发人员,我以前从未接触过它。但我被要求向网站添加谷歌购物车跟踪代码。当有人完成一个订单然后被发送到finishorder.php。当我转到finishorder.php 文件时,它看起来像这样:
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.
这对我来说就像服务器脚本(来自 .net 背景),所以我认为我无法在此处添加 javascript,php 如何决定获取此页面的布局?如何将 javascript 代码添加到此页面。
回答by shadyyx
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...
但我认为 HandlePage() 方法会对我们的页面做一些事情,所以我会看看这个方法 Class ISC_ORDER->handlePage() 它做了什么......然后你可以在这个方法中的适当位置回显你......
EDIT:
编辑:
<?php
echo '<script type="text/javascript">//<!--
alert("Hello to multiline JS script");
alert("Do You get it?");
//--></script>';
?>
回答by Varada
You can add javascript inside a php code as
您可以在 php 代码中添加 javascript 作为
<?php echo "<script> alert('this is a javascript code')</script>"; ?>
回答by Hetal1311
You can add script in PHP page by 2 ways
您可以通过两种方式在 PHP 页面中添加脚本
The first way is to add it in PHP tags
第一种方式是在PHP标签中添加
<?php
//PHP CODE
if($_POST['submit']){
echo '<script>alert('Hello')</script>';
}
?>
The second way is to add it after PHP code
第二种方式是在PHP代码后添加
<?php
//PHP CODE
?>
<script>
alert('Hello');
</script>