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?
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;
}
}
}
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>
In order to achieve the desired code,you need to use AJAX.
In your js file you need to add
$('#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);
}
});
});
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);
});
});