-1

I'm trying to send multiple values in my drop down menu contained within a form. So basically, I want the form to return database for both Scotland and England when the user selects 'United Kingdom' in the drop down. I've tried the following (and some variations) but it's just returning values from Scotland

<div class="form-group">

<select class="form-control" name="nationality">

....

<option value="Scotland"|"England">United Kingdom</option>

<option value="United States" >United States</option>

<option value="Uruguay" >Uruguay</option>

<option value="Uzbekistan" >Uzbekistan</option>

...

I did look at this post - Can an Option in a Select tag carry multiple values?

But I couldn't get the number array to work - perhaps because I'm not passing an number through. And in the solution to this post - the second answer where an object is passed - I wasn't sure where to apply the JSON? Yup - I'm struggling with the basics here.

Adam

Hi all. Thanks for the comments. Just to update this post and explain things a bit more clearly.

I'm not trying to add information to the database - I'm trying to pull information from the database.

The application is a teacher search box. Users can search for teachers in an existing table under the parameter 'nationality'. Teachers already exist in the table and the UK teacher nationalities are set as Scotland, England, Wales, NI. I could change all of these in the database to UK however I wish them to remain as the UK forming nations.

I would like 'The UK' to appear in the search form drop down list and this would return all teachers from all UK countries.

Adam
  • 3
  • 4
  • ^^and Northern Ireland (for UK) – treyBake Nov 22 '18 at 15:03
  • 2
    you should have value as value="Scotland,England" – treyBake Nov 22 '18 at 15:04
  • also, it's much better to just use IDs – treyBake Nov 22 '18 at 15:04
  • add the attribute `multiple` to your select or if it's just two values on the same option (can't be done as you can only use one value per option), so separate them with a delimiter and put that into the value and then split the value once in the backend – Pete Nov 22 '18 at 15:06
  • We need to see the PHP. – Funk Forty Niner Nov 22 '18 at 15:06
  • 1
    If you want multiple choices, you need to add `multiple` and then use an array for it. – Funk Forty Niner Nov 22 '18 at 15:07
  • 1
    `` does not work.. why dont you make user to select multiple options? – Shiva Nov 22 '18 at 15:09
  • I would do it like this answer: https://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values#answer-50224019 but add a check to see if the delimiter existed first. Although if you are going to separate out the countries, why don't you give them the option of each country - for example if I chose UK and you stored me living in scotland - that is incorrect - either store it as UK or give them the option to choose the country seperately – Pete Nov 22 '18 at 15:10
  • Thanks @treyBake. I tried 'value="Scotland,England"' but this just returns no database match. – Adam Nov 22 '18 at 16:45
  • Thanks @Pete. I will look into adding the delimiter again. My attempt was clearly off. I know what you mean about choosing between UK or the nation countries but in this situation I feel that a lot of the teachers identify with their nation country but potential students from outside Europe are less bothered about from where in the UK the teacher comes from. Also, ideally, students could search for teachers from Scotland specifically, but also teachers from the UK which would include Scottish teachers. – Adam Nov 22 '18 at 16:51
  • Yeah but how are you going to know they are from Scotland specifically, if they have just chosen UK, you then insert the 2 values which will then give false data if they only come from one place – Pete Nov 22 '18 at 16:53
  • Hi @Pete. In the teacher profile there would also be a notification that the user is from Scotland, England etc. but these teachers would show up in a search for UK teachers. – Adam Nov 22 '18 at 17:05

1 Answers1

-2

It’s not clear from your question alone what you intend to do with the “nationality” value later on, but you could create multiple values out of the single “nationality” value on submission.

if ($_POST[“nationality”] == “United Kingdom”) {
    $value1 = “Scotland”;
    $value2 = “England”;
}

Seems messy though. There is a multiple attribute you can give to your select element that will allow for multiple values.

https://www.w3schools.com/tags/att_select_multiple.asp

Nick Dawes
  • 2,089
  • 1
  • 13
  • 20
  • 1
    Your answer's syntax will throw parse errors. The curly quotes `“`, need to be regular quotes `"`. – Funk Forty Niner Nov 22 '18 at 15:19
  • Thanks @Nick Dawes. I thought to avoid the multiple attribute as I don't really want the person searching to have to select multiple elements. I just want UK to be a stand alone option which returns rows with any four value attributes for nationality (i.e. Scotland, England...) (if that makes sense) – Adam Nov 22 '18 at 16:55
  • Hi @Nick Dawes. With your post code above, what would be the option value i.e. - Would it be: – Adam Nov 22 '18 at 17:01
  • If using the code suggested, the option value should be “United Kingdom”. Your select element should have a name attribute (nationality). You use this name to access the value of the input in the script your form is pointing to in it’s “action” attribute. So if your select is named “nationality” and you choose “United Kingdom” and then submit the form, the value of that input (“United Kingdom”) can be accessed via $_POST[“nationality”]. The value of this variable will be whatever you selected (United Kingdom, Spain, Germany etc) from the drop down in the form. Make sense? – Nick Dawes Nov 22 '18 at 17:21
  • @Nick Dawes - thanks. So it's in the php script, - the sql statement, where my form points to do I would need to have some code that pulls info from the fout UK countries if United Kingdom is selected? – Adam Nov 22 '18 at 17:37
  • Yup, that’s right. The if statement in my answer could be placed in that script and if United Kingdom is selected, you can handle it from there. Not how I’d recommend doing it, but I understand your happy as long as it works and you understand it. If you need more help with the second part, let me know. – Nick Dawes Nov 22 '18 at 17:54
  • @Nick Dawes - thanks. Yes, I'll be happy enough if it works. I'll try it tomorrow and I fully expect I'll get stuck and need your help so thanks for offering in advance. – Adam Nov 22 '18 at 19:23