I search for answers here but haven't found a solution.
I have also added picture of the error.
I want the data to go to the first drop-down list ( above the error)
I think the method I try to perform is also create drop-down list, am I correct?
<form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
<div class="form-group">
<label id="senderName">שם השולח:</label>
</div>
<div class="form-group">
<label for="to_user">מען:</label>
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = \mysqli_query("SELECT name From users");
$row = mysqli_num_rows($sql);
echo "<select name='to_user'>";
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
echo "</select>" ;
?>
</select>
</div>
picture of the error
In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.
$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");
Note: replace $con with your database connection variable!
As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
</div>
<label><b>Select Steam: </b></label>
<select id="study">
<option value="" selected="selected" disabled="">---Selected---</option>
<?php
$query = "SELECT study FROM details";
$query_run = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($query_run)) {
echo "<option value='".$row['study']."'>".$row['study']."</option>";
}
?>
</select>
Related
The Below code we are using. Please check and update:
`
dashboard
</div>
<div class="form-group">
<label for="to_user">Turbine</label>
<select name="to_user" class="form-control">
<option value="pick">--Select All--</option>
<option> <?php
$query = "SELECT DISTINCT Turbine FROM [JMDWF].[dbo].[TENAVG]";
$sql = sqlsrv_query($link,$query);
$row = sqlsrv_num_rows($sql);
$menu = "";
echo "<select name='to_user'>";
while ($row = sqlsrv_fetch_array($sql)){
/* echo "<option value='". $row['Turbine'] ."'>" .$row['Turbine'] ."</option>" ;*/
$menu.="<option>".$row['Turbine']."</option>";
}
echo "$menu";
?>
</option>
</select>
</div>`
Still we getting output out of the drop down list. I need to show the values in drop down.
Your are printing the output from PHP into your <option></option> node of the html - it won't be visible.
Try this instead:
<div class="form-group">
<label for="to_user">Turbine</label>
<?php
$query = "SELECT DISTINCT Turbine FROM [JMDWF].[dbo].[TENAVG]";
$sql = sqlsrv_query($link,$query);
$row = sqlsrv_num_rows($sql);
$menu = "";
echo "<select name='to_user'>";
echo "<option value='pick'>--Select All--</option>";
while ($row = sqlsrv_fetch_array($sql)){
/* echo "<option value='". $row['Turbine'] ."'>" .$row['Turbine'] ."</option>" ;*/
$menu.="<option>".$row['Turbine']."</option>";
}
echo "</select>";
echo "$menu";
?>
</div>`
I'm trying to do an update page, the data that I'm fetching are input texts, dates, dropdowns and a textarea.
The data on input texts & date works completely fine however, the data on my dropdowns and textarea wont appear.
I still can update it, the only problem here is it just won't appear. So I'm hoping if anybody can help me and see what I missed in my coding.
How my form looks like:
Solution:
I've found the solution to this question and below is my new html coding. There a few php coding that I edited at the dropdown and the dates.
Updated html code:
php code that are outside from my html:
<?php
session_start();
require('db.php');
include("auth.php");
$noID=$_REQUEST['noID'];
$query = "SELECT * from daftartempah where noID='".$noID."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
php code that are inside my html:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$noID=$_REQUEST['noID'];
$trn_date = date("Y-m-d H:i:s");
$pemohon =$_REQUEST['pemohon'];
$trkhMula = $_REQUEST['trkhMula'];
$trkhAkhir =$_REQUEST['trkhAkhir'];
$n_program = $_REQUEST['n_program'];
$lokasi =$_REQUEST['lokasi'];
$n_anjuran = $_REQUEST['n_anjuran'];
$catatan = $_REQUEST['catatan'];
$status_hr = $_REQUEST['status_hr'];
$submittedby = $_SESSION["username"];
$update="update daftartempah set trn_date='".$trn_date."', pemohon='".$pemohon."', trkhMula='".$trkhMula."', trkhAkhir='".$trkhAkhir."', n_program='".$n_program."', lokasi='".$lokasi."', n_anjuran='".$n_anjuran."', catatan='".$catatan."', status_hr='".$status_hr."', submittedby='".$submittedby."' where noID='".$noID."'";
mysqli_query($con, $update) or die(mysqli_error());
$status = "Record Updated Successfully. </br></br>
<a href='Page8.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
Dropdowns:
<div class="form-group row text-left">
<label for="example-text-input" class="col-3 col-form-label"><b>Dept/Kelab/Anjuran: </b></label>
<div class="col-8">
<select class="form-control" name="n_anjuran" id="namaAnjuran">
<option selected name="" value="<?php echo $row['n_anjuran'];?>"><?php echo $row['n_anjuran'];?></option>
<?php
$sql = mysqli_query($con, "SELECT kd_dept, desc_dept From koddept");
$rows = mysqli_num_rows($sql);
while ($rows = mysqli_fetch_array($sql)){
echo "<option value='". $rows['kd_dept'] ."'>" .$rows['desc_dept'] ."</option>" ;
}
?>
</select>
</div>
</div>
Textarea:
<div class="form-group row text-left">
<label for="exampleTextarea" class="col-3 col-form-label"><b>Catatan: </b></label>
<div class="col-8">
<textarea class="form-control" rows="3" name="catatan" required><?php echo $row['catatan'];?></textarea>
</div>
</div>
My new form:
All questions are welcome, feel free to comment and ask, I'll try to help.
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>
Below is the code where i created a drop down list, i am able to retrieve data from MySQL but i have to repeat the option step for every field, how can I setup a code for the drop down so i can just shorten to call it once in selection list, like in a for loop..also how do i create a error message to display if there is no selection, and if i select a data from the list it stores in the database
<form action="sign.php" method="post">
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<label for="manager">manager</label>
<select name = "manager">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<label for="senior">Senior</label>
<select name = "Senior">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<input type="submit" name="register" value=" Click to Add"></button>
</form>
Use array and run query once to set options in array.
<?php
$optionArray = array();
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$optionArray[] = '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
?>
<form action="sign.php" method="post">
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?php
foreach($optionArray as $row)
{
echo $row;
}
?>
</select>
how can I setup a code for the drop down so i can just shorten to call it once in selection list, like in a for loop.
Just create a loop. though it should be foreach, not for.
First collect your data,
$people = $pdo->query('Select name from people order by name')->fetchAll(PDO::FETCH_COLUMN);
$sections = [
'employee_name' => 'Employee name',
'manager' => 'Manager',
'senior' => 'Senior',
];
and then loop it over
<form action="sign.php" method="post">
<?php foreach ($sections as $label => $caption): ?>
<label for="<?=$label?>"><?=$caption?></label>
<select name = "<?=$label?>">
<option value=""> -----------Select----------- </option>
<?php foreach ($people as $name): ?>
<option><?=$name?></option>
<?php endforeach ?>
</select>
<?php endforeach ?>
<input type="submit" name="register" value=" Click to Add"></button>
</form>
Instead of repeating this every time you can create a method which performs the same task.
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?=loadOptions('name','people');?> <!--shortcut way to echo in php-->
</select>
<?php
function loadOptions($field,$table){
$stmt = $pdo->prepare('Select '.$field.' from '.$table);
$stmt->execute();
$options='';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$options .= '<option>'.$row['name'].'</option>';
}
return $options;
}
?>
hope you fine and well,
i have a drop down list that contains categories list as follows:
<div class='form-group'>
<br/>
<label class='control-label col-md-2 'for='id_date'>Category</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
</div>
</div>
below this select, i have another select which is to choose element from the category as follows :
<div class='form-group'>
<br/>
label class='control-label col-md-2 ' for='id_date'>element</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control" id="sel12" ng-model="elemnt" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT element FROM elements where category = ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['element'] . "'>" . $row['element'] . "</option>";
} ?>
</select>
</div>
</div>
how i can make the content of the second drop list to be based on the first drop list ?! e.g how i can put the input of the first drop list in the second SQL statement ?!
regards.
You can't do that with only php.
You must use javascript/Jquery and ajax.
Make a php script who load data from a request.
After change your first select use ajax function who call your php script with the right value and update the second select.
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
Jquery
$("#sel1").change(function(){
$.ajax({
method: "POST",
url: "yourscript.php",
data: {myval : $(this).val()};
})
.done(function( msg ) {
//Here append your result in your second select
});
});
PHP
<?php
if(isset($_POST['myval']))
{
//SQL query where id=myval
echo $result;//result of query
}
Using pure PHP with intermediate submit:
<?php
if(isset($_POST['submitForm']) && $_POST['submitForm'] == 1){
//form is submitted by button, proceed with DB stuffs
echo 'Great, you have submitted the form, will check VALUES and do INSERT.';
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="aForm">
<input type="hidden" name="submitForm" id="submitForm">
<select name="category" onchange="this.form.submit();">
<option value="">Choose...</option>
<option value="1" <?=($_POST['category']==1 && !$_POST['submitForm'])?'selected':'';?>>Cat 1</option>
<option value="2" <?=($_POST['category']==2 && !$_POST['submitForm'])?'selected':'';?>>Cat 2</option>
<option value="3" <?=($_POST['category']==3 && !$_POST['submitForm'])?'selected':'';?>>Cat 3</option>
</select>
<select name="element">
<?php
if($_POST['category'] && !$_POST['submitForm']){
// SELECT from DB based on passed category ID
echo '<option value="">Now choose element...</option>';
echo '<option value="1">Elem 1</option>';
echo '<option value="2">Elem 2</option>';
echo '<option value="3">Elem 3</option>';
}else{
echo '<option value="">Choose category first...</option>';
}
?>
</select>
<input type="button" name="btnSubmit" value="Submit" onclick="document.getElementById('submitForm').value = 1; this.form.submit();">
</form>
</body>
However this is not either the best or most efficient approach, the script only demostrates how can be done without JS as is requested.