donglv9116 2009-02-27 10:59
浏览 81

PHP嵌入在Javascript中

in my project I have the situation like this.

<script language="JavaScript">
function fonsubmit()
{
    alert('Out side PHP');
    //Here the php code starts 
    include("customerpage.php");
    $cc="us";
    $clang="en";
    $cpath="us"; 
    some coding goes here......
    Php ends here
}
</script>

But it shows Object expected error. What is wrong in this? But I give only PHP tag it won't generate any error. Please help me to find out this issue, thanks in advance...

  • 写回答

5条回答 默认 最新

  • doujiaohuo1096 2009-02-27 11:09
    关注

    You're trying to embed a server based language in a client-side based language. This will not work.

    If you could explain a bit more what it is you're trying to achieve there might be a better solution, like AJAXie type thing.

    Let me explain a bit more as others have given answers that could be put here.

    You can have php embedded inside your *.php files which will be interpretted on the server and parsed there. Using this you can set variables which javascript could pick up on later and use on the client-side. Like I said though the client side doesn't have a route for intpretting and parsing php and so sending php across to the user is not going to work.

    If you are however trying to get things working so that your php sets some javascript values then I would, like Gary Willough and Tudor Olariu both say, add the php tags

    <?php
    ... PHP CODE...
    ?>
    

    However, you have to realise this will be interpretted on the server and not on the client. Therefore the following code would alert with "outside php" and then "inside php".

    <script language="JavaScript">
    function fonsubmit()
    {
       alert('outside php');
       <?php
         $message = "inside php";
         echo "alert('$message');";
       ?>
    }
    </script>
    

    The html sent to the user would then be

    <script language="JavaScript">
    function fonsubmit()
    {
       alert('outside php');
       alert('inside php');
    }
    </script>
    

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入