php html select value - php

I have a select box connect to database for countries, it works but trying to keep the selected data in the form clears itself out. I have tried adding a selected but gives error
<div class="form-group">
<label>Country</label>
<?php
$sql = "SELECT * FROM countries ";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<option value="">Select a country</option>
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
</select>
</div>

Assign the return value of a ternary operator to the variable $selected and add it to the option tag
<?php
$selected = ($_POST['country'] == $row["country_id"])?"selected":"";
?>
<option value="<?=$row["country_id"];?>" <?=$selected ?>>
<?=$row["country_name"];?>
</option>

Related

Form isn't showing select dropdown after validation error

In my form, I generate the select options from my MySQL data table. But when I submit the form without any value, it returns to a blank page and does not show any validation error. When I select a value and submit the form, it inserts the value to MySQL table. No MySQL or PHP error is thrown. I am assuming the problem is the query for the select option is not running. But if it is the problem, what should be the good practice to avoid this problem?
My form is:
<?php
require 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ad_di = "";
$ad_di_err = "";
if (empty(trim($_POST["ad_di"]))) {
$ad_di_err = "District cannot be empty.";
} else {
$ad_di = trim($_POST["ad_di"]);
}
if (empty($ad_di_err)) {
$sql = "INSERT INTO address (ad_di) VALUES (?)";
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("i", $p_ad_di);
$p_ad_di = $ad_di;
if ($stmt->execute()) {
$id = $mysqli->insert_id;
header("location: add.php?id=$id&update=success");
}
$stmt->close();
}
}
}
?>
<form id="acEdit" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="contact-form">
<div class="row">
<div class="col-3">
<div class="form-group">
<label>District</label>
<select name='ad_di' id='ad_di' class='custom-select'>
<option value=''>Select one ...</option>
<?php
$qDistrict = "SELECT * FROM districts order by name ASC";
echo "<select name='ad_di' id='ad_di' class='custom-select'><option value=''>Select one ...</option>";
foreach ($mysqli->query($qDistrict) as $rowDistrict) {
echo "<option value={$rowDistrict['id']}>{$rowDistrict['name']}</option>";
}
echo "</select>";
?>
</select>
<span class="invalid-feedback"><?php echo $ad_di_err; ?></span>
</div>
</div>
</div>
</form>
The above query for select options returns as following when the page is loaded first,
<select name="ad_di" id="ad_di" class="custom-select">
<option value="">Select one ...</option>
<option value="28">Dist A</option>
<option value="11">Dist B</option>
<option value="35">Dist C</option>
<option value="33">Dist D</option>
</select>
But when the validation error occurs, the returning page doesn't have any options, just returns
<select name="ad_di" id="ad_di" class="custom-select">
<option value="">Select one ...</option>
</select>

How to echo the selected value from an array fetched from the database

I want to echo the selected value from the database to update it then store it
for example I have an asset with category printers from table category which contains other categories and when I want to edit this asset on the edit page I should get a dropdown list contains all the categories and selected on printers then if I want to change it I will if not leave unchanged
The array is drop-down from table category inner joined with user_asset table in the database by asset_category as a foreign key
this is what I have done so far
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?>
</option>
<?php }?>
</select>
</div>
You can add if check if ($row['cate_name'] == 'computer') { ?> and then add selected to this option:
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required >
<?php while( $row = mysqli_fetch_array($result)) {
if ($row['cate_name'] == 'computer') { ?>
<option value="<?php echo $row['category_id'];?>" selected><?php echo $row['cate_name'];?></option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['cate_name'];?></option>
<?php }
}?>
</select>
Notice: If you have multiple elements with that category it will select the last one.
the answer is very simple.. let's put this code
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {
if($row['cate_name']== printers) { ?>
<option value="<?php echo $row['category_id'];?>" selected="selected">
<?php echo $row['cate_name'];?> </option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?> </option>
<?php }?>
</select>
</div>
The logic is that using while loop, checking the condition using if class, and when it satisfies make it as selected. Then it will be echo as selected Value.

How to used selected in dropdown list

i dont know exactly how i am supposed to explain my question. but i'll try my best. Im currently trying to make an update form. when user clicked on the edit icon they will be directed to the edit form and the value are carried. i used 'typeid' to carry the values. im having problem with my drop down. i used selected but the value duplicate.
dropdown
so im trying to solve this, but know idea how to solve it. i have to used php only and i am not an expert of php.
if (isset($_GET['typeid'])) {
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
if($row['status_vehicleType']==1){
$status = "Enabled";
}
else{
$status = "Disabled";
}
}
above are the typeid that i used to carry the values.
<div class="form-group">
<label>Choose Vehicle Type Status</label>
<select class="form-control" name="status" required class="form-control" value="<? php if(isset($row['status_vehicleType'])){ echo $status; } ?>">
<option value="">Select Vehicle Type</option>
<option value=<?php echo $row['status_vehicleType']; ?> <?php if($_GET["typeid"]==$row['status_vehicleType']){ ?> selected <?php } ?> ><?php echo $status; ?></option>
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</select>
Here is simple answer for this question
if you have any other questions freely ask me thanks
<?php
function selected($x, $y) {
if ($x == $y) {
return " selected";
} else {
return "";
}
}
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$q_status = $row['status_vehicleType'] ;
?>
<div class="form-group">
<label class="control-label text-inverse" for="name">Status</label>
<select class="form-control" name="q_status" id="q_status" required="" autocomplete="off" >
<option value="1" <?php echo selected($q_status,"1");?> >Enabled</option>
<option value="0" <?php echo selected($q_status,"0");?> >Disabled</option>
</select>
</div>
use below code, (for testing set $_GET['typeid']=somevalue):
$row=array();
if (isset($_GET['typeid'])) {
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
}
<div class="form-group">
<label>Choose Vehicle Type Status</label>
<select class="form-control" name="status" required class="form-control">
<option value="">Select Vehicle Type</option>
<option value="1" <?php if($row['status_vehicleType']==1){?>selected<?php }?>>Enabled</option>
<option value="0" <?php if($row['status_vehicleType']==0){?>selected<?php }?>>Disabled</option>
</select>

Text box show value from Ajax

Using PHP, Ajax and Jquery, I'm able to create a cascading dependent drop down menu. But I want to have the third drop down to be a text box instead.
How can I do this?
Ajax Code
PHP code...
<?php
include('conn.php');
$query = $con->query("SELECT * FROM job_category group by category_name ");
$rowCount = $query->num_rows;
?>
Category:
<select name="category" id="category">
<option value="">Select Category</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_num'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<br>
Sub-Category:
<select name="sub_type" id="sub_type">
</select>
<br>
Priority:
<select name="priority" id="priority_level" >
</select>
?>
Please help, I'm having a hard time on this.

Setting selected option in drop-down box

My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>

Categories