问题描述
我有一个类似于下面的表单格式,用户可以使用选择选项为每个成员选择不同的评级.
I have a form format something like below where users can select different ratings for each member using a select option.
+---------+---------+----------+
| Sl.No |Members | Rating |
+------------------------------+
| 1 | abc | 4 |
| 2 | xyz | 4 |
| 3 | pqr | 3 |
| 4 | jkl | 5 |
+---------+---------+----------+
评级是通过使用名称为 name="ratings"
的 select
选项完成的.
The rating is done by using select
options with a name name="ratings"
.
提交表单时,如何在php中获得每个成员的相应评分?
On submitting the form, how can I get the corresponding rating for each memeber in php?
$rating = Input::get('rating')
这是从选择中获取值的传统方式.但是我为每个成员有多个选择.那么如何获得每个用户的评分?
This is the traditional way to get the value form the select. But I have multiple selects for each members. So how can I get the rating for each user?
我尝试添加一个隐藏字段并为每个成员附加 user_id
.但是仍然不知道如何从选择选项中选择正确的数据.从用户那里获取输入的最佳方式是什么?
I tried to add a hidden field and append the user_id
for each member. But still haven't got an idea about how to select the right data from the select option. What would be the best way to get the input from the users?
推荐答案
如果您有以下 html(使用 Member 作为排名键,但可以使用 user_id 等)-
If you have the following html (used Members as rankings key, but could use user_id, etc) -
Sl.No |Members | Rating
------------------------------
| 1 | abc | <select name="rankings[abc]"><option selected>4</option></select>
| 2 | xyz | <select name="rankings[xyz]"><option selected>4</option></select>
| 3 | pqr | <select name="rankings[pqr]"><option selected>3</option></select>
| 4 | jkl | <select name="rankings[jkl]"><option selected>5</option></select>
然后在你的 php 中
Then in your php
$rating = Input::get('rating');
foreach($rating as $user => $rate){
echo $user.": ".$rate."<br />";
}
或者,如果你在 mysql 中更新他们的评级
or, if you are updating their rating in mysql
$rating = Input::get('rating');
foreach($rating as $user => $rate){
query -> UPDATE yourTable SET rating = $rate WHERE user_id = $user
}
这篇关于如何从php中的多行中获取选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!