如何使用PHP填充不同的表单字段值(How to fill different form fields with values using php)

编程入门 行业动态 更新时间:2024-10-23 01:38:01
如何使用PHP填充不同的表单字段值(How to fill different form fields with values using php)

我在填写3种表单输入时遇到问题。 单选按钮,选择(下拉列表)和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>

我需要一个预先选择的单选按钮,选择的下拉列表条目,还应填写注释字段(这还不起作用)。 怎么可能用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?

最满意答案

<textarea>不支持value属性,在<textarea></textarea>标记之间回显$comment 。

使用条件逻辑检查单选按钮并选择框选项:

<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

应用于您的原始代码:

<?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>

这将选择“PHP”选项并选中第4个单选按钮。

<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.

更多推荐

php,option>,drop-down,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-08-07 20:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   如何使用   PHP   fill

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!