1

I'm having in my front .html or .tpl page following:

<select name="" id="selectBox" class="fancyDrop" onchange="showFunc()">
    <option selected></option>
    <option value="20"><?php echo $this->translate('по 20');?></option>
    <option value="50"><?php echo $this->translate('по 50');?></option>
    <option value="100"><?php echo $this->translate('по 100');?></option>
    <option value="<?php echo $this->totalValue;?>"><?php echo $this->translate('Text');?></option>
</select>
..........................................................................
<script type="text/javascript">
function showFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    window.location.href = "<?php echo $this->url(); ?>?perPage=" + selectedValue;
}
</script>

I'm trying to redirect to current controller and action and also set value perPage and in controller i'm getting it with $perPage = $this->_getParam('perPage', 25);. Above method work but is simple and it shows in user's browser http://website.net/index?perPage=25 for example. Problem is that user can change this value and behaviour of page will also be change.

I've tried to use $this->_forward('route'); but that gives me and redirect loop, also _redirect($url, array $options = array()) i think also creates an redirect loop.

Problem To redirect from front-end .html/.tpl file to certain action/controller/module, currently the same action/module/controller when 1 option is selected and set parameter perPage so that user wouldn't see this value.

doydoy44
  • 5,720
  • 4
  • 29
  • 45
Igor
  • 35
  • 5
  • I think in the function **showFunc()**, `window.location.href = window.location.href+"?perPage=" + selectedValue;` is needed – Nandakumar V Dec 17 '14 at 08:55
  • If use only **js** yes you are right, but generated link also is http://link.link/action?perPage=25. My goal is to achieve http://link.link/action and to retrieve in controller using `$this->_getParam('perPage`)` – Igor Dec 17 '14 at 09:06

1 Answers1

1

I think updating the showFunc() to window.location.href = window.location.href+"?perPage=" + selectedValue; will forward the request to the current page with with perPage as the selected option. But this approac have some issues

  1. When the user again selects the option from the forwarded page the perPage will be appended again. So you have to remove the perPage parameter first. For that you can use any of the methods from these SO posts.How can I delete a query string parameter in JavaScript?

  2. ' so that user wouldn't see this value.' => from what i understood you didnt want the user to see the perPage value. Then you can store the selected value in cookie and from php you can access the perPage value using $_COOKIE['perPage']. The showFunc() will be like

    function showFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    
    var d = new Date();
    d.setTime(d.getTime() + (1*24*60*60*1000));
    document.cookie = "perPage=" + selectedValue + "; " + "expires=" + d.toUTCString();;
    
    window.location.href = window.location.href;
    }
    

    But you wont be able to access it via $this->_getParam('perPage). You have to use $_COOKIE for that.

Community
  • 1
  • 1
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
  • I will try in a moment, it's possible to use *Zend_Http_Cookie*? And no **js** code? – Igor Dec 17 '14 at 09:18
  • I mean using http://framework.zend.com/manual/1.9/en/zend.http.cookies.html and onChange trigger a php function not a JavaScript function – Igor Dec 17 '14 at 09:19
  • You cannot trigger an php function from onChange event, nor any other JS events. – Nandakumar V Dec 17 '14 at 09:21
  • The best you can do is to call an ajax on these events, which seem unnecessary in this scenerio. Is there any reason you didnt want to use JS? – Nandakumar V Dec 17 '14 at 09:22
  • No, also when i chose again value `$_COOKIE['perPage']` changes to 20 if selected to 50 if selected, it doesn't append like you mentioned in first point. According to first point it should be *2050100...*, if i correctly understood what append does. – Igor Dec 17 '14 at 09:24
  • it would be like `link/action?perPage=25?perPage=50?perPage=100`. I haven't checked it but it should be like that. – Nandakumar V Dec 17 '14 at 09:28
  • Actually if you are in page `link/action` and select 25 the new href will be like `link/action?perPage=25`. But if you are in page `link/action?perPage=25` and then select 50 the new href will be `link/action?perPage=25?perPage=50` – Nandakumar V Dec 17 '14 at 09:33
  • Yes you are certainly right. I changed to `window.location.href +"?perPage=" + selectedValue` to see the effect. – Igor Dec 17 '14 at 09:34
  • But if leave like this `window.location.href = window.location.href`, it will affect something? – Igor Dec 17 '14 at 09:37
  • No, but the perPage value will not be sent to server unless you have saved it in a cookie – Nandakumar V Dec 17 '14 at 09:38
  • But isn't is saved every time with document.cookie? Because i tried it now and work perfect. Was bugging with this problem because i wanted to realize a zend redirect with parameters. – Igor Dec 17 '14 at 09:44
  • "Was bugging with this problem because i wanted to realize a zend redirect with parameters." sorry i didnt understood this. – Nandakumar V Dec 17 '14 at 09:48