Search code examples
phpsimple-html-dom

How to get value in this select using simple-html-dom?


<select name="size_select" class="long form-control">
<option selected="selected" value="">Please select</option>     
<option value="6 (xxs)" title="6 (XXS)">6 (XXS)</option>    
<option value="8 (xs)" title="8 (XS)">8 (XS)</option>       
<option value="10 (s)" title="10 (S)">10 (S)</option>   
<option value="12 (m)" title="12 (M)">12 (M)</option>   
<option value="14 (l)" title="14 (L)">14 (L)</option>   
<option value="16 (xl)" title="16 (XL)">16 (XL)</option>  
<option value="18 (xxl)" title="18 (XXL)">18 (XXL)</option>     
<option value="20 (xxxl)" title="20 (XXXL)">20 (XXXL)</option>   

How to get all value in this select?

$element =  $html->find('#sizeDdl',0);
foreach($element as $elemen) {
    echo ($elemen->plaintext);
}

I try this output:

Notice: Trying to get property of non-object in /h2/home/website/website.com/test.php on line 11

The idea is that each individual value should be taken and recorded in SQL base. Now is a common result , not every value separately


Solution

  • First add id in select tag as
    <select name="size_select" id="sizeDdl" class="long form-control">
    then change these lines

    $element =  $html->find('#sizeDdl',0);     
    foreach($element as $elemen) {         
        echo ($elemen->plaintext);     
    } 
    

    to these:

    $text_array = array();
    $html = "Your html";     
    foreach($html->find('#sizeDdl') as $element) { 
        $options = $element->find('option'); 
        foreach($options as $element1) { 
            $text_array[] = ($element1->plaintext); 
        } 
    } 
    var_dump($text_array);
    print_r(text_array);