如何在php页面中显示所选值,如下所示?

时间:2014-02-08 11:45:55

标签: javascript php arrays

我制作了一个php页面,其中我显示了所选的值,从post数组中选中的值检查这里是我的数组:

Array ( [events] => Array [fromdate] => [todate] => [description] => [subject] => [fromname] => [replyto] => [senddatetime] => [timezone] => America/Los_Angeles [message] =>

dfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh [submit_skip] =>继续)

我想获取事件数组并将事件数组与事件数组进行比较,如果事件数组值与选项匹配则显示如下所示选择:

enter image description here

这是我的代码:

 <select name="events[]" multiple size="10" >
        <?
        $events = array();
        $events=$_POST['events'];
        $select = sprintf ("SELECT event_id,event_name
                        FROM `events`
                        WHERE (`user_id` = '%s') order by event_name",
                        $GLOBALS ['mysqli']->real_escape_string ($_SESSION['user_id']));

        $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
        if ($res->num_rows > 0)
        {

            while($row = $res->fetch_assoc ())
            {
                if($row['event_id']== $_POST['events'])
                $selected = "selected=\"selected\"";
                else
                $selected = "";
                echo "<option value=\"".$row['event_id']."\" $selected>".$row['event_name']."</option>\n ";
            }
        }           
        ?>
        </select>

1 个答案:

答案 0 :(得分:2)

你的post数组不包含任何event_id数据,所以这个比较不起作用。如果你有这样的事情:

Array ( [events] => Array [0] => 3 [1] => 5 [2] => 4 )

然后你可以与之比较:

if(in_array($row['event_id'], $_POST['events'])