douxieqiu0651 2012-11-08 11:29
浏览 92
已采纳

在OOP PHP中调用方法

I am very new at OOP, and i want to get things right at the first learning cycle.

I have a HTML form:

<?php
include("classes/Gaestebog.php");
$gaestebog = new Gaestebog();
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
    <table>
        <tr>
            <td>Navn:</td>
            <td><input type="text" name="navn" value="Patrick" /></td>
        </tr>
        <tr>
            <td>Besked:</td>
            <td><input type="text" name="besked" value="Hej med dig !!" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="opret" value="Opret" /></td>
        </tr>
    </table>
</form>

</body>
</html>

and a Guestbook class:

<?php
class Gaestebog {

    public function Gaestebog() {

    }

    public function getPosts() {

    }

    public function addPost() {

    }
}
?>

I want the :Guestbook to invoke the addPost method on form submission. How would i approach this?

  • 写回答

4条回答 默认 最新

  • doulao1934 2012-11-08 11:39
    关注
    <?php
    if(isset($_POST)) {
        include("classes/Gaestebog.php");
        $gaestebog = new Gaestebog();
    
        $data = array(
            'title' => $_POST['title'], 
            'author' => $_POST['author'],
            'content' => $_POST['content']
            // etc. 
        ); // Do not forget to validate your data
    
        $gaestebog->addPost($data);
    }
    ?>
    <!-- The HTML part... --> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?