Jim
Jim

Reputation: 2828

Variable in Joomla Module

I am busting my head to the wall .... I am making a simple joomla module, in helper.php I cant assign values posted from the form.

<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

class modReservationHelper {
    public $name;
    public $email;
    public $message;
    public $comment;

    protected function  __construct() {
        $this->name = $_POST['fullname'];
        $this->email = $_POST['email'];
        $this->message = $_POST['message'];
        $this->comment = $_POST['comment'];
    }

     function validateForm() {
        echo $this->name;   //The output is always 0
        echo $this->email+"</br>";//The output is always 0
        echo $this->message;//The output is always 0


        //When I try
        echo $_POST['comment']; // Is correct
        }   
    }

?>

Also I've tried not to use constructor with the same Zero effect:(

<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

class modReservationHelper {
    public $name;
    public $email;
    public $message;
    public $comment;


    function  getValues() {
        $this->name = $_POST['fullname'];
        $this->email = $_POST['email'];
        $this->message = $_POST['message'];
        $this->comment = $_POST['comment'];
    }

     function validateForm() {
        modReservationHelper::getValues;
        echo $this->name;   //The output is always 0
        echo $this->email+"</br>";//The output is always 0
        echo $this->message;//The output is always 0

        //When I try
        echo $_POST['comment']; // Is correct
        }   
    }

?>

The whole procedure is called from "mod_wreservation.php" I call modReservationHelper::validateForm();

Upvotes: 0

Views: 1608

Answers (1)

Gaurav
Gaurav

Reputation: 28755

You are calling the class in static manner. So $this in class will not be object of modReservationHelper.

The right way to use this as in mod_wreservation.php is

$helperObj = new modReservationHelper(); // your choice will work (__counstruct) with this
$helperObj->validateForm();

For second choice

$helperObj = new modReservationHelper();
$helperObj->setValue();
$helperObj->validateForm();

and class will be

<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

class modReservationHelper {
    public $name;
    public $email;
    public $message;
    public $comment;


    function  setValues() {
        $this->name = $_POST['fullname'];
        $this->email = $_POST['email'];
        $this->message = $_POST['message'];
        $this->comment = $_POST['comment'];
    }

     function validateForm() {            
        echo $this->name;   //The output is always 0
        echo $this->email+"</br>";//The output is always 0
        echo $this->message;//The output is always 0

        //When I try
        echo $_POST['comment']; // Is correct
        }   
    }

?>

and it will be better if you use this in mod_wreservation.php

$post = JRequest::get('post');
$helperObj = new modReservationHelper();
$helperObj->setValue($post);
$helperObj->validateForm();

Upvotes: 2

Related Questions