doomm4711 2013-06-26 09:59
浏览 173

表单提交php函数返回值打印在html中

I have a html form. when i submit the form, it manages to call a function. i want to show the result in the html page. the picture is like this: THE HTML

<form method="post" action="" enctype="multipart/form-data" name="uploadForm">
<input name="test" />
<input type="submit" name="submit" />
</form>

<div id="data"><?php echo $result; ?></div>

PHP CLASS

<?php
class Test{

    public function __construct(){
        if(isset($_POST['submit'])){
            $result = $this->myfunction();
        }
    }
        private function myfunction(){
        //some actions
        return "values";
        }
}

I am just giving the idea. the class file is different php file and so the html is. the instance of the class is created. how can i show the result "values" in the div id="data" after submitting the form?

  • 写回答

4条回答 默认 最新

  • douren9077 2013-06-26 10:07
    关注

    Although your approach seems a bit weird, here's a version that should be working:

    Your html file:

    <?php /* Includes here */ ?>
    <?php $result = new Test($_POST)->getResult(); ?>
    <form method="post" action="" enctype="multipart/form-data" name="uploadForm">
    <input name="test" />
    <input type="submit" name="submit" />
    </form>
    
    <div id="data"><?php echo $result; ?></div>
    

    Your php class:

    <?php
        class Test{
    
            private $result = '';
    
            public function __construct($postData){
                if(isset($data['submit'])){
                    $this->result = $this->myfunction();
                }
            }
    
            private function myfunction($postData){
                //some actions
                return "values";
            }
    
            public function getResult() {
                return $this->result;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示