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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17317028/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:40:26  来源:igfitidea点击:

form submit php function return value print in html

phphtml

提问by Imrul.H

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

我有一个 html 表单。当我提交表单时,它设法调用一个函数。我想在 html 页面中显示结果。图片是这样的: 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 类

<?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?

我只是给出想法。类文件是不同的 php 文件,所以 html 是。类的实例被创建。提交表单后,如何在 div id="data" 中显示结果“值”?

回答by Neil

Using AJAX we can display result in that particular div as follows :

使用 AJAX,我们可以在该特定 div 中显示结果,如下所示:

$('#submitBtn').click(function(){
   $.post("YourFile.php",{$('#form').serialize()},function(res){
       $('#data').html(res);
   });

});

回答by karllindmark

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

虽然你的方法看起来有点奇怪,但这里有一个应该可以工作的版本:

Your html file:

您的 html 文件:

<?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类:

<?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;
        }
    }
}

回答by bIgBoY

In order to achieve the desired code,you need to use AJAX.

为了实现所需的代码,您需要使用 AJAX。

In your js file you need to add

在您的 js 文件中,您需要添加

$('#form type=[submit]').click(function(e){
   e.preventDefault();//prevents default form submit.
   $.ajax({    //fetches data from file and inserts it in <div id="data"></div>
     url:'your url here',
     data:{data:$(#form).serialize()},
     success :function(data){
$('#data').html(data);
     }
   }); 
});  

回答by M Khalid Junaid

You have to include the file or code

您必须包含文件或代码

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

if(isset($_POST)){
 $test = new Test();
$result=$test->result;
}
?>
<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>