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> 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.
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>
Related
Open a div on based on database radio button value. if value is male in database then show For male and if value is female in database then show Female male
$status = isset($_POST['status']) ? $_POST['status'] : '';
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
<div class="1">For male</div>
<div class="2">Female male</div>
if i understand you right this will do the trick
$status = isset($_POST['status']) ? $_POST['status'] : '';
if ($status == "male") {
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
}elseif ($status == "female") {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female" checked="checked">female<br>
}else {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female">female<br>
}
Just add if condition
$status = isset($_POST['status']) ? $_POST['status'] : ''; ?>
<input type="radio" name="status" value="male" <?php echo $status=='male'? 'checked="checked"':''?>>male<br>
<input type="radio" name="status" value="female" <?php echo $status=='female'? 'checked="checked"':''?>>female<br>
<?php if($status=='male') { ?>
<div class="1">For male</div>
<?php } ?>
<?php if($status=='female') { ?>
<div class="2">For Female</div>
<?php } ?>
I'm working on a project. I want to pre-populated data from database.
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q1" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q1" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q2" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q2" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q3" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q3" value="C" <?php echo $checkedC ?>>
</div>
<!-- etc till let's say 30 question -->
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
for($i=1; $i<=30; $i++){
switch($data[${"answer".$i}]{
case "A" : $checkedA="checked"; break;
case "B" : $checkedB="checked"; break;
case "C" : $checkedC="checked"; break;
}
}
Then, how to make the q1 which is corresponded with $data['answer1'] checked if it is filled with data from database, etc?
Using the code you've provided, It's viable to get the data from the database and then show the result in your html and not over complicate things. To pre-populate your radio buttons, you need to have the following below:
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo ($data['q1'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q1" value="B" <?php echo ($data['q1'] == 'B')? 'checked' : ''; ?>>>
<input type="radio" name="q1" value="C" <?php echo ($data['q1'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo ($data['q2'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="B" <?php echo ($data['q2'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="C" <?php echo ($data['q2'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo ($data['q3'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="B" <?php echo ($data['q3'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="C" <?php echo ($data['q3'] == 'C')? 'checked' : ''; ?>>
</div>
With that piece of code, it sets checked depending on what the value coming from the database is and displays it on the screen.
Example has exactly 3 variables for checked ($checkedA, $checkedB, $checkedC). In order to have 3 varsiables per question, it would need a collection of "checks". If you have n questions, your collection needs n members. And each of those members needs 3 members representing whether the answer is checked, one for each option.
As an example:
If the answer to q1 is A, then
$checked['q1'] = ['A'=> "checked",'B'=>"",'C'=>""].
In the php, initialize a $checked member for each question as described above. Then change the switch to update the corresponding member. [The reason to initialize this way is because 1) everything needs a value in the html and 2) checked is a boolean attribute]
The html would echo $checked[q#]['A'] $checked[q#]['B'] and $checked[q#]['C'] respectively.
I have a table with the fields name and gender. I get the data and I try to display the information in the input fields, and it works on the input text Name, but i wonder if i can check the radio buttons depending of the data i get from the table?
<?php
$sql="select * from empleo where id='$id';";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));}
$cs=mysqli_query($con,$sql);
while($resul=mysqli_fetch_array($cs)){
$name=$resul[0];
$sexo=$resul[1];
}
?>
Name: <input type="text" value="<?php echo $name?>"/><br>
<label for="radio">Hombre</label>
<input type="radio" name="gender" id="radio" value="men">
<label for="radio" > Mujer</label>
<input type="radio" name="gender" id="radio" value="women">
Supposing your $result[1] is W for women and M for men (change the code, if not):
<label for="radio">Hombre</label>
<input type="radio" name="gender" id="radio" value="men" <?php if($sexo=="M") echo "checked" ?>>
<label for="radio" > Mujer</label>
<input type="radio" name="gender" id="radio2" value="women" <?php if($sexo=="W") echo "checked" ?>>
Read Radio Button Input
<label for="sexo-men">Hombre</label>
<input type="radio" name="gender" id="sexo-men" value="men" <?php $sexio === 'men' ? 'checked="checked"' : '' ?> />
<label for="sexo-women">Mujer</label>
<input type="radio" name="gender" id="sexo-women" value="women" <?php $sexio === 'women' ? 'checked="checked"' : '' ?> />
And also id attribute of html is must unique on the page.
Checking a checkbox in HTML is done with the checked attribute:
<input name="awesome" type="checkbox" checked="checked" />
So we can setup a little function to help us output this:
function check_if($condition) {
if ($condition) {
echo 'checked="checked"';
}
}
The problem with your script is that you only set up variables for your first record from your empleo table.
What you most likely want is to instead loop through all the records:
// Lets get all the records from result and as an associative array
$employees = mysqli_fetch_all($cs, MYSQLI_ASSOC);
// this lets us use column names instead of indexes - much easier to read the code.
foreach($employees as $e) { ?>
<label for="name">Name<label><input name="name" type="text" value="<?php echo $e['name']?>"/>
<label for="gender">Hombre</label>
<input type="radio" name="gender" id="radio" value="male" <?php check_if($e['gender'] === 'male') ?>>
<label for="gender">Mujer</label>
<input type="radio" name="gender" id="radio" value="female" <?php check_if($e['gender'] === 'female') ?>>
<?php } // end foreach ?>
I assumed the columns in your empleo table where gender and name.
You also should note that each <input> should have a name attribute and each <label> should have a for
attribute which matches the name of the input it labels.
I have sucessfully sent my data to my page in the form of the url :
http://localhost:8101/Tutorials/sandbox/myfiles/make_a_booking.php?period=2&date=04/29/2014&room=028
But I am struggling to place the data into the form. But how would I place the period=2 into the radio buttons and the room=028 into the select drop down?
<form class="submit_date" method="post" action="insert.php" id="submit_date" onsubmit="return confirm('Confirm your booking?');">
<p>Date: <input type="text" name="datepicker" id="datepicker" required value="<?php echo $_GET['date'];?>"></p>
<input type="radio" name="bookperiod" id="bookperiod" value="1" required/>1<br>
<input type="radio" name="bookperiod" id="bookperiod" value="2" required/>2<br>
<input type="radio" name="bookperiod" id="bookperiod" value="3" required/>3<br>
<input type="radio" name="bookperiod" id="bookperiod" value="4" required/>4<br>
<input type="radio" name="bookperiod" id="bookperiod" value="5" required/>5<br>
<select class="dropdown" id="bookroom" name="bookroom" required;>
<option selected disabled hidden value=''></option>
<?php
for ($x=0; $x<sizeof($rooms_array); $x++)
{
echo "<option value='$rooms_array[$x]'>".$rooms_array[$x]."</option>";
}
?>
</select>
<input class="submit_btn" value="Submit" type="submit" name="Submit";/>
</form>
I've tried placing the room=028 into option via $_GET but it hasnt worked and the radio buttons I wouldnt know where to begin.
you can use php for that:
<?php
$selectedPerion = $_GET['period'];
for($x = 1; $x < 6; $x++): // short notation to separate php and html better?>
<input type="radio" name="bookperiod" id="bookperiod" value="<?php echo $x ?>" required
<?php echo ($selectedPerion == $x ? 'checked' : ''); //so called 'ternary operator' ?>/>
<?php echo $x ?>
<br>
<?php endfor; ?>
Or you can use jQuery to set the 'checked' property or simulate click on the proper button, but that's another story.
it is similar for the select dropdown, you have to add the 'selected' property to proper "" tag, or set the value for bookroom field with jQuery.
I have a Gender Row with radio buttons male & female. when i register as first time the values of radio button will store in database. Now my question is if i edit that row again it want to come(that means checked) with that value as male/female. how to make it?
Note : Doing with php.
HTML Script :
<tr id="inside">
<td align="right" width="40%" id="side" >Gender</td>
<td width="3%"> </td>
<td align="left" width="50%">
<input type="radio" name="sex" value="Male" size="17">Male
<input type="radio" name="sex" value="Female" size="17">Female
</td>
</tr>
When you populate your fields, you can check for the value:
<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ?>size="17">Male
<input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female
Assuming that the value you return from your database is in the variable $sex
The checked property will preselect the value that match
just add 'checked="checked"' in the correct radio button that you would like it to be default on. As example you could use php quick if notation to add that in:
<input type="radio" name="sex" value="Male" size="17" <?php echo($isMale?'checked="checked"':''); ?>>Male
<input type="radio" name="sex" value="Female" size="17" <?php echo($isFemale?'checked="checked"':''); ?>>Female
in this example $isMale & $isFemale is boolean values that you assign based on the value from your database.
This is easier to read for me:
<input type="radio" name="rWF" id="rWF" value=1 <?php if ($WF == '1') {echo ' checked ';} ?> />Water Fall</label>
<input type="radio" name="rWF" id="rWF" value=0 <?php if ($WF == '0') {echo ' checked ';} ?> />nope</label>
Gender :<br>
<input type="radio" name="g" value="male" <?php echo ($g=='Male')?'checked':'' ?>>male <br>
<input type="radio" name="g" value="female"<?php echo ($g=='female')?'checked':'' ?>>female
<?php echo $errors['g'];?>
For those who might be in need for a solution in pug template engine and NodeJs back-end, you can use this:
If values are not boolean(IE: true or false), code below works fine:
input(type='radio' name='sex' value='male' checked=(dbResult.sex ==='male') || (dbResult.sex === 'newvalue') )
input(type='radio' name='sex' value='female' checked=(dbResult.sex ==='female) || (dbResult.sex === 'newvalue'))
If values are boolean(ie: true or false), use this instead:
input(type='radio' name='isInsurable' value='true' checked=singleModel.isInsurable || (singleModel.isInsurable === 'true') )
input(type='radio' name='isInsurable' value='false' checked=!singleModel.isInsurable || (singleModel.isInsurable === 'false'))
the reason for this || operator is to re-display new values if editing fails due to validation error and you have a logic to send back the new values to your front-end
If you are getting your values from a database table and creating radio buttons dynamically, here is the solution. The database records are fetched into an array in this example and used for creating radio buttons.
<?php foreach ($dbrecords as $item) : ?>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="paymentMethod" id="paymentMethod" value=<?php echo $item["Id"]; ?> <?php echo ($paymentMethod == $item["Id"]) ? 'checked' : '' ?>><?php echo $item["Name"]; ?>
</div>
<td><input type="radio" name="gender" value="Male" id="male" <? if($gender=='Male')
{?> checked="" <? }?>/>Male
<input type="radio" name="gender" value="Female" id="female" <? if($gender=='Female') {?> checked="" <?}?>/>Female<br/> </td>