Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form submit php function return value print in html

Tags:

html

php

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?

like image 381
Imrul.H Avatar asked May 14 '26 15:05

Imrul.H


2 Answers

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

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

});
like image 177
Neil Avatar answered May 17 '26 05:05

Neil


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;
        }
    }
}
like image 32
karllindmark Avatar answered May 17 '26 06:05

karllindmark