0

I have a html block like this :

$localurl = '
<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>
 ';

I'm trying to extract the selected value in this case "Australia" using simple_html_dom ( http://simplehtmldom.sourceforge.net/ ). So far I have build a function but is not working :

//extract the selected value

function getValue_selected($value, $localurl)
{
  $html = file_get_html($localurl);
  $i = 0;
   foreach ($html->find('select[option selected="selected"]') as $k => $v) {
     if ($v->name == $value) {
   $shows[$i]['Location'] = $v->value;
   }

   }
$value = $shows[$i]['Location'];
$html->clear();
unset($html);
return $value;
}

  $selected_value = getValue_selected('cCountry', $localurl)

An alternative such QueryPath would be accepted too .

2
  • Suggested third party alternatives to SimpleHtmlDom that actually use DOM instead of String Parsing: phpQuery, Zend_Dom, QueryPath and FluentDom. Commented Jun 26, 2011 at 13:13
  • @Gordon How can I get it with QueryPath ? Commented Jun 26, 2011 at 13:21

2 Answers 2

2

the right answer is:

$html->find('#cCountry',0)->find('option[selected=selected]',0);
Sign up to request clarification or add additional context in comments.

Comments

1

My guess is that you're trying to access $shows when it is defined outside of the function. If this is the problem, you either need to put global $shows; at the top of the func, or, better still, modify the signature to pass it in. Something like:

getValue_selected($value, $localurl, &$shows)
{/* your function here */ }

getValue_selected($val1, $val2, $shows);

2 Comments

Originally I made the function to get hidden inputs : foreach ($html->find('input[type="hidden"]') as $k => $v) { . I could set the input name as $value . (e.g if there is <INPUT TYPE=HIDDEN NAME="postingID" value="98765"> I call the function getValue_selected('postingID', $localurl) which retrieves the value "98765" from the html content ) . Now I've tried to modify it to make it work on "select" input types as you can see above but doesn't seem to work .
I still say you're trying to access a variable which is undefined in scope. Have you tried adding a variable $select = array(); to the inside of the function?

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.