469,282 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Form querystring manipulation.

6
Hi all,

I'm trying to create a simple navigation script. It allows you to select a region. Once region is selected another list opens up and you can select a city within that region. It all works fine, you can change cities freely and url updates correctly. BUT if you have a city selected and want to re-select the region, the city id stays in the url which messes up my page.

Some examples:
http://192.168.2.16/Seznam.php?IdR=1
http://192.168.2.16/Seznam.php?IdR=1&IdM=101
These two are correct urls, region 1 and city 1 in region 1.

If you now change the region you will get this.
http://192.168.2.16/Seznam.php?IdR=4&IdM=101
Which means region 4 and city 1 in region 1. It should set IdM (city id) to 0 or remove it completely.

Any ideas?

Here's the code I'm using.
Expand|Select|Wrap|Line Numbers
  1.    <form method="get" action="Seznam.php" id="Spremeni">
  2.    <fieldset>
  3.    <select class="Polje" name="IdR" onchange="if(this.options[this.selectedIndex].value != -1){ forms['Spremeni'].submit() }">
  4.    <option value="0">Izberi Regijo</option>
  5.    <?PHP
  6.  
  7.    $Regija = Zavaruj($_GET['IdR']);
  8.    $Mesto = Zavaruj($_GET['IdM']);
  9.    // $Mesto_2 = substr($Mesto,0,1);
  10.  
  11.    $Poizvedba_Regije = mysql_query("SELECT * FROM Regije ORDER BY Id");
  12.  
  13.    while ($Izpis_Regij = mysql_fetch_assoc($Poizvedba_Regije))
  14.    {
  15.       ?>
  16.       <option value="<?PHP echo $Izpis_Regij['Id'] ?>" <?PHP if ($Regija == $Izpis_Regij['Id']) echo "selected=\"selected\""; ?>><?PHP echo $Izpis_Regij['Ime'] ?></option>
  17.       <?PHP
  18.    }
  19.  
  20.    ?>
  21.    </select>
  22.    <?PHP
  23.  
  24.    if (!$Regija)
  25.    {
  26.       echo "</fieldset></form>";
  27.    }
  28.  
  29.    if ($Regija)
  30.    {
  31.       ?>
  32.       <select class="Polje" name="IdM" onchange="if(this.options[this.selectedIndex].value != -1){ forms['Spremeni'].submit() }">
  33.       <option value="0">Izberi Mesto</option>
  34.       <?PHP
  35.  
  36.       $Poizvedba_Mesta = mysql_query("SELECT * FROM Mesta WHERE Id_Regije = '$Regija' ORDER BY Id");
  37.  
  38.       while ($Izpis_Mest = mysql_fetch_assoc($Poizvedba_Mesta))
  39.       {
  40.          ?>
  41.          <option value="<?PHP echo $Izpis_Mest['Id'] ?>" <?PHP if ($Mesto == $Izpis_Mest['Id']) echo "selected=\"selected\""; ?>><?PHP echo $Izpis_Mest['Ime'] ?></option>
  42.          <?PHP
  43.       }
  44.  
  45.       ?>
  46.       </select>
  47.       </fieldset>
  48.       </form>
  49.       <?PHP
  50.    }
  51.  
Sep 25 '09 #1
11 3175
Dormilich
8,651 Expert Mod 8TB
before you submit the form from the first select, simply reset the value of the second select.

you could improve your code further by using AJAX, so that you donít need to reload the page each time.
Sep 25 '09 #2
AnonG
6
If I add a hidden input with name IdM then I get this: IdR=1&IdM=0&IdM=whatever it was before.

I think I don't understand exactly what you mean.

About ajax, I need to refresh page each time because that way all links get new paths. Sort of like categories / subcategories on a webstore page.
Sep 25 '09 #3
Dormilich
8,651 Expert Mod 8TB
@AnonG
a hidden field is completely useless.

@AnonG
well, I kind of understand thatÖ

ok, first you need to understand, how your form works. you have 2 form fields (2x <select> (Iíll refer to it as S1 and S2))
if you call form.submit() (either in S1 or S2) the browser checks every form field and makes a name-value pair for each and transforms that into an URL (in your case).

now the idea is that when you call submit() from S1, before you actually call it, you need to reset the current name-value pair of S2 (i.e. only the value, because the name is not changed)

expressed as pseudo code:
Expand|Select|Wrap|Line Numbers
  1. S1:
  2. S2.value = default;
  3. form.submit();
  4.  
  5. S2:
  6. form.submit();
@AnonG
not a problem for AJAX, merely a matter of you JS skills.
Sep 25 '09 #4
AnonG
6
I have no knowledge of JS. I've been googling for hours now and I didn't find anything useful.

Am I going into the right direction?

Expand|Select|Wrap|Line Numbers
  1. <script type="Javascript">
  2. function reset()
  3. {
  4. document.forms["Spremeni"].elements["IdM"].selectedIndex = 0;
  5. }
  6. </script>
  7.  
I have tried adding this reset() to select fields and to option fields.. nothing worked.
Sep 25 '09 #5
Dormilich
8,651 Expert Mod 8TB
I thought of
Expand|Select|Wrap|Line Numbers
  1. document.forms.IdM.value = 0;
Sep 25 '09 #6
AnonG
6
Were should I put that? I've tried adding it into select onchange"" but then I can't change region. Nothing happens when I click it.
Sep 25 '09 #7
Dormilich
8,651 Expert Mod 8TB
@AnonG
there should nothing visible happen. does the URL be right now?
Sep 25 '09 #8
AnonG
6
I mean, I created a function with what you wrote in it.

Expand|Select|Wrap|Line Numbers
  1. <script type="Javascript">
  2. function reset()
  3. {
  4. document.forms.IdM.value = 0;
  5. }
  6. </script>
  7.  
And then I included it into this.

Expand|Select|Wrap|Line Numbers
  1.  <select class="Polje" name="IdR" onchange="if(this.options[this.selectedIndex].value != -1){ reset() forms['Spremeni'].submit() }">
  2.  
If I add it like that nothing happens when I change regions in the first select form.
Sep 25 '09 #9
Dormilich
8,651 Expert Mod 8TB
can you give me a link to the page, so that i can see for myself?
Sep 25 '09 #10
Dormilich
8,651 Expert Mod 8TB
the interesting point is that it works every second time...
Sep 25 '09 #11
AnonG
6
Yep. Been like that since day one. I kinda need to get it to work every time not every second, hehe. But my lack of JS skills doesn't help at all. :)
Sep 25 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by momo | last post: by
10 posts views Thread by Steve Benson | last post: by
4 posts views Thread by Rob Meade | last post: by
5 posts views Thread by Nirmal Singh | last post: by
2 posts views Thread by Roshawn | last post: by
6 posts views Thread by Carlo | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.