I have the following PHP code:
<?php
echo '<select name="transact_day" id="transact_day">';
echo"<option value=''>Select Day</option>";
for($i= 1; $i<=31; $i++){
echo "<option value=". $i ." ";
if(isset($day) == "$i"){
echo 'selected = "selected" ';
}
echo ">$i</option>\n";
}
echo '</select>';
?>
I want it to be sticky but when a date is selected (e.g 10) and the form is submitted it's only the value 31 that is sticky (irrespective of the selected date). I have tried different options, searched through similar questions but I couldn't figure out what was wrong with the above code. Any suggestion? Thank
isset($day) returns a boolean value, so comparing this to $i is incorrect. Check if $day is set, then compare $day with $i:
echo '<select name="transact_day" id="transact_day">';
echo"<option value=''>Select Day</option>";
for($i= 1; $i<=31; $i++){
echo "<option value=". $i ." ";
if(isset($day) && ($day == "$i")){
echo 'selected = "selected" ';
}
echo ">$i</option>\n";
}
echo '</select>';
Related
I have made a form that lets the user to select a year and place they want. The select tag for year is as below:
<strong>Tahun: </strong>
<?php
echo ('<select name="tahun">');
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo ('<option value= "'.$i.'">'.$i.'</option>');
}
echo ('</select>');
?>
And code for place selection tag as below:
<strong>Tempat: </strong>
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error._LINE_);
while($row = $results->fetch_assoc()){
echo ('<option value= "'.$row['nama'].'">'.$row['nama'].'</option>');
}
echo ('</select>');
?>
Now, what I want to do is add empty value with a message "Please choose one" for both selection tag. So, can someone help me how to add that value and message in this tag?
If you wanted to add select options to your code, this is how it should be:
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo ('<option>Please choose one </option>'); // Added this here
while ($row = $results->fetch_assoc()) {
echo ('<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>');
}
echo ('</select>');
?>
When you add default option with empty value then you should add as <option value="">Select...</option>
<strong>Tahun: </strong>
<?php
echo '<select name="tahun">';
echo '<option value="">Please Choose One</option>';
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo '<option value= "'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
and
<?php
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo '<select name="tempat">';
echo '<option value="">Please choose one</option>'; //Please choose one
while ($row = $results->fetch_assoc()) {
echo '<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>';
}
echo '</select>';
?>
in my dropdown list I insert a value from a mySQL query. This Value is my selected Option.
The problem is, that he duplicate my selected Value (it is logical because I create "6 Options" but the user only should have "5 Options").
How can I prevent this?
This is my Code:
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
echo "<option value='".$row['a_qty']."' selected>".$row['a_qty']."</option> ";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "</select>";
echo "<script>
And my Output:
Try as below :
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
for($i=1;$i<=5;$i++)
{
$selected = '';
if($i==$row['a_qty'])
$selected = 'selected="selected"';
echo "<option value='$i' $selected>$i</option>";
}
echo "</select>";
I would like to delete an user from a file. I generate the html form with the php code. Now, it reads the user from a file ann puts it in a email input field. When I click the delete button after the user I choose, it should delete him out of the file. But I don't know how to do it that it takes the right user, because I generate it with a for.
From where does the php choose if the button is clicked?
By the way the $liste is an Array with all the users in it.
echo '<form id=\"myform\" name=\"myform\">';
for ($j = 0; $j < count($liste); $j++) {
echo '<input id=\"email\" type=\"email\" name=\"email\" required=\"required\" value=' . $liste[$j][0] . '>';
echo '<select name="permission">
<option value="admin">admin</option>
<option value="user">user</option>
</select>';
echo '<input type=\'submit\' name=\'submit\'>';
echo '<input type=\'submit\' name=\'delete\' value=\'delete\'>';
echo '</form>';
if(isset($_GET['submit'])){
echo "Option doesn't work yet";
}}
if(isset($_GET['delete'])){
for ($i = 0; $i < count($liste); $i++){
if ($liste[$i][0] == $liste[j][0]){
echo $liste[$i][0].'deleted';
}
}
}
Ok for the beggining you having some syntax to fix in your form tag (missing method to get and action to some page)
for ($j = 0; $j < count($liste); $j++) {
echo "<form id=\"myform\" name=\"myform\" action=\"\" method=\"GET\">";
echo '<input id=\"email\" type=\"email\" name=\"email\" required=\"required\" value=' . $liste[$j][0] . '>';
echo '<select name="permission">
<option value="admin">admin</option>
<option value="user">user</option>
</select>';
echo '<input type=\'submit\' name=\'submit\'>';
echo '<input type=\'submit\' name=\'delete\' value=\'delete\'>';
echo '</form>';
}
if(isset($_GET['submit'])){
echo "Option doesn't work yet";
}
if(isset($_GET['delete'])){
for ($i = 0; $i < count($liste); $i++){
if ($liste[$i][0] == $liste[j][0]){
echo $liste[$i][0].'deleted';
}
}
}
I have a php select populated and given a matching value by using php to loop through the results of a sql query.
$result = mysqli_query($con, "select * from course")
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
That gives me a drop down list populated with all course titles,
upon submission I can access the selected value using $_POST['CourseSelect'];
However the drop down (select) resets itself to the default value when the page reloads.
How can I keep that option selected using php?
I know that I can use the selected keyword inside of an a select option to make that option the default selected option.
for example the second option would be selected when loading the page:
<select>
<option>One</option>
<option selected>Two</option>
<option>Three</option>
</select>
You can make it simple as
while($row = mysqli_fetch_array($result))
{
$select = '';
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] == $row['Title'] ) $select = 'SELECTED';
echo "<option value='".$row['Title']."' ".$select.">" . $row['Title'] . "</option>";
}
you can use like below..
$result = mysqli_query($con, "select * from course");
$selected = "";
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $row['Title'] == $_REQUEST['CourseSelect'] ? "Selected" : "";
echo "<option value='" . $row['Title'] ." ' $selected>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
try this:
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $_POST['CourseSelect'] == $row['Title'] ? 'selected' : '';
echo "<option value='{$row['Title']}' {$selected}>{$row['Title']}</option>";
}
you can do like this
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != "0")
{
echo "<option >".$_POST['CourseSelect']."</option>";
}
else
{
echo "<option value='0'> - Select Course - </option>";
}
while($row = mysqli_fetch_array($result))
{
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != $row['Title'])
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
PHP Function
# select box
/*
Example:
Parameter 1:
$options[1] = 'Course 1';
$options[2] = 'Course 2';
$options[3] = 'Course 3';
Parameter 2:
$selectedOption = 2; The dropdown need to be selected
*/
function buildOptions($options, $selectedOption)
{
foreach ($options as $value => $text)
{
if ($value == $selectedOption)
{
$Return .='<option value="'.$value.'" selected="selected">'.stripslashes($text).'</option>';
}
else
{
$Return .='<option value="'.$value.'">'.stripslashes($text).'</option>';
}
}
return $Return;
}
Function Call
$result = mysqli_query($con, "select * from course");
while ($row = mysqli_fetch_array($result))
{
$UniqueId = $row['Title'];
$Value = $row['Title'];
$optionsArray[$UniqueId] = $Value; // Store the values into an array
}
$CourseSelect = isset($_POST['CourseSelect']) ? $_POST['CourseSelect'] : '0';
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
echo buildOptions($optionsArray, $CourseSelect);
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
Notes
You can use buildOptions() for all of your projects to display select box. I have been using this for years.
I have a simple html form with select element. To define which option is selected, I add selected as
<option value="10" <?php if($value == '10') {echo 'selected="selected"';}?>>10</option>
<option value="20" <?php if($value == '20') {echo 'selected="selected"';}?>>20</option>
<option value="50" <?php if($value == '50') {echo 'selected="selected"';}?>>50</option>
$value is a variable which comes from PHP codes. This methods seems to be very simple and naive. Is it the best way to do so?
$options = array(10,20,50);
foreach($options as $option) {
$selected = ($value == $option) ? ' selected="selected"' : '';
echo '<option value="' . $option . '"' . $selected . '>' . $option . '</option>';
}
Not a bad way to do it. But why not create the entire thing using programmatic approach (i.e., generate the code using a PHP loop through an array):
$items = array(10, 20, 50);
for ($i = 0; $i < count($items); $i ++) {
echo("<option value='" . $items[$i] . "'");
if ($items[$i] === $value) {
echo(" selected='selected'");
}
echo(">" . $items[$i] . "</option>");
}
Something along the lines of the below should work:
<?
$values = array(10, 20, 30);
foreach ($values as $val) {
?>
<option value="<?=$val?>" <?=($value==$val ? "SELECTED" : "")?>><?=$val?></option>
<?
}
?>
Keep your values in an array and loop over it to generate the HTML. Then you don't need to repeat yourself.
<?php foreach($list as $key => $value): ?>
<option value=<?php echo $value ?> <?php echo $value == $selected? 'selected' : '' ?> ><?php echo $value ?></option>
<?php endforeach ?>
Hope you get the idea.
This is the like i do it:
<?
$values = array(10,20,30,......);
$value = XXX;
echo "<select name='somename'>";
foreach($values as $v)
{
if($v == $value)
{
echo "<option value='$v' 'selected'>$v </option>";
}
else
{
echo "<option value='$v'>$v </ option> ";
}
}
echo "</select>";
?>