I have done google searches and found several answers but I cannot get this to work. I am trying to read a field from ym mysql database and create and populate a combo box containing those values. The database connection works but all I get is a list of the values and no combo box. I am posting my code, along with the results.
<?php
$cn=mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("db_name",$cn) or die(mysql_error());
$sql = "SELECT LoadNumber FROM tblLoads";
$result = mysql_query($sql);
?>
<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>
Here is my output:
1637825
1637933
1638102
1638109
1638574
1638683
>
Please, this is driving me crazy, i don't see what I am doing wrong. it does not create the combo box.
You are complicating your code! Some part of it is wrapped in PHP and part of it is not.
Change this...
<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>
To this...
<select name="loads" id="loads">
<?php while($ri = mysql_fetch_array($result)) { ?>
<option value="<?php echo $ri['LoadNumber'] ?>"><?php echo $ri['LoadNumber'] ?></option>
</select>
<?php } ?>
Finally... you should stop coding in mysql_ !!! It has been deprecated and will soon cease to exist. When that happens, all of your sites using mysql_ code will stop functioning.
You should really be learning pdo_mysql
Try this.
<select name="loads" size=1>
<?php while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";?>
<select>
just move the last select from the php script.
Related
This is the code I've got so far:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
I'm trying to make it so the dropdown appears blank at first instead of showing an option pulled from the database. (all connection etc is above)
Alex answered this question for you. Just add <option>Select...</option> before the loop (Change Select... to white space if you really want it to show a "blank" option).
Here is the code:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<option>Select...</option>
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
I am trying to fetch values from my database and display the selected value in a dropdown list with other values. I have seen many questions here regarding this issue but none of them is working. Please Help!
<select name= "indication_name" id= "update_indication_id" class="form-control" required>
<?php
$sql = "SELECT id,description From indications";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result)){
echo "<option selected='selected'>" . $row['id']." - ". $row['description'];
}
?>
</select>
There are three main errors I can see:
You're making every option selected like that.
The options don't have a value.
And the option tags should be closed.
So it would be like this:
<select name= "indication_name" id= "update_indication_id" class="form-control" required>
<?php
$sql = "SELECT id,description From indications";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
echo "<option value='" . $row['id'] . "'>" . $row['description'] . "</option>";
?>
</select>
Another thing, that isn't an error, but I personally think is good, is having a "unselected" option:
<select name= "indication_name" id= "update_indication_id" class="form-control" required>
<option selected="selected" value="">-- Select an option --</option>
<?php
$sql = "SELECT id,description From indications";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
echo "<option value='" . $row['id'] . "'>" . $row['description'] . "</option>";
?>
</select>
The value of that option should be null, so that required applies to it.
<?php
$server="localhost";
$username="root";
$password="";
$link=#mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".#mysql_error());
$dbname = 'database_name';
#mysql_select_db($dbname, $link) or die ("Cannot connect to database: ".#mysql_error());
$query="SELECT id,description from indications";
$result = #mysql_query ($query);
echo "<select name=indication_name value=' '>";
while($drop=#mysql_fetch_array($result)){
echo "<option value=$drop[id]>$drop[description]</option>";
}
echo "</select>";
?>
Try using switch case and put the value in switch from the database and then set selected ='selected' if the case value matches.
I am fresher in php & mysql.
I have a from where there are 2 dropdwon's. The value in dropdwon are coming via mysql. But now i am not able to get the value in 2nd dropdown dependent on value selected in 1st dropdown.
Here is the code i have tried so far.
Please Help!
Thank You
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn=mysqli_connect('localhost','root','');
$db="sample";
mysqli_select_db($conn,$db);
$sql="SELECT DISTINCT(Site) FROM `bom`";
$result=mysqli_query($conn,$sql);
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while($row=mysqli_fetch_array($result))
{
echo "<option value='".$row['Site']."'>".$row['Site']."</option>";
}
echo "</select>";
$sql1="SELECT BOM Desc FROM `bom` where Site= ";
$result1=mysqli_query($conn,$sql1);
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while($row1=mysqli_fetch_array($result1))
{
echo "<option value='".$row1['BOM Desc']."'>".$row1['BOM Desc']."</option>";
}
echo "</select>";
?>
</body>
</html>
Hi Your fixed code here:
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$db = "sample";
mysqli_select_db($conn, $db);
$sql = "SELECT DISTINCT(Site) FROM `bom`";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); // add error show info
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Site'] . "'>" . $row['Site'] . "</option>";
}
echo "</select>";
$sql1 = "SELECT BOM Desc FROM `bom` where Site IS NULL "; // change the for null site
$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn)); // add error show info
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while ($row1 = mysqli_fetch_array($result1)) {
echo "<option value='" . $row1['BOM Desc'] . "'>" . $row1['BOM Desc'] . "</option>";
}
echo "</select>";
?>
</body>
</html>
And if you need load on second select php only cant handle this because you must give time to user for check first select.
I think the better way is using Ajax request for second:
Auto Load Second Dropdown using AJAX
I suggest to use in both <select> the clause selected to show the selected <option>.
Then, in the first <select> add the on change event to launch a page refresh so that:
The selected entry is recognised as selected
The SQL for the second drop-down can be filtered on the selected entry in first dropdown
The first select should appear then like this:
<select onChange='if(options[selectedIndex].value) { location=options[selectedIndex].value;}' size='1'>
<option value='?site=Plant1'>Plant ONE</option>
<option value='?site=Plant2' selected>Plant 2</option>
</select>
When Plant 2 is selected, the page will be refreshed with the URL containing the parameter &site=Plant2 which you can read in the variable $_REQUEST['site'] to be used in the 2nd SQL query
Is it possible to insert drop-down-list into table cell?
Thank you!
Here is some of the code:
$sql_query="SELECT nameOfwarning FROM warnings";
$sodss=mysqli_query($d,$sql_query);
if (mysqli_num_rows($result)<1)
?>
<select name = "warnings">
<option value="Choose one of the warnings: " </option>
<?php
while ($row = mysqli_fetch_array($warnings)) {
print "<option value=";
print $row["id_warning"];
print ">" ;
print $row["warning"];
print "</option>";
}
?>
</select><br>
<?php
print "<tr><td>Insert drop down list here?: </td><td><input type=\"text\" name=\"OR HERE\"></td></tr>";
I took the liberty to also format your code. Hope you don't mind. You should do that as a general practice to make it more readable.
<?php
$sql_query = "SELECT id_warning, warning FROM warnings";
$result = mysqli_query($d, $sql_query);
$dropDownHtml = '';
if (mysqli_num_rows($result) > 0) {
$dropDownHtml .= '<select name = "warnings">';
$dropDownHtml .= '<option value="Choose one of the warnings: " </option>';
while ($row = mysqli_fetch_array($result)) {
$dropDownHtml .=
'<option value="{$row["id_warning"]}">' .
$row["warning"] .
'</option>';
}
$dropDownHtml .= '</select><br>';
}
?>
<tr>
<td><?php echo $dropDownHtml; ?></td>
<td><input type=\"text\" name=\"OR HERE\"></td>
</tr>
It looks like you skipped some of the code, because what the SQL query should return doesn't match with the results array, also there is some variable mismatching. I changed it a bit to look believable, you should make sure you adapt it back to your specific case if you don't want to post all of your code in the question.
I have two dropdown boxes and I want the user to choose values from both.
The dropdown boxes are filled with results of a query on page load. I want these values to then be stored as a session variable for use in subsequent queries.
First file = choose.php
<html>
<form action="choose2.php" method="post">
<?//db connection stuff taken out from here
}
// fill classes
$result = mysqli_query($con,"SELECT * FROM classes");
echo "<select id='classlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['classname'] . "</option>";
}
echo "</select>";
//fill schemes of work
$result = mysqli_query($con,"SELECT * FROM schemes_of_work");
echo "<select id='sowlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit">
</form>
</html>
second file = choose2.php
<?php session_start();
$_SESSION['classname'] = $_POST["classlist"];
$_SESSION['sowname'] = $_POST["sowlist"];
?>
<?php echo $_SESSION['classname'];?>
<?php echo $_SESSION['sowname'];?>
I cant get this to work though - I am getting an empty page on choose2 and the following error in apache log:
"Undefined index: sowlist in /var/www/assessment/choose2.php on line 3,
The select tag should have a name attribute to be able to use it in php POST.
Try
<select id='classlist' name="classlist">
The same for the other one.