RazorFinger
RazorFinger

Reputation: 241

How to get the value from a combobox in php

I'm trying to get the options of a combobox to disappear depending on the preselected option that comes from the database.

When i asked that same question before, i've been given the idea to use two comboboxes, and depending on the value of the first one, options on the second one would disappear. And that worked, but the problem with a Jscript function is that i have to call it with an onchange event, meaning that it couldn't be preloaded from a previous page.

It used to look like this:

<select name="cbostatusHide" id="cbostatusHideID" onchange="getDropDown()">
                <option selected "" ></option>
                <option value="Planned"<?php if ($row['task_status']=='Planned') echo 'selected="selected"';?>>Planned</option>
                <option value="Started"<?php if ($row['task_status']=='Started') echo 'selected="selected"';?>>Started</option>
                <option value="Available"<?php if ($row['task_status']=='Available') echo 'selected="selected"';?>>Available</option>
                <option value="Impeded" <?php if ($row['task_status']=='Impeded') echo 'selected="selected"';?>>Impeded</option>
                <option value="Finished"<?php if ($row['task_status']=='Finished') echo 'selected="selected"';?>>Finished</option>
                </select>
                </td>
                </tr>

                <tr>
                <td>
                <select name="cbostatus" id="cbostatusID">
                <option selected "" ></option>
                <option value="1" <?php if ($row['task_status']=='Planned') echo 'selected="selected"';?>>Planned</option>
                <option value="2" <?php if ($row['task_status']=='Started') echo 'selected="selected"';?>>Started</option>
                <option value="3" <?php if ($row['task_status']=='Available') echo 'selected="selected"';?>>Available</option>
                <option value="4" <?php if ($row['task_status']=='Impeded') echo 'selected="selected"';?>>Impeded</option>
                <option value="5" <?php if ($row['task_status']=='Finished') echo 'selected="selected"';?>>Finished</option>
                </select>
                </td>
                </tr>

And the function:

function getDropDown() {
     var optionDrop = document.getElementById("cbostatusHideID");
    var numberDrop = document.getElementById("cbostatusID");

   if (optionDrop.options[optionDrop.selectedIndex].value == "Planned" || optionDrop.options[optionDrop.selectedIndex].value == "Started" || optionDrop.options[optionDrop.selectedIndex].value == "Available"){

       numberDrop.options[4].style.display = "none"; 

}else if (optionDrop.options[optionDrop.selectedIndex].value == "Impeded" || optionDrop.options[optionDrop.selectedIndex].value == "Finished"){            
                numberDrop.options[4].style.display = "block"; 
            }
            }

What this did was make the "Impeded" option unavailable whenever i selected either "Started", "Planned" or "Available".

But, do to the lack of sucess that i've explained in the beggining of the question, i decided to apply the same logical concept, but using switches in PHP.

The big problem is, i'm an absolute beginner at PHP, and i don't know how to do this properly, so it isn't working. Here's what a bit of fumbling around got me to:

                    <?php
                switch (cbostatusHideID.selectedIndex)
                {               
                    case $row['task_status'] == 'Planned';
                        cbostatus.option[4].style.display = "none";
                }
                ?>

As you more experienced PHP users can probably tell, this doesn't work at all. So how do i change it and make it into a proper switch?

Also, please don't suggest using Jquery, as we don't really use it here at work...

Upvotes: 1

Views: 782

Answers (1)

Proper implementation of switch in PHP is like this:

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

So, you need to modify your code:

<?php
switch ($row['task_status']) {               
    case 'Planned':
        // do your stuff
}
?>

And you cannot use JavaScript and PHP together the way you are trying to do in your code example.

Upvotes: 1

Related Questions