H_MZ 2015-11-22 23:04 采纳率: 0%
浏览 13

在PHP类中使用Ajax

I am building a simple website using php in a mvc fomat and I'm trying to use ajax to call a simple clock but having trouble. Right now im just tyring to get a "hello world" example in my code to get started and go from there. An example format of my php class is : (the hello.php just echos hello world, and a Run() is being called at an index.php). For the life of me I can not get the script to work , any help would be appreciated. Thank you.

<?php
class MyFile{
   public static function Run(){
       MyFile::show();
   }
   public static function show(){
        echo 'html stuff';
    ?>
    //run this sample ajax script i found //
    <body>
    <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
     $(function(){
       $.get("hello.php", function(data){
           alert(data);
       });
     });
     </script>
    </body>

    <?php 
   }
 }
?>
  • 写回答

1条回答 默认 最新

  • weixin_33674976 2015-11-22 23:25
    关注

    If we disregard the fact that your provided example code is a mess of PHP + HTML.

    The provided code works just fine if you know how to call it via php, e.g.:

    $obj = new MyFile(); // instantiate obj/class
    $obj::Run();         // execute static method
    

    Advice in general: try to avoid writing mixed "view (html)" + "logic (php)" code especially like the example you provided where it's a class no less.

    If you can't help it at least you can make it look cleaner by using include or similar in php.

    Here is a bit of longer explanation why you should try to avoid mixing:

    评论

报告相同问题?