douhuan1937 2016-05-10 20:19 采纳率: 0%
浏览 8

将PHP变量设置为用户选择的下拉表单的值

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:

<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>

The form then checks if the user has filled out the "name" field with this:

$name = $input->get('name', null);
if($name == null)
    return false;

This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:

<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
    <option value="default">Select </option>
    <option value="1"><?php echo $params->get('department1');?></option>
    <option value="2"><?php echo $params->get('department2');?></option>
    <option value="3"><?php echo $params->get('department3');?></option>
    <option value="4"><?php echo $params->get('department4');?></option>
    <option value="5"><?php echo $params->get('department5');?></option>
    <option value="6"><?php echo $params->get('department6');?></option>
    <option value="7"><?php echo $params->get('department7');?></option>
    <option value="8"><?php echo $params->get('department8');?></option>
</select>

$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........

This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.

  • 写回答

3条回答 默认 最新

  • dongyongju9560 2016-05-10 20:28
    关注

    And if you want to a "pre selected" option use "selected" in the option you want:

    <select name="department" form="quickcontact_frm">
        <option value="default">Select </option>
        <option value="1"><?php echo $params->get('department1');?></option>
        <option value="2"><?php echo $params->get('department2');?></option>
        <option value="3"><?php echo $params->get('department3');?></option>
        <option value="4" selected><?php echo $params->get('department4');?></option>
        <option value="5"><?php echo $params->get('department5');?></option>
        <option value="6"><?php echo $params->get('department6');?></option>
        <option value="7"><?php echo $params->get('department7');?></option>
        <option value="8"><?php echo $params->get('department8');?></option>
    </select>
    

    As explained by W3CSchools.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?