在PHP的Group Drop Down中获取标签的值和选定的选项

时间:2018-12-05 05:44:41

标签: php html

我是php的新手,我在HTML表单中有一个下拉列表,提交表单后,我需要获取该分组的名称以及PHP中的选定选项。

<select name="assign_id">
    <optgroup label="Admins">
            <option value="1">John Smith</option>
            <option value="2">Jane Smith</option>
    </optgroup>
    <optgroup label="Editors">
            <option value="3">Brian Smith</option>
            <option value="4">Scott Smith</option>
    </optgroup>
    <optgroup label="Basic Users">
            <option value="3">Kevin Smith</option>
            <option value="4">Tanya Smith</option>
    </optgroup>
</select>

例如,在上面的一堆I代码中,我需要在提交表单后获取标签“ Admins”以及选定的选项“ John Smith”,然后使用PHP将其保存在MySql表中。

欢迎任何帮助。

3 个答案:

答案 0 :(得分:0)

这可能有助于您在工作中取得进步!

$("#assign_id").change(function(){
    var el = document.getElementById('assign_id');
    var text = el.options[el.selectedIndex].innerHTML;
    
     var label = $(this.options[this.selectedIndex]).closest('optgroup').prop('label');
     alert(text);
     alert(label);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="assign_id" id="assign_id">
    <optgroup label="Admins">
            <option value="1">John Smith</option>
            <option value="2">Jane Smith</option>
    </optgroup>
    <optgroup label="Editors">
            <option value="3">Brian Smith</option>
            <option value="4">Scott Smith</option>
    </optgroup>
    <optgroup label="Basic Users">
            <option value="3">Kevin Smith</option>
            <option value="4">Tanya Smith</option>
    </optgroup>
</select>

答案 1 :(得分:0)

为什么不在选项的值中添加组的指示符?

<select name="assign_id">
<optgroup label="Admins">
        <option value="Admins_1">John Smith</option>
        <option value="Admins_2">Jane Smith</option>
</optgroup>
<optgroup label="Editors">
        <option value="Editors_3">Brian Smith</option>
        <option value="Editors_4">Scott Smith</option>
</optgroup>
<optgroup label="Basic Users">
        <option value="Basic_Users_3">Kevin Smith</option>
        <option value="Basic_Users_4">Tanya Smith</option>
</optgroup>

,并在您的php文件中使用substr,例如:

<?php $groupName = ( (substr($_POST['assign_id'],'Admins') != -1) ? 'Admins' : ( (substr($_POST['assign_id'],'Editors') != -1 ) ? 'Admins' : 'Basic_Users' ) ) ?>

并获取选项的真实值,请使用str_replace

<?php $optionValue = str_replace(array('Admins_','Editors_','Basic_Users_'),'',$_POST['assign_id']) ?>

答案 2 :(得分:0)

<?php
/*

Database: `testdb`

Table structure for table `groupnames`

DROP TABLE IF EXISTS `groupnames`;
CREATE TABLE IF NOT EXISTS `groupnames` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`groupname` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

Dumping data for table `groupnames`

INSERT INTO `groupnames` (`id`, `groupname`, `name`) VALUES
(1, 'Admins', '1'),
(2, 'Admins', '2'),
(3, 'Editors', '3'),
(4, 'Editors', '4'),
(5, 'Basic Users', '5'),
(6, 'Basic Users', '6');
COMMIT;

*/

$conn = mysqli_connect("localhost","root","","testdb"); //server,username,password,database
if(!$conn){
die("Database Connection Failed."); //if wrong server name,username,password or database name
}

if(isset($_POST['submit'])){
$grouplabel_name = $_POST['grouplabel_name'];
    echo $grouplabel_name; //displays the group label name and name from the group
}
?>
<form method="post" action="">
<select name="grouplabel_name">
<option></option>
    <?php
        $query = mysqli_query($conn,"SELECT * FROM groupnames ORDER BY id ASC")or die(mysqli_error($conn));

            while($row = mysqli_fetch_object($query)){
                $grouplabel = $row->groupname; //group label
                $name = $row->name; //name from the group

                    if($grouplabel == "Admins"){
                    ?>
                    <optgroup label="Admins">
                        <option value="<?php echo $grouplabel.' - '.$name; ?>"><?php 
                            if($name == 1){
                            echo "John Smith"; 
                            }
                            elseif($name == 2){
                            echo "Jane Smith";
                            }
                    ?>    
                        </option>
                    </optgroup>

                    <?php
                    }
                    elseif($grouplabel == "Editors"){
                    ?>
                    <optgroup label="Editors">
                        <option value="<?php echo $grouplabel.' - '.$name; ?>"><?php 
                            if($name == 3){
                            echo "Brian Smith"; 
                            }
                            elseif($name == 4){
                            echo "Scott Smith";
                            }
                    ?>
                        </option>
                    </optgroup>

                    <?php
                    }
                    elseif($grouplabel == "Basic Users"){
                    ?>
                    <optgroup label="Basic Users">
                        <option value="<?php echo $grouplabel.' - '.$name; ?>"><?php 
                            if($name == 5){
                            echo "Kevin Smith"; 
                            }
                            elseif($name == 6){
                            echo "Tanya Smith";
                            }
                    ?>
                        </option>
                    </optgroup>
                    <?php
                    }
            }
        ?>
</select>
<input type="submit" name="submit" value="Submit">
</form>

希望它会有所帮助:)