dongwei1855 2016-03-02 21:35
浏览 37
已采纳

如何使用php用值填充不同的表单字段

I'm having issues with filling 3 types of form input. Radio buttons, select (drop-down list) and textarea.

<textarea name="kommentar" cols="25" rows="7" value="<?php echo "$comment";?>" required></textarea>

<select name="interesse" required>
<option disabled selected>Bitte auswählen</option>
<option>Java</option>
<option>PHP</option>
<option>C++</option>
<option>Ruby</option>
<option>SQL</option>
<option>PLSQL</option>
</select>

<fieldset>
<label for="bewertung">
<input type="radio" name="bewertung" value="1" required />1
<input type="radio" name="bewertung" value="2" required />2
<input type="radio" name="bewertung" value="3" required />3
<input type="radio" name="bewertung" value="4" required />4
<input type="radio" name="bewertung" value="5" required />5
<input type="radio" name="bewertung" value="6" required />6
</label>
</fieldset>

I need a preselected radio button, selected drop-down list entry and also the comment field should be filled (that doesn't work yet). How is it possible, to fill these with values from php variables?

  • 写回答

2条回答 默认 最新

  • drgawfsf1069 2016-03-02 21:49
    关注

    <textarea> doesn't support the value attribute, echo your $comment between the <textarea></textarea> tags.

    Use conditional logic to check off a radio button and select box options:

    <option value="bar" name="foobar" <?php echo ($foobar == "bar" ? "selected=\"selected\"" : ""); ?>>bar</option>
    
    <input type="radio" value="foo" name="foobar" <?php echo ($foobar == "foo" ? "checked=\"checked\"" : ""); ?> /> foo
    

    UPDATE

    Applied to your original code:

    <?php
    $interesse = "PHP";
    $bewertung = 4;
    ?>
    
    <textarea name="kommentar" cols="25" rows="7"  required><?php echo "$comment";?></textarea>
    
    <select name="interesse" required>
    <option disabled>Bitte auswählen</option>
    <option <?php echo ($interesse == "Java" ? "selected=\"selected\"" : ""); ?>>Java</option>
    <option <?php echo ($interesse == "PHP" ? "selected=\"selected\"" : ""); ?>>PHP</option>
    <option <?php echo ($interesse == "C++" ? "selected=\"selected\"" : ""); ?>>C++</option>
    <option <?php echo ($interesse == "Ruby" ? "selected=\"selected\"" : ""); ?>>Ruby</option>
    <option <?php echo ($interesse == "SQL" ? "selected=\"selected\"" : ""); ?>>SQL</option>
    <option <?php echo ($interesse == "PLSQL" ? "selected=\"selected\"" : ""); ?>>PLSQL</option>
    </select>
    
    <fieldset>
    <label for="bewertung">
    <input type="radio" name="bewertung" value="1" required <?php echo ($bewertung == 1 ? "checked=\"checked\"" : ""); ?> />1
    <input type="radio" name="bewertung" value="2" required <?php echo ($bewertung == 2 ? "checked=\"checked\"" : ""); ?> />2
    <input type="radio" name="bewertung" value="3" required <?php echo ($bewertung == 3 ? "checked=\"checked\"" : ""); ?> />3
    <input type="radio" name="bewertung" value="4" required <?php echo ($bewertung == 4 ? "checked=\"checked\"" : ""); ?> />4
    <input type="radio" name="bewertung" value="5" required <?php echo ($bewertung == 5 ? "checked=\"checked\"" : ""); ?> />5
    <input type="radio" name="bewertung" value="6" required <?php echo ($bewertung == 6 ? "checked=\"checked\"" : ""); ?> />6
    </label>
    </fieldset>
    

    This would have the "PHP" option selected and the 4th radio button checked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装