Welcome Guest, Not a member yet? Register   Sign In
refresh page with dropdown
#1

[eluser]tfncruz[/eluser]
Hi!
I have a main menu inside a drop-down field.
Anybody knows how to refresh the page every time the user selects a new menu item?

Thanks!
#2

[eluser]Rick Jolly[/eluser]
Code:
<form name="menu_form" action="your_controller/your_method" method="post">
<select name="menu">
...
</select>
&lt;/form&gt;
Bah, the forum keeps removing the javascript. In the select tag you need to write onChange = "document.menu_form.submit()"
#3

[eluser]tfncruz[/eluser]
Sorry, I'm not getting it...

I have the following function:
Code:
private function getGramMenu() {

//open the form (Here I have to concat the selected option next to the controller's function)
$data = form_open('main/view/'.$selection.'/1');

//get the data to fill the dropdown
$result = $this->gramaticas_model->getGramNomes();
foreach($result as $row) $options[$row->id] = $row->titulo;

$data .= form_dropdown('Gramáticas', $options);

$data .= form_close();
    
return $data;
}

Now... every time the user selects a new item, I want to refresh the page depending on the selection chosen. Could you explain how I may accomplish this?
#4

[eluser]Rick Jolly[/eluser]
See the additions below:

[quote author="tfncruz" date="1219534614"]Sorry, I'm not getting it...

I have the following function:
Code:
private function getGramMenu() {

//open the form (Here I have to concat the selected option next to the controller's function)
$attributes = array('name' => 'menu_form');
$data = form_open('main/view/'.$selection.'/1', $attributes);

//get the data to fill the dropdown
$result = $this->gramaticas_model->getGramNomes();
foreach($result as $row) $options[$row->id] = $row->titulo;

$selected_option = (empty($_POST['Gramáticas'])) ? '' : $_POST['Gramáticas'];
$js = 'onChange="document.menu_form.submit();"';
$data .= form_dropdown('Gramáticas', $options, $selected_option, $js);

$data .= form_close();
    
return $data;
}

Now... every time the user selects a new item, I want to refresh the page depending on the selection chosen. Could you explain how I may accomplish this?[/quote]
Make sure to filter/validate the $_POST variable.

Edit: I now see that you have a $selection variable for your form action. I was assuming that you'd always submit to the same page and load content based on the menu. However, you can change the form action dynamically as well. Instead of calling "document.menu_form.submit(); in the select tag, call a javascript function that changes the form action before it submits.
#5

[eluser]tfncruz[/eluser]
Thanks Rick!
I tried that and the browser refreshes every time I make a new selection.
But the following errors are occurring:
First, "Undefined variable: selection". I think I can initialize this var with the third uri segment.
Then, when I select a option and the browser refreshes, it returns the following error "Disallowed Key Characters". I already tried to remove the accents from "Gramática" but that doesn't solve the issue...

Suggestions...?
#6

[eluser]tfncruz[/eluser]
Solved!
Thank you very much Rick!

The solution to refresh the page is in the js. Then the solution was to point the form to a function that redirects to the view function with the required parameters.

here's the code:
Code:
private function getGramMenu($gramID) {
        
    //open the form
    $attributes = array('name' => 'menu_form');
    $data = form_open('main', $attributes);

    //get the data to fill the dropdown
    $result = $this->gramaticas_model->getGramNomes();
    foreach($result as $row) $options[$row->id] = $row->titulo;

    $selected_option = $gramID;
        
    $js = 'onChange="document.menu_form.submit();"';
    $data .= form_dropdown('gramaticas', $options, $selected_option, $js);

    $data .= form_close();

    return $data;
}

Now I only have to check, if $this->input->post('gramaticas') is set, redirect to 'main/view/lalala'. If not, redirect lalala.

Thank you again!




Theme © iAndrew 2016 - Forum software by © MyBB