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?
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);
});
});
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With