PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 24th, 2009, 08:46 AM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 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:
<form method="get" action="Seznam.php" id="Spremeni">
      <
fieldset>
      <
select class="Polje" name="IdR" onchange="if(this.options[this.selectedIndex].value != -1){ forms['Spremeni'].submit() }">
      <
option value="">Izberi Regijo</option>
      <?
PHP

      $Regija 
Zavaruj($_GET['IdR']);
      
$Poizvedba_Regije mysql_query("SELECT * FROM Regije ORDER BY Id");

      while (
$Izpis_Regij mysql_fetch_assoc($Poizvedba_Regije))
      {
         
?>
         <option value="<?PHP echo $Izpis_Regij['Id'?><?PHP if ($Regija == $Izpis_Regij['Id']) echo "selected=\"selected\""?>><?PHP echo $Izpis_Regij['Ime'?></option>
         <?PHP
      
}

      
?>
      </select>
      <?PHP

      
if (!$Regija)
      {
         echo 
"</fieldset></form>";
      }

      if (
$Regija)
      {
         
?>
         <select class="Polje" name="IdM" onchange="if(this.options[this.selectedIndex].value != -1){ forms['Spremeni'].submit() }">
         <option value="">Izberi Mesto</option>
         <?PHP

         $Mesto 
Zavaruj($_GET['IdM']);
         
$Poizvedba_Mesta mysql_query("SELECT * FROM Mesta WHERE Id_Regije = '$Regija' ORDER BY Id");

         while (
$Izpis_Mest mysql_fetch_assoc($Poizvedba_Mesta))
         {
            
?>
            <option value="<?PHP echo $Izpis_Mest['Id'?><?PHP if ($Mesto == $Izpis_Mest['Id']) echo "selected=\"selected\""?>><?PHP echo $Izpis_Mest['Ime'?></option>
            <?PHP
         
}

         
?>
         </select>
         </fieldset>
         </form>
         <?PHP
      


Last edited by AnonG : September 25th, 2009 at 04:34 AM.

Reply With Quote
  #2  
Old September 24th, 2009, 12:02 PM
Morrisminor Morrisminor is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 11 Morrisminor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 25 sec
Reputation Power: 0
Cool Try Using

form method = "post" or use $_SERVER['SCRIPT_NAME'] in the URL action which would eliminate the query string if you want to use GET

Reply With Quote
  #3  
Old September 24th, 2009, 12:32 PM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
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.

Reply With Quote
  #4  
Old September 24th, 2009, 01:12 PM
Morrisminor Morrisminor is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 11 Morrisminor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 25 sec
Reputation Power: 0
Cool If you use

$_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.

Reply With Quote
  #5  
Old September 24th, 2009, 01:20 PM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
Sadly that doesn't change anything. Always leaves &IdM=x in the url.

Reply With Quote
  #6  
Old September 25th, 2009, 04:35 AM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
I've updated the question. I hope I'm being more clear now.

Reply With Quote
  #7  
Old September 25th, 2009, 12:15 PM
Morrisminor Morrisminor is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 11 Morrisminor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 25 sec
Reputation Power: 0
Red face New JavaScript Approach

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

Reply With Quote
  #8  
Old September 25th, 2009, 01:18 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 15 m 37 sec
Reputation Power: 5
this is basically what I would do.
PHP Code:
<html>
<
head>
<
script type='text/javascript'>
function 
next(id1id2id3){
    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;
    }
    
document.location "test.php"+queryString;
}
</script>
</head>
<body>
<select id='region' onChange="next('region');">
    <option value=0>Please select one</option>
    <option value='region1' <?php echo (isset($_GET['region'])&&$_GET['region']=='region1')?'selected':''?>>region 1</option>
    <option value='region2' <?php echo (isset($_GET['region'])&&$_GET['region']=='region2')?'selected':''?>>region 2</option>
    <option value='region3' <?php echo (isset($_GET['region'])&&$_GET['region']=='region3')?'selected':''?>>region 3</option>
    <option value='region4' <?php echo (isset($_GET['region'])&&$_GET['region']=='region4')?'selected':''?>>region 4</option>
    <option value='region5' <?php echo (isset($_GET['region'])&&$_GET['region']=='region5')?'selected':''?>>region 5</option>
</select><br />
<select id='subRegion' onChange="next('region','subRegion');">
    <option value=0>Please select one</option>
    <option value='subRegion1' <?php echo (isset($_GET['subRegion'])&&$_GET['subRegion']=='subRegion1')?'selected':''?>>sub region 1</option>
    <option value='subRegion2' <?php echo (isset($_GET['subRegion'])&&$_GET['subRegion']=='subRegion2')?'selected':''?>>sub region 2</option>
    <option value='subRegion3' <?php echo (isset($_GET['subRegion'])&&$_GET['subRegion']=='subRegion3')?'selected':''?>>sub region 3</option>
    <option value='subRegion4' <?php echo (isset($_GET['subRegion'])&&$_GET['subRegion']=='subRegion4')?'selected':''?>>sub region 4</option>
    <option value='subRegion5' <?php echo (isset($_GET['subRegion'])&&$_GET['subRegion']=='subRegion5')?'selected':''?>>sub region 5</option>
</select><br />
<select id='city' onChange="next('region','subRegion','city');">
    <option value=0>Please select one</option>
    <option value='city1' <?php echo (isset($_GET['city'])&&$_GET['city']=='city1')?'selected':''?>>city 1</option>
    <option value='city2' <?php echo (isset($_GET['city'])&&$_GET['city']=='city2')?'selected':''?>>city 2</option>
    <option value='city3' <?php echo (isset($_GET['city'])&&$_GET['city']=='city3')?'selected':''?>>city 3</option>
    <option value='city4' <?php echo (isset($_GET['city'])&&$_GET['city']=='city4')?'selected':''?>>city 4</option>
    <option value='city5' <?php echo (isset($_GET['city'])&&$_GET['city']=='city5')?'selected':''?>>city 5</option>
</select>
<?php
echo '<br /><br />'.$_SERVER['QUERY_STRING'].'<pre>';
print_r($_GET);
echo 
'</pre>';
?>
</body>
</html> 

Reply With Quote
  #9  
Old September 25th, 2009, 09:10 PM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
Quote:
Originally Posted by Morrisminor
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


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!

Reply With Quote
  #10  
Old September 25th, 2009, 09:33 PM
Morrisminor Morrisminor is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 11 Morrisminor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 25 sec
Reputation Power: 0
Cool To Llama

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"';

Reply With Quote
  #11  
Old September 26th, 2009, 09:13 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 15 m 37 sec
Reputation Power: 5
Quote:
Originally Posted by Morrisminor
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"';

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.

Reply With Quote
  #12  
Old October 1st, 2009, 06:04 AM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
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.

Reply With Quote
  #13  
Old October 1st, 2009, 11:55 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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.

Reply With Quote
  #14  
Old November 16th, 2009, 09:58 AM
AnonG AnonG is offline
Contributing User
Click here for more information.
 
Join Date: Jun 2009
Posts: 72 AnonG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 47 sec
Reputation Power: 1
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.

Reply With Quote
  #15  
Old November 16th, 2009, 11:17 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Form + javascript


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek