0

I made a Joomla module which contains a form element select box. On selecting, it submits the form and the value of selected item is stored in a session variable.

<?php
defined('_JEXEC') or die;

$session = JFactory::getSession();

if (isset($_POST["country"])){

$session->set('cont', $_POST["country"]);

}

?>

<div id="dropdown">
<form action="" method="post" name="country"><label for="country"></label>
<select id="country" name="country" onchange="this.form.submit()">


<?php

    $rows = Array('UAE', 'KSA', 'OMAN');

foreach($rows as $row){ 

    if($row == $session->get('cont')){ 

        $isSelected = ' selected="selected"'; 
    } 
    else { 
        $isSelected = ''; 
    } 

    echo "<option ".$isSelected.">".$row."</option>";
    }
?>

</select>
</form>
</div>

and in K2 item page the code is

<?php

                    if($session->get('cont') == "KSA"){

                    echo "SR-".number_format($this->item->ksaprice);

                    }elseif($session->get('cont') == "OMAN"){

                    echo "OMR-".number_format($this->item->omanprice);

                    }else

                    echo "AED-".number_format($this->item->uaeprice);

            ?>

Now the problem is when I select a country, page reloads, the session is created successfully but the price doesn't change as the page needs to be refreshed again to get the session's value.

I need to change the price without refreshing the page again.

1

Can you not use the $_POST['country'] value in your K2 item page rather than the session value?

For example:

                if (isset($_POST['country']) $_POST['country'] == "KSA") {
                    echo "SR-".number_format($this->item->ksaprice);
                } elseif (isset($_POST['country']) && $_POST['country'] == "OMAN"){
                    echo "OMR-".number_format($this->item->omanprice);
                } else {
                    echo "AED-".number_format($this->item->uaeprice);
                }

This can be rewritten a little bit nicer using the Joomla API

$app = JFactory::getApplication();

$country = $app->input->getString('country', '');
if ($country == 'KSA')
{
    echo "SR-" . number_format($this->item->ksaprice);
}
elseif ($country == 'OMAN')
{
    echo "OMR-" . number_format($this->item->omanprice);
}
else 
{
    echo "AED-" . number_format($this->item->uaeprice);
}

UPDATE Okay I thought your module and your k2 item are always on the same page. If you want to keep the selected country you would need to save the country into the session as you currently do and overwrite it everytime the POST value 'country' is set. You could do the same on the K2 item page (ie try to get the value from POST first and if it's empty then from the session).

<?php
defined('_JEXEC') or die;

$app = JFactory::getApplication();
$session = JFactory::getSession();

$country = $app->input->getString('country', '');
if ($country) 
{
    // country variable is set in POST data, overwrite the current session value
    $session->set('cont', $country);
}
else 
{
    // Try to get the country from the session
    $country = $session->get('cont', '');
}

...
<?php
$rows = array('UAE', 'KSA', 'OMAN');

foreach($rows as $row)
{ 
    // Use $country now as it contains either the value from POST, session or nothing
    if ($row == $country)
    { 
        $isSelected = ' selected="selected"'; 
    } 
    else 
    { 
        $isSelected = ''; 
    } 
    echo "<option ". $isSelected . ">" . $row . "</option>";
}
...
?>
  • I have tried it already. It works fine when we stay on the same page. But when we go to other pages of the website, it changes to default country. I want to keep the same country selected. – Agha Jan 20 '15 at 12:18
  • Okay I thought your module and the K2 item are always on the same page. – fruppel Jan 20 '15 at 20:49
  • Updated the answer with a sligthly different approach. I don't think that AJAX is an option here, because you get the data from K2 models (and I don't know how they work internally). – fruppel Jan 20 '15 at 21:16
  • Thanks fruppel, it solved my problem. I called session if post array is not set. – Agha Jan 21 '15 at 11:01
0
  • Firstly, it not recommended to access variables directly with $_POST. Always, JInput or any other sanizitation.
  • Secondly, a module does not have a controller. It cannot easily support a page workflow. You need any back-end component to receive and send information.
  • Finally, to develop your module, without creating a component, you can simply integrate the country query with a com_ajax plugin.

Using Joomla Ajax Interface https://docs.joomla.org/Using_Joomla_Ajax_Interface

Ajax-Session-Module https://github.com/Joomla-Ajax-Interface

Ajax-Session-Plugin https://github.com/Joomla-Ajax-Interface/Ajax-Session-Plugin

0

Fundamentally, it is about the page needing to have the info for whatever country user chooses. There are three ways to do it.

1

Reload the page.

2

Use AJAX to load the correct info when the module javascript reloads.

3

Include all alternatives in hidden form on the page, and use javascript to switch on the one actually used when the module reloads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.