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.
Related
When executed, the radio buttons are not working, which they were when they were introduced without PHP.
Here is my code:
<td><?php echo date('l');?></td>
<?php
while ($meal_num<=4)
{
echo '<td>';
echo $row[$meal_num+2];
echo '<form action="rating.php?hostel='.$hostel.'&meal_type='.$meal_num.'" method="POST"><br><br>';
?>
<span class="radiolabel" >
<input type="radio" name="radio1" id="radio-1" value="1" />
<label for="radio-1">1</label>
<input type="radio" name="radio1" id="radio-2" value="2"/>
<label for="radio-2">2</label>
<input type="radio" name="radio1" id="radio-3" value="3"/>
<label for="radio-3">3</label>
<input type="radio" name="radio1" id="radio-4" value="4"/>
<label for="radio-4">4</label>
<input type="radio" name="radio1" id="radio-5" value="5"/>
<label for="radio-5">5</label>
</span>
<span> <input type="submit" class="submitbutton1" value="OK"></span>
</form>
</td>
<?php
$meal_num=$meal_num+1;
}?>
</tr>
</table>
Probably because your radio inputs are in a while loop, so you recreate multiple sets of radio inputs with the same set of ids.
Try attributing a different value for "id" for each loop that's executed with an increment number.
For example:
$i = 1;
while (blabla) {
echo '<input type="radio" id="radio-'.$i.'" name="radio1" />';
$i++;
}
So the radios from one loop will not interfere with the radios from another loop.
After each radio you put, you increment the number so the id will never be the same (an id should be unique in a page).
I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.
Here is my code,
$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}
<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>
Please help me regarding this. Thanks in advance.
You need to apply condition on checked HTML attribute.
Try this:
<form>
Address : <br>
<textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
Gender : <br>
<input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : ''; ?> >Male
<input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : ''; ?>>Female <br>
<input type="submit" value="Save">
</form>
Value has to be placed between the openning and closing tags :
<texterea name="whatever">Textarea value goes here</textarea>
To set a radio/checkbox as selected/checked, you need to add to it a "checkded" attribute :
<!-- HTML4 -->
<input type="radio" name="whatever" value="value1" checked="checked" /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
<!-- HTML5 -->
<input type="radio" name="whatever" value="value1" checked /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
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>
How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database
To allow a user to edit information in a record, this is done:
$case=$_GET['case'];
$query="SELECT * FROM `cases` WHERE `case`= '$case'";
$result=mysql_query($query);
<input type="text" name="firstname" value="<?php echo $firstname; ?>" />
I need to set the value of a radio group based on what its value is in the "cases" table.
<input type="radio" name="flight1_departing" value="AM" />
<input type="radio" name="flight1_departing" value="PM" />
How is this possible?
<input <?php if ($somevalue == 'AM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="AM" />
<input <?php if ($somevalue == 'PM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="PM" />
Given a known value $val, you just need to check it against each radio button value and set the checked attribute, eg
<input type="radio" name="flight1_departing" value="AM"
<?php if ($val == 'AM') : ?>checked="checked"<?php endif ?>
/>
<input type="radio" name="flight1_departing" value="PM"
<?php if ($val == 'PM') : ?>checked="checked"<?php endif ?>
/>
That example is very manual. It would be easier if the radio elements are created in a loop.
Your questions is a little ambiguous but I'm going on the assumption that you mean you need to determine which value is default checked based on the value in the cases table?
Something like,
<input type="radio" name="flight1_departing" value="AM" <?php if ($some_cases_value) { print 'CHECKED'; } ?>/>
<input type="radio" name="flight1_departing" value="PM" <?php if ($some_cases_value) { print 'CHECKED'; } ?> />
Though there is likely a very more elegant way of doing it?