0

I am processing files that contain HTML markup.

I need to get the selected option from the drop down box. In this case Australia is the selected option ..

  <select name="cCountry" id="cCountry" style="width:200" tabindex="5">

  <option value="251">Ascension Island</option>
  <option selected="selected" value="14">Australia</option>
  <option value="13">Austria</option>

Another scenario :

    <select name="cBirthYearM" id="cBirthYearM">
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option selected="selected" value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>

In this case '1977" is the value that I need to extract as it's the selected option . To make it clear I need to get the value from the markup not from user GET/POST input

3
  • 1
    why using regexp instead of DOMDocument? Commented Jun 27, 2011 at 11:02
  • Your question doesn't make it clear if you mean getting the value the user submits in the form, or getting the value out of the markup. Please clarify. Commented Jun 27, 2011 at 11:12
  • @GordonM The value is already selected . Double check and see Australia is "option selected="selected" Commented Jun 27, 2011 at 12:10

2 Answers 2

3

The value specified in the value part of the option will be in $_GET ['cCountry'] or $_POST ['cCountry'] (depending if you're using GET or POST to submit the form).

It should be simple matter to look up the value from a lookup table or DB query.

EDIT: This question didn't make it clear if you meant how to get the value the user selected when the form is submitted, or how to get the value from the markup. If it's the latter, then you should look at DOMDocument.

Sign up to request clarification or add additional context in comments.

4 Comments

hm... and what if user will change his selection?
The value is already selected . Double check and see Australia is "option selected="selected" . I'm trying to get the selections from different files which have different selection . Is more a data extractor /crawler than web application for users use
@bazmegakapa I'm not using neither POST or GET request . I'm crawling the files and I need to extract the selected value . In the above case "Australia" is selected.
@k102 it's an offline process . I crawl the html pages so there won't be any user action
1

If you wish to parse HTML in PHP then the preferred method is the DOM hierarchy of objects, especially DOMDocument. You can use Javascript-like methods of accessing the DOM tree such as getElementById to grab the select control (in this case getElementById ('cCountry')) and then examine its children (the options) to find the selected one and get its attributes.

It can take a little getting used to using the DOM objects but they're far more powerful for parsing and manipulating HTML than regex would be, so they're well worth learning.

1 Comment

+1 for HTML parsing DOM is the way to go, using regexes here will only kill cute animals, destroy the happiness on Earth and create chaos

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.