1

I have a drop-down list on a PHP/HTML page. I have a button beside it which when clicked opens a pop-up window via which I can add more items to the drop down list (it gets stored in a mysql table). When I close the pop-up window it should trigger an event which in turn should refresh the drop-down list on parent page.

How to do it? Thanks!

Edit: There are 2 drop-down lists on the page. Options list in 2nd DDL depend on the selection made in 1st DDL. Beside 2nd DDL, there is an button which opens a pop-up window. Using the PPW one can add options to 2nd DDL (stored in mysql table).

Now the pop-up window option is working perfectly, data is getting saved in the table. Right now only option for me is to refresh the page. I want an event to occur when close button on PPW is clicked. This event should refresh the 1st DDL in such a way that option selected previously remains selected. Can it also do this: data added by me should be selected in 2nd DDL.

Code

1st DDL

<td>State Name</td> 
<td><select id="a" name="a" onchange="ajax(this.value);">
<option value="">Select State Name</option>
<?php
$sql = "SELECT * FROM State";
$abc = mysql_query($sql);   
while($row = mysql_fetch_array($abc)){ 
echo "<option value=\"".$row['scode']."\">".$row['State']."</option>\n";}
?>
</select></td>

2nd DDL

<td>School Name</td>
<td><select name="schname">
<option value="">Select School Name</option></select>
<A href="addsch.php?State=<?php echo $state?>&scode=<?php echo $scode?>"Target='_blank'>Add New School Name</a></td>

The ajax function named 'ab' does the refresh when 1st DDL is selected.

addsch.php opens a form in a pop-up window with a submit button which saves the form data to mysql table.

Example: I select 'State1' from 1st DDL, school names in for 'State1' are displayed in 2nd DDL. I want to add one school name (say 'State2' to 2nd DDL, so I use the add option. After saving the form data, I want 1st DDL to refresh in such a way that 'State1' remains there and 2nd DDL shows 'State2'. Otherwise atleast 1st DDL is refreshed then the user can select 'State2' from 2nd DDL himself.

How to do this?

Krrish
  • 13
  • 7

1 Answers1

1

If it is stored into a MYSQL table there is no way for the system to refresh your dropdown list without actually refreshing the page. If I've understood everything correctly this might help you :

Open popup and refresh parent page on close popup

If you do not wish to refresh the page but only your dropdown list then you'll have to make an AJAX function yourself.

Edit: By the look of it you'd better be off refreshing the whole page and doing a javascript function which would store the value from the option input into a session array and everytime you would load this page it would look for this value. And if the key isn't set then the first option in your first list would be selected.

Take a quick look at this js fiddle :

https://jsfiddle.net/98k456ho/1/

Needs jQuery

Unfortunately it doesnt work as it uses sessionStorage, but you can paste this in your php file and it should work.

sessionStorage is similar to localStorage is allows you to store key/value "object" which can be accessed pretty quickly. It allows you to store more data compared to a simple cookie.

What this code does is that everytime you choose an option from your list, the value is stored into a session array. Everytime the page is loaded it will look for the value into the session and if the first value was selected or not a single value was selected by the user it will set the default value to "Select".

In your case if the user selects for example "California" he gets access to all the options in your second list. After closing your popup since everything is stored into the session everything will be the same.

One important thing to note, is that the javascript needs to run before you even do your mysql query else the value in your first dropdown will be selected but you'll get no or wrong values into your second list.

Community
  • 1
  • 1
Croisciento
  • 149
  • 1
  • 12
  • Yes, refresh only the dropdown list. Can't I theoratically use a onclick event for the pop-up window which should refresh only dropdown on the parent page? – Krrish Aug 06 '15 at 09:38
  • Yes of course. But as you said the data from your list are stored into a database and the only way to refresh the values from your list according to what's been inserted into your table is to either refresh the whole parent page or to make a get/post request via ajax so you're only refreshing your list. Let's say the values from your list comes from an array. An ajax request will returns the new values which will then replace the one you have into your first array. – Croisciento Aug 06 '15 at 09:43
  • Thanks, but there are other elements (drop-down, textbox etc) prior to this DDLs which a user will access. – Krrish Aug 06 '15 at 12:59
  • Then you can adapt your code so everything is filled depending on what the user is selecting and what he selected before the refresh. That's the same thought process. No matter which way you'll go (ajax or not) as you want to keep trace of the users choice you'll have to store its choice somewhere and "adapt" your refresh on it. – Croisciento Aug 06 '15 at 13:06
  • Ok, now I am stuck somewhere else. I am temporarily storing the form variables using $_SESSION['xyz']=$_POST['xyz']; and when "Add school name" button is clicked a new windon containing the form. After submitting the form the parent page is automatically reloaded. I am also displaying the session variables in the input boxes using value="" in the input tag. But when the page is refreshed, values are not retained. What I doing wrong? – Krrish Aug 10 '15 at 10:40