0

I'm trying to display multiple values in every option tag. I read some questions and answer here with the same issue I have, like this. But it's not working on my side. The data of option tag will dynamically created with mysql.

How to fix this? Thanks in advance!

E.g.

<option value="data 1,data 2, data 3....">Text 1</option>
<option value="data 6,data 7, data 8....">Text 2</option>

My current form:

$results_streets = $wpdb->get_results('SELECT DISTINCT streets,stores FROM table WHERE streets IN ("Street 1") ORDER BY streets ASC', OBJECT);

<form action="" name="formName">
<div>
<select>
    <option name="default" class="filter_by" value="Select by Street">Select by Street</option>
    <?php
    foreach($results_streets as $option){
        if(isset($_POST['streets_list']) && $_POST['streets_list'] == $option->streets)
            echo '<option name="streets_list" class="filter_by" selected value="'. $option->stores .'">'. $option->streets .'</option>';
     };
    ?>
</select>
</div>
<input type="submit" value="Submit"/>
</form>
Community
  • 1
  • 1
Elsk
  • 41
  • 7

1 Answers1

0

I think the attribute for the option tag isn't values. Try instead value, so like this:

<select name="test">
    <option value="data 1,data 2, data 3....">Text 1</option>
    <option value="data 6,data 7, data 8....">Text 2</option>
</select>

UPDATE:

$results_streets = $wpdb->get_results('SELECT DISTINCT streets,stores FROM table WHERE streets IN ("Street 1") ORDER BY streets ASC', OBJECT);

<form action="" name="formName">
<div>
<select name='streets_list'>
    <option class="filter_by" value="Select by Street">Select by Street</option>
    <?php
    $values = "";
    foreach($results_streets as $option){
        if(isset($_POST['streets_list']) && $_POST['streets_list'] == $option->streets){
            $values.=$option->stores."|";
       } 
     }
    echo '<option class="filter_by" selected value="'. $values .'">Street 1</option>';
    ?>
</select>
</div>
<input type="submit" value="Submit"/>
</form>
Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64
  • Sorry, typo error. How to display multiple values in one option tag dynamically from mysql? – Elsk Feb 22 '17 at 10:08
  • I think your php code is okay. Do you set the name attribute in the select tag? Because in the second code the name attribute is in the option tag. – Julian Schmuckli Feb 22 '17 at 10:31
  • yes, it's already set. but how to put all values in one option tag? E.g. `` instead of ` ` – Elsk Feb 22 '17 at 10:42
  • it's not working, i tried this `$values = ""; foreach($results_streets as $option){ if(isset($_POST['mall_list']) && $_POST['streets_list'] == $option->streets) $values.=$option->stores."|"; }; echo '';` there's no value – Elsk Feb 23 '17 at 02:44
  • i tried this also, `$values = ""; foreach($results_streets as $option){ if(isset($_POST['streets_list']) && $_POST['streets_list'] == $option->streets) $values.=$option->stores."|"; echo ''; };` but the value of option is blank – Elsk Feb 23 '17 at 02:48
  • I updated again my answer. Is there really data in the option value? Try var_dump in the foreach loop. – Julian Schmuckli Feb 23 '17 at 05:12