Drop Down issue with Spaces

Hello Again :)

I have a script in which you can add your personal information such as state and city. When a person enters a city and state, it populated the drop down menu. There will only ever be one city of the same name in that state.

I have a drop down menu where you can edit these users.

First you choose the state, then the next page takes you to the cities in that state and so on.

Here is my problem…

When adding a city, it is a text field where you type in the name

ex. Chandler

The problem comes along when a persons city is 2 parts…

ex. Queen Creek

In the drop down menu, only Queen was inserted because of the space in the city name. How can i go about fixing this issue, where it some how turns the spaces into the %20 to make it function correctly.

could you please post the code for that part so we can see what’s going on?

Here is the drop down menu im using.

So when i enter a city through a standerd text box, if its more than 2 parts, like my example Queen Creek. It will then only put the first part in the drop down.

The url looks like this then

choosecitytoedit.php?country=US&state=Arizona&city=Queen

It should look like this after i enter the city

choosecitytoedit.php?country=US&state=Arizona&city=Queen%20Creek

Its not a major problem but it is in a way …

When they give me all ov there information a page is generated. It automatticly pulls in there info from the database and puts it onto the page. So i would need the final format to be the one with the space.

[php]

<?php include "upda.php"; ?> <?php include "conn.inc.php"; ?> <? $sql="SELECT abrv FROM countries ORDER BY abrv ASC"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["city"]; $state=$row["state"]; $thing=$row["abrv"]; $options.="".$thing.''; } ?> <?=$options?>

[/php]

I have one other problem related to the drop downs as well that i might as well post so i dont need another topic.

I have a drop down that is pulling a list of pre populated countries. When a user picks the country it then takes them to the choose state page showing only the states in that selected country.

Well that is the part that works, only problem is this,

It pulls every state in the database that is in that country and lists them in the drop down.

SO if Arizona is in The US Arizona will show up on the drop down.

BUT if Arizona is listed 3 times in the database with different cities, it will show Arizona 3 times on the drop down menu.

If thats not fixable to not show duplicates, then i will have to prepopulate the states for them to choose from.

1st problem: If I am understanding the problem, I would look into using str_replace(). You could replace the space with an underscore then before entering it into a database or displaying it str_replace() it again replacing the underscore with a space again.
Also, if you want to check to see if there is a space in the city run some checks. Use strpos() which return false if there is no space. Someone else here may have a better solution to this, but this is the best one I could think of right off the top of my head. For more information on how to use these function - http://www.php.net (You may have knew that, but just incase!) :)

Now for the 2nd question: when you do a select statement you can use DISTINCT. This should give you one of each state.

On your first problem, I’m not sure when the %20 is being introduced (or where), but it’s not too hard to use a str_replace() on it, reverting it back to a space.

On the second problem, I’m not sure what your database looks like, but the most logical thing I could think of is the following:

Table Countries:
- countryID     smallint
- countryName   varchar(255)
- continent     tinyint

Table States:
- stateID     smallint
- stateName   varchar(255)
- countryID   smallint

Table Cities:
- cityID     int
- cityName   varchar(255)
- stateID    smallint

And then link them together:

// Fetching all countries from a continent:
SELECT countryName FROM Countries WHERE continent = 4

// Fetching all states from a country:
SELECT stateName FROM States WHERE countryID = 5

// Fetching all cities from a state:
SELECT cityName FROM Cities WHERE stateID = 6

That way, you won’t have any problem with double values, and can define the dropdown population on a per-page basis:

page1.php    page2.php    page3.php
=========    =========    =========
 choose   ->  choose   ->  choose
 country      state         city

First problem:

Using what you advised would this keep the _ once you have made a query to pull it from the database?

When adding a user (putting there information in city state etc) i want to be able to do it like i normally would.

Name: Bob
State: Arizona
City: Queen Creek

I wouldnt really mind if there was an underscore in it while in the database but once i pull it on to there page, i need it to be just like i enterd it Queen Creek. If what i have just said is what you told me to do then ill try that when i get home.

Problem2:

Solved! Thanks Much eaiser now

The underscore would remain in your city name, but it would of course be a piece of cake to use str_replace() again to convert it back to a space after extracting it from the database :wink:

the %20 isnt in the code. I was merly thinking thats what i would have to do because that is a SPACE (the browser sees it as one).

I will try str replace when i get home :)!

Curious though, on my add user page, where would i put the code for the str_replace() ?

I have several text fields that they need to fill in. Am i adding that to the filed it self or in the INSERT query some where.

Thanks for the fast help guys!

Hmmz, I’d use it to convert any spaces just before your query.
What I find strange is that it does this at all. If you allow someone to input a text through an input text field, and you pick it up in your PHP page through $_POST, it should still have a space, and nothing else. And if you insert it into your MySQL database into a (var)char or text column, the space should also remain (in fact, as many spaces as were entered should remain). Even after extracting them from the DB it should still have a normal space.

What kind of code do you use to input the text, pick up the variables from the form, and insert/select the data from the DB?

Ya, with the text input fields they are just the basic stuff. It inserts the names into the database exactly how they are typed.

SO Yes Queen Creek is enterd into the database like that successfully.

Here is an image of my problem.

These are images from the 3 different pages.

As you can see it shows it fine in the drop down as it was enterd.

Here is the URL before i Hit enter to choose the city.

choosecitytoedit.php?country=US&state=Arizona

Here is the URL after i choose the city

editthecity.php?country=US&state=Arizona&city=Queen

So the drop down seems to be working fine, but when it posts the value to the final page that shows there information based on the city, its messing up and not including the space.

Here is my code that inserts the data enterd into the database.

[php]

<?php } else { $query = "INSERT INTO user_info (city, first, country, last, email, weather, classifieds, cloud, portal, state, sbd_email, sbd_pass, sbd_user, persphone, phone)". // Add the new user info to the database "VALUES ('" . $_POST['city'] . "', " . "'" . $_POST['first'] . "', " . "'" . $_POST['country'] . "', " . "'" . $_POST['last'] . "', ". "'" .$_POST['email'] . "', ". "'" .$_POST['weather'] . "', ". "'" .$_POST['classifieds'] . "', ". "'" .$_POST['cloud'] . "', ". "'" .$_POST['portal'] . "', ". "'" .$_POST['state'] . "', ". "'" .$_POST['sbdemail'] . "', ". "'" .$_POST['sbdpass'] . "', ". "'" .$_POST['sbduser'] . "', ". "'" .$_POST['persphone'] . "', ". "'" .$_POST['phone'] ."');"; $result = mysql_query($query) or die(mysql_error()); ?>

[/php]

And here is the code for the CITY drop down

[php]

<?php include "conn.inc.php"; include "upda.php"; ?> <? $country = $_GET["country"]; $state = $_GET["state"]; $sql="SELECT city FROM user_info WHERE country = '".$country."' AND state='".$state."'"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["city"]; $thing=$row["city"]; $options.="".$thing.''; } ?> <?=$options?>

[/php]

Now I see what the problem is.

Rather than using the URL query, try to pass the country, state and city variables through hidden form inputs:

<input type="hidden" name="country" value="$myCountry">
<input type="hidden" name="city" value="$myCity">
<input type="hidden" name="state" value="$myState">

So on my final Edit page where it shows the information for that city, i would be using POST or GET to obtain the values that are in the hidden fields?

And what would this then look like ?

$options.="<OPTION VALUE=editthecity.php?country=$country&state=$state&city=$thing>".$thing.'</option>';

I assume it will be just

$options.="<OPTION VALUE=editthecity.php>".$thing.'</option>';

you guys have been a great help so far and my project is almost complete!

This replacement is the last fix i need to do and then im done thanks to you guys.

I have tried a few different ways to try and replace the space with a _ and i couldnt manage to get it to work. No errors it just wouldnt happen.

Zyppora’s idea might work with the hidden form input but i wasnt exactly sure what he ment by it.

The code that is populating my drop down menu is here.

[php]

<?php include "conn.inc.php"; include "upda.php"; ?> <? $country = $_GET["country"]; $state = $_GET["state"]; $sql="SELECT city FROM user_info WHERE country = '".$country."' AND state='".$state."'"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["city"]; $thing=$row["city"]; $options.="".$thing.''; } ?> <?=$options?>

[/php]

I think it would be much eaiser if i could get the hidden input way to work.

If i put the hidden fields on this page and pull in the values from the query, what will go on my results page?

$_GET[‘city’] or $_POST[‘city’] or neither.

If you could be as kind to help me out with this small issue i would greatly appreciate it.

Thanks alot

Sorry if I’m being rude here, but this is where your knowledge of PHP is being completely overshadowed by your complete lack of ‘seeing the obvious’ in a programmatical way. You have a long way to go, my friend.

chooseCountry.php:
[php]

<?php $sql = "SELECT * FROM countries ORDER BY countryName"; $result = mysql_query($sql); $options = ""; while ($row = mysql_fetch_array($result)) { $options .= "".$row['countryName'].""; } ?> <? echo $options; ?> [/php]

chooseState.php:
[php]

<?php $country_id = intval($_POST['country_id']); $sql = "SELECT * FROM states WHERE countryID = ".$country_id." ORDER BY stateName"; $result = mysql_query($sql); $options = ""; while ($row = mysql_fetch_array($result)) { $options .= "".$row['stateName'].""; } ?> <? echo ""; ?> <? echo $options; ?> [/php]

chooseCity.php:
[php]

<?php $country_id = intval($_POST['country_id']); $state_id = intval($_POST['state_id']); $sql = "SELECT * FROM cities WHERE stateID = ".$state_id." ORDER BY cityName"; $result = mysql_query($sql); $options = ""; while ($row = mysql_fetch_array($result)) { $options .= "".$row['cityName'].""; } ?> <?php echo ""; echo ""; ?> <? echo $options; ?> [/php]

I’m not giving any guarantees on these, but if you look at these longer than 5 seconds you should be able to comprehend what’s going on and solve any further issues accordingly. Good luck on finalizing your project :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service