Jump to content

PHP, HTML and Javascript problem with Select drop down


etymole

Recommended Posts

Hi.

 

Sorry if I'm posting in the wrong forum. My problem spans all three types.

I'm working as an intern at a web development company and am trying to create a select box to contribute to filtering a table by country based on the country selected, but without submitting, so I guess I need a bit of javascript to do this.

 

I have a <select> drop down menu that is pulling information from a spreadsheet (created by the company).

I have that working, but now what I want to do is pull the value of the currently selected item (country) out and place it in a variable so that I can run a compare with all of the current 'user' countries that match the selected country.

 

I apologise if I sound a little confusing. It's really a simple thing I'm trying to do but because I want to do it in real-time I don't want to use a submit and a $Get to get the value passed.

 

I then want to compare the value obtained with an already existing PHP variable(s).

 

I'm afraid I don't have the code as I couldn't take it home.

It looks something like this:

I've used open and closing <?php ?> tags because it makes it easier for me to read instead of using quotation marks and echo's all over the place, sorry if this is bad practice, I hope it's readable.

 

$countries = array(country pulling method) blah blah...
<select id="countries">
foreach($countries as $value) {
<option value="<?php $value ?>" > <?php $value ?></option>
}
</select>

 

Now, to re-iterate, this select box displays all of the countries in the spreadsheet on each line as I want. Can anyone help me get the real-time selected country without the need for a Submit?

 

Appreciate any help

 

Thanks

Link to comment
Share on other sites

You want to use AJAX. There are countless tutorials available and I couldn't explain it adequately in a forum to do it justice.

 

Basically, you will use JS to trigger when a chnage is made to the select list and then "submit" that value using JS to a PHP page. That PHP page can then use the value(s) to run a query, or whatever you want to do, then return results back to the JS calling function. The JS can then do something such as update a table of records based on the currently selected country. All of this happens in real time without having to submit/refresh the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.