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

Fetch the value of the labels and the selected options in Group Drop Down in PHP

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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表中。

欢迎任何帮助。


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

1
2
3
4
5
6
7
8
$("#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);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
<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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?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>

希望有帮助:)


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

1
2
3
4
5
6
7
8
9
10
11
12
13
<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,例如:

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

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

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