Spaces in Drop-down box

Hi all,

I have trouble with the following drop-down box which contains spaces.
All text gets dropped after the first space.
I would appreciate some advice please

$BankNameLU = mysqli_query($mysqli, "SELECT * FROM LookupList WHERE BankName <> ''");
while($Row = mysqli_fetch_array($BankNameLU)) { 
    $BankNameOptions = $BankNameOptions."<option>$Row[5]</option>"; 
}

And then in the html part

<select name="BankName" <?php echo $BankName;?>>
    <option value=<?php echo $BankName;?> > 
    <selected="selected"> <?php echo $BankName;?> 
    </option>
    <?php echo $BankNameOptions;?>
</select>

Everything can’t be selected for starters.

I don’t get what your code should do - you are assigning a variable $BankNameOptions, which you overwrite again and again and then echo it plain within an HTML options list. You are opening a second select element within the first option that uses a variable $BankName which is never defined. Also the option is never closed.

I’m not seeing valid html for starters. The issue isn’t spaces in the name, that is normal. The issue I see is not a clear path on what it should be doing.

Hi there, thanks for the responds.
I’m very new to web dev so I’m learning with the help of Google :slight_smile:
The code works very well - the first bit is the php which query the LookupList table for the ‘BankName’ field.
The result is then later on used in the HTML part (I named it as such in my original post) to populate the drop-down field named $BankName.

I got the code from a site long ago - can’t remember where and changed it to my needs.
The only problem is that, although I can see the full text in the drop-downlist, once it populates it, it stores only up to the first space. So for now I’ve replaced the spaces with underscores.

I agree that the code is probably not 100% but like I said I know very little and it is not actually my code.
So it there is a better way to handle drop-down boxes I would appreciate it if you could post the php and html part of a better way to do it.
I noticed for instance that the page is very slow if the Lookuplist is big (like 12000 Towns) so it might have something to do with the ‘overwriting’ that is mentioning.

Let me know if I can clarify anything else

What only stores up to the first space? I suspect the problem is how your code is using/displaying the submitted form data. For the html being shown (thank you
astonecipher for the edit), spaces in the label/value shouldn’t cause a problem.

However, the symptom you are describing is usually due to having a value=’…’ attribute, with space(s) in the value without having quotes around the value. In this case, the browser doesn’t know where the value ends and it uses the first white-space/non-printing character as the end of the value.

That reply was because the forum software wasn’t displaying the actual html markup. However, if you are trying to output 12000 options, that would take a long time to be sent to the browser and a long time for the browser to render that much html markup. No human is going to want to see that many choices, especially since they are not being sorted in any particular order in the query. You should have some type of ‘required’ search input to limit the number of option choices.

Ahh - thanks for understanding what I meant.
I fixed the problem by adding a ’ before and after the value so now it includes the spaces (and I could remove the underscores)
Will change the code to pre-populate another field and then base the query to show less of the 12000 dropdown records. For now I’m letting the User type in the Towns instead of selecting them from a huge list.
Thanks for the solution!

Hi all,
I’m revisiting this topic with another question.
It seems like it will not be practice to “divide” the drop-down selection to get fewer results (out of 12000 drop-down options). The result will be less but still very big. Maybe 4000 X 3
So I was wondering if the speed will be faster if I parse a xml file with the drop-down data if the xml file resides in the same online directory as the php file instead of populating the drop-down data from a MySQL table.
If so - how would I have to change my code to use the xml file?
Thanks a lot

If the data has already been parsed and inserted into a database table, the database query will likely be the faster choice. You would need to time both methods to determine which is faster.

The simplexml extension is probably the current best method php has to parse xml data - https://www.php.net/manual/en/book.simplexml.php

Based on the amount of choices, most of the time will be in sending the html markup from the server to the browser, the browser parsing that amount of html, and rending the select/option menu.

Doesn’t this data have some sort of location, region, state, county, city, zipcode, alphabetical name, … that can be used to limit the number of choices?

It is the way I’m getting the data (a list of all Cities in the country) for a Government website as a file download.
The list consists of a list of Cities only, which means I have to add an extra column with postal codes manually for each one - very labour intensive and will not help the End User limit the list as they might not even know the postal code and there will be just as may postal codes.
The best I can do it pre-populate the Province field and then base the Cities on that, but that would bring it down to still a few thousand per group.
At the moment I’m letting the User type it in (the field is just a textbox at the moment and might have ot stay that way it seems

What country is this for?

You can use a auto complete, look it up I don’t feel like giving “example code”, and call this API for all of that… https://rapidapi.com/wirefreethought/api/geodb-cities

South Africa
Will check out Auto Complete and then look for some examples.
The only way a Newbee learn is by changing existing code I think :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service