• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to retain value of select options, after page reload

#1
Photo 
hi, 
i have three <select options> , second and third are inside a form tag. when first one change , based on that second  changes.
when i press the submit button, according to the selected values of first and second , it displays the output in to third option.

when page refresh after submit button press, at location.reload, i want to retain the already selected value of first <select option> , but new values for second and third based on the query return values. i have attached screen shot of my source code herewith. how to do that ? please any one can help me?

//HTML CODE
<form action="#" id="form_user_roll">
<div class="form-group">
    <div class="col-md-10">
              <label for="username" class="control-label col-md-6">Select a User to Assign/Remove Role</label>                           
                <select class="form-control" name = "sysuser" id = "sysuser">
                  <option value="-1">--- SELECT USER ---</option>
                  <?php foreach ($information_valid_user as $row) {?>
                  <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
                  <?php } ?> 
                </select>                 
                  <span class="help-block"></span>
              </div>
            </div>
 <div class="form-group">
                     <div class="col-md-2">
                     </div>
 </div>
            <div class="form-group">
                <div class="col-md-4">
              <label for="assignrole" class="control-label col-md-6">Assigned Role</label>                         
                <select class="form-control" name = 'assignrole' id = 'assignrole' size="10"  style="width:275px">                 
                </select>                                
                  <span class="help-block"></span>
              </div>
            </div>
                    <div class="form-group">
                     <div class="col-md-2">
                         <button type="button" class="btn-danger btn-sm" id="btn_remove_roll" onClick="remove_roll()">>>REMOVE</button>
                         <button type="button" class="btn-success btn-sm" id="btn_add_roll" onClick="add_roll()"><<  ADD </button>             
                    </div>
                    </div>
            <div class="form-group">
                 <div class="col-md-4">
              <label for="all_role" class="control-label col-md-6">All Roles</label>                             
                  <select class="form-control" name = 'all_role' id = 'all_role' size="10"  style="width:275px">
                  <?php foreach ($info_all_roll as $row) {?>
                  <option value="<?php echo $row->role_id; ?>"><?php echo $row->role_name; ?></option>
                  <?php } ?> 
                </select>             
                  <span class="help-block"></span>
              </div>
            </div>   
</form>
Reply

#2
I dont understand what you mean with "after submit button press, at location.reload, "

As soon as you submit the Form you can retrieve all the form values in your controller with
$something = $this->input->post('something');
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#3
(07-13-2017, 08:36 AM)rtenny Wrote: I dont understand what you mean with "after submit button press, at location.reload, "

As soon as you submit the Form you can retrieve all the form values in your controller with
$something = $this->input->post('something');

when i select a user, then the all relevant assigned role for that user will display.
After that i have to select one of the role, which i wish to assign for that user from 'All Roles' and  press the ADD button.
Then it add that role to the selected user. 

After adding when the page reload, i need to have the selected user in the first drop down as it is and need to list all assigned role with the newly added role

But what happen is first drop down doesnot retain the selected user. it again ask to '--- SELECT USER ---' with empty value of 'Assigned Role'

I need to have get the list of assign role with newly added assign role with the selected user in the first select dropdown.
pls help me to this 
Reply

#4
In this part:
Code:
<select class="form-control" name = "sysuser" id = "sysuser">
 <option value="-1">--- SELECT USER ---</option>
 <?php foreach ($information_valid_user as $row) {?>
   <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
  <?php } ?>
</select>  
you need to add the "selected" attribute to the option that contains the current user (after it has been selected).

E.g.:
Code:
<?php foreach ($information_valid_user as $row) : ?>

   <?php  $sel = $row->id == $current_user ? 'selected' : NULL;  ?>
   <option value="<?= $row->id; ?>" <?= $sel; ?> ><?= $row->name; ?></option>

<?php endforeach; ?>

$current_user must be the id of the previously selected user, passed by the controller.
Reply

#5
Sad 
(07-14-2017, 12:13 AM)Wouter60 Wrote: In this part:
Code:
<select class="form-control" name = "sysuser" id = "sysuser">
 <option value="-1">--- SELECT USER ---</option>
 <?php foreach ($information_valid_user as $row) {?>
   <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
  <?php } ?>
</select>  
you need to add the "selected" attribute to the option that contains the current user (after it has been selected).

E.g.:
Code:
<?php foreach ($information_valid_user as $row) : ?>

   <?php  $sel = $row->id == $current_user ? 'selected' : NULL;  ?>
   <option value="<?= $row->id; ?>" <?= $sel; ?> ><?= $row->name; ?></option>

<?php endforeach; ?>

$current_user must be the id of the previously selected user, passed by the controller.

that is not working for me. Sad
after i ADD/REMOVE roll to/from user, when page load, i can't see the selected user name in the first drop down menu. and newly updated assign roles for that user.
my output image, after i ADD/REMOVE roll to/from user,  attached below. pls help me.   Huh  Confused
what is the method to echo the newly updated assign rolls with the selected user name after i ADD a new roll  to that user.
Reply

#6
Where is the submit button?
If there is none, and you reload the page, no form data is being submitted to the controller.
But on reload, the controller method runs again, building your form as a new request, without remembering any user input.

You are using AJAX. With that, you can update the web browser's page without reloading it. So skip the reloading part and you will be fine.
Reply

#7
(07-14-2017, 08:56 AM)Wouter60 Wrote: Where is the submit button?
If there is none, and you reload the page, no form data is being submitted to the controller.
But on reload, the controller method runs again, building your form as a new request, without remembering any user input.

You are using AJAX. With that, you can update the web browser's page without reloading it. So skip the reloading part and you will be fine.
these are the two buttons  
 
 <button type="button" class="btn-danger btn-sm" id="btn_remove_roll" onClick="remove_roll()">>>REMOVE</button>
 <button type="button" class="btn-success btn-sm" id="btn_add_roll" onClick="add_roll()"><<  ADD </button>  

when i press remove button, it remove the selected roll from the selected user. its ok. same time i need to see the new rolls after removing the selected roll for that selected user. is there any method to do this ?
according to method i used, i can see only two select option without any selected values for user and assign roll.  i want to keep the selected user with its newly updated assign roles.
Reply

#8
The way I see it, the RemoveRoll AJAX function is calling a php function in the controller to remove the roll. After that, the AJAX function appends the removed roll to the list of available rows (on the right). So far so good. But after that, it's reloading the page. All the effort you are using AJAX for, has been for nothing. You might just as well turn the two buttons into real submit buttons, perform the Add or Remove roll actions in the controller after POSTING the form, and then repopulate the page to see the result.

But it's the power of AJAX that you can do all that WITHOUT reloading the page! You really are on the right track.

Instead of reloading, you should remove the selected roll from the list on the left side (in your AJAX function).
Opposite to that, if you Add a roll, make your AJAX function add the <option> to the list of selected rolls (on the left side) and also remove it from the list of available rows (on the right).
This way, nothing happens with the users list. So the selected user will remain visible all the time.
Reply

#9
(07-15-2017, 01:43 AM)Wouter60 Wrote: The way I see it, the RemoveRoll AJAX function is calling a php function in the controller to remove the roll. After that, the AJAX function appends the removed roll to the list of available rows (on the right). So far so good. But after that, it's reloading the page. All the effort you are using AJAX for, has been for nothing. You might just as well turn the two buttons into real submit buttons, perform the Add or Remove roll actions in the controller after POSTING the form, and then repopulate the page to see the result.

But it's the power of AJAX that you can do all that WITHOUT reloading the page! You really are on the right track.

Instead of reloading, you should remove the selected roll from the list on the left side (in your AJAX function).
Opposite to that, if you Add a roll, make your AJAX function add the <option> to the list of selected rolls (on the left side) and also remove it from the list of available rows (on the right).
This way, nothing happens with the users list. So the selected user will remain visible all the time.

Thank you very much for your guidance. Now it works Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.