|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Form - manipulating querystring.
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. PHP Code:
Last edited by AnonG : September 25th, 2009 at 04:34 AM. |
|
#2
|
|||
|
|||
|
form method = "post" or use $_SERVER['SCRIPT_NAME'] in the URL action which would eliminate the query string if you want to use GET
|
|
#3
|
|||
|
|||
|
If I change "get" to "post" url stays the same all the time.
You meant like this right? Code:
<form method="get" action="<?PHP $_SERVER['SCRIPT_NAME']?>" id="Spremeni"> It's does the same thing. IdR (Region Id) changes and IdM = (City Id) stays the same / doesn't go away. |
|
#4
|
|||
|
|||
|
$_SERVER['PHP_SELF'] in the action, you will get the address of the page that it is on. If you use $_SERVER['REQUEST_URI'] instead you will get the query string too. So if you don't want the query string use action="<?=$_SERVER['PHP_SELF'];?>" else use action="<?=$_SERVER['REQUEST_URI'];?>" if you want the query string.
|
|
#5
|
|||
|
|||
|
Sadly that doesn't change anything. Always leaves &IdM=x in the url.
|
|
#6
|
|||
|
|||
|
I've updated the question. I hope I'm being more clear now.
|
|
#7
|
|||
|
|||
|
I am trying a new approach. Since you have the form submitting everytime you change either select box, on the Region select change the onchange event to this:
<select class="Polje" name="IdR" onchange="document.Spremeni.IdM.value=''; if(this.options[this.selectedIndex].value != -1){ forms['Spremeni'].submit() }"> So when the region is changed it will set the City box back to first option which is the blank, or <option value="">Izberi Mesto</option> one |
|
#8
|
|||
|
|||
|
this is basically what I would do.
PHP Code:
|
|
#9
|
|||
|
|||
|
Quote:
If I change select statement to what you wrote nothing happens when I click on region names. It all stays the same. Page doesn't refresh at all. IAmALlama I'll try your solution in the morning. Time to sleep! |
|
#10
|
|||
|
|||
|
if($_GET['subRegion'])&&$_GET['subRegion']=='subRegion1')
that is duplicative: if $_GET['subRegion']=='subRegion1' then $_GET['subRegion'] has to be set. Better to extract the $_GET's so you can just use if($subRegion =='subRegion1') echo 'selected="selected"'; |
|
#11
|
|||
|
|||
|
Quote:
No, that is not duplicative, they both have separate meanings. if error reporting is set to E_ALL, leaving off the isset() part will throw an E_NOTICE error. When I program, I write the code as if error_reporting is E_ALL (like all my servers are) so that they will run on any server regardless of the settings. Generally if I download a script and try to run it and see a bunch of errors, even notice errors, I just delete it. If the programmer isn't taking the time to program it right, I don't feel it is worth the time to fix. And using extract on $_GET is considered REALLY bad. you will overwrite any previously set variables (at least without the overwrite flag which can be easy to miss). If that is what you are going to do, you might as well leave register globals on, its just as bad. |
|
#12
|
|||
|
|||
|
I apologize for the late reply. I took a week off to relax a bit. I'm back now and trying to implement your script. It works perfectly.
There's 1 small bug. queryString = queryString+"&"+id1+"="+ele.value; Just needed to remove that & and it works great. Edit: So that you don't get test.php?&x=y And changed "onChange" to "onchange" so that my site gets parsed correctly as xhtml 1.0 strict. Thanks alot! Last edited by AnonG : October 1st, 2009 at 06:16 AM. |
|
#13
|
|||
|
|||
|
I left the & on the first part because on my server it still worked with the & on the first part and it also meant that the script could use pretty much the same code for all 3 options. but whatever works for you is just fine.
|
|
#14
|
|||
|
|||
|
Sorry for replying to an old thread, but I stumpled upon a erorr.
I've been renaming .php files on my server to easily maintain my code and when I changed "xy.php" to "x_y.php" the JS stopd working. I changed the document.location to the new filename, but it just puts ? behind filename in the web address bar. If I change back to the name without "_" in then it works fine. Code:
function x_y(id1, id2, id3, id4)
{
var queryString = "?";
var ele = document.getElementById(id1);
if(ele && ele.value != 0)
{
queryString = queryString+""+id1+"="+ele.value;
}
ele = document.getElementById(id2);
if(ele && ele.value != 0)
{
queryString = queryString+"&"+id2+"="+ele.value;
}
ele = document.getElementById(id3);
if(ele && ele.value != 0)
{
queryString = queryString+"&"+id3+"="+ele.value;
}
ele = document.getElementById(id4);
if(ele && ele.value != 0)
{
queryString = queryString+"&"+id4+"="+ele.value;
}
document.location = "x_y.php"+queryString;
}
Any ideas why filenames can't contain "_" in JS? Or am I missing something completely obvious? Thanks for the help. |
|
#15
|
|||
|
|||
|
try putting the url with the filename into a variable and alert() it out to make sure there is nothing wrong there. AFAIK there is nothing wrong with using an underscore in a file name.
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Form + javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|