Welcome Guest, Not a member yet? Register   Sign In
Re-populating select from db and validate it?
#11

[eluser]felyx[/eluser]
Here is the js code:

Code:
$(document).ready(function() {
                $("select[name=category]").change(function() {
                    //var optionArray = {
                        //"Value 1" : "Text 1",
                        //"Value 2" : "Text 2",
                        //"Value 3" : "Text 3"
                    //};
                    $("select[name=subcategory]").children().remove();
                    $("select[name=subcategory]").addOption('Please Choose!', 'Please Choose!');
                    var selcat = $("select[name=category]").val();
                    if (selcat != 'Please Choose!')
                    {
                    <?php
                     echo 'switch(selcat){ ';
                     foreach($subcategories as $catname => $key) {
                         $scat_arr = explode(",",$key);
                         sort($scat_arr);
                         $j = count($scat_arr);
                         echo 'case "' . $catname . '": ';
                         echo 'var optionArray = { ';
                         $i = 1;
                         $j = count($scat_arr);
                         foreach($scat_arr as $scat_item) {
                             echo '"' . $scat_item . '" : "' . $scat_item . '"';
                             echo ($i < $j) ? ', ' : '';
                             $i++;
                         }
                         echo '}; ';
                         echo 'break; ';
                     }
                     echo '}';
                    ?&gt;
                    // Use true if you want to select added options
                   $("select[name=subcategory]").addOption(optionArray, false);
                   }
                });
            });


I know I got errors in my code because im still trying to figure out a solution and I also know that my category system is very much lame but thats not the problem atm. If you can, please help me about this problem.

Basically I need to be able to set a default value for selects if the data is set in the database (like set_value('', default) for other input elements).
#12

[eluser]tyuwan[/eluser]
This is what i did for mine, i hope this would help you.


Code:
<label class="desc">Select Category:</label>
<select class="field select small" name="edit_parent">
    <option></option>
    &lt;?php foreach($categories as $cat):?&gt;
    <option value="&lt;?=$cat['id'];?&gt;"&lt;?php if($cat['id'] == $editinfo['parent_id'])
    {
        echo set_select('edit_parent',$cat['id'],TRUE);
    }
    else
    {
        echo set_select('edit_parent',$cat['id']);
    }
    ?&gt;>&lt;?=$cat['title'];?&gt;</option>
    &lt;?php endforeach;?&gt;
</select>




Theme © iAndrew 2016 - Forum software by © MyBB