3

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?

3
  • 4
    If you don't want to reload the page, you need to read about something called AJAX. Commented Jun 26, 2013 at 10:04
  • Use AJAX for this. Commented Jun 26, 2013 at 10:04
  • to show results use print_r($_POST); and if you don't want to reload page use AJAX as suggested above. Commented Jun 26, 2013 at 10:07

4 Answers 4

4

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

});
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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>

1 Comment

Side note: Afaik, $_POST will always be set, so you don't really need to do the isset($_POST). :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.