Reputation: 155
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?
Upvotes: 0
Views: 474
Reputation: 1021
<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.
Upvotes: 1
Reputation: 193
Basically, all you have to do is echo in certain content.
For textarea, we want to echo in the comment between the textarea open/close tags.
<textarea name="kommentar" cols="25" rows="7" required> <?php echo "$comment";?> </textarea>
For the radio button, you use the word "checked" (or checked="checked") to declare a checked option. You could check specific things as needed, and echo in the word checked where it should be.
<input type="radio" name="bewertung" value="1" required <?php echo "checked"; ?> />
For the select element, you use the word "selected" (or selected="selected") to declare a selected option. You could check specific things as needed, and echo in the word selected where it should be.
<option <?php echo "selected"; ?> >Java</option>
Upvotes: 0