<select> accessing values with $_POST - php

I am little confused about this because everywhere I look for it, they have it same as me, but I have problem that I am listing various of options by <select> and <option> and I need to get back the value of what I have clicked, by it doesn't seem to be working, so if there is someone who knows?
$connect = mysqli_connect("localhost", "root", "", "uklidy");
$sql = "SELECT * FROM budovy ORDER BY id ASC";
$result = mysqli_query($connect, $sql);
echo '<select name="budova_name">
';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["id"].'">'.$row["jmeno"].'</option>';
}
echo '</select>';
$budova = $_POST["budova_name"];
echo $budova;

You need to submit it with a form.
<?php
$connect = mysqli_connect("localhost", "root", "", "uklidy");
$sql = "SELECT * FROM budovy ORDER BY id ASC";
$result = mysqli_query($connect, $sql);
echo '<form method="post" action="mypage.php">';
echo '<select name="budova_name">';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["id"].'">'.$row["jmeno"].'</option>';
}
echo '</select>';
echo '<input type="submit">';
echo '</form>';
and if you're talking about getting the selected value on the same page you're gonna have to use javascript for that. PHP is a server side language.

Related

Search in Select List

Currently creating this project and having problems on how to create a search in select list. I would like to search from option values. how can i create this kind of search on select list?
<?php
error_reporting(0);
//connect to database
$conn = mysqli_connect("localhost", "root", "", "waterdis_stbwd");
if(mysqli_connect_errno($conn)) {
echo "Unable to connect to database server";
}
//query database for items to populate
$sql = "SELECT id FROM usrs";
$query = mysqli_query($conn, $sql);
echo '<select id="slct">';
echo '<option value="">Select ID</option>';
while($id = mysqli_fetch_assoc($query)){
echo "<option>{$id['id']}</option>";
}
echo '</select>';
?>
You will want to use a plugin, like select2
https://select2.github.io/examples.html
https://select2.github.io/
<?php
error_reporting(0);
//connect to database
$conn = mysqli_connect("localhost", "root", "", "waterdis_stbwd");
if(mysqli_connect_errno($conn)) {
echo "Unable to connect to database server";
}
//query database for items to populate
$sql = "SELECT id FROM usrs";
$query = mysqli_query($conn, $sql);
echo '<select id="slct">';
echo '<option value="">Select ID</option>';
while($id = mysqli_fetch_assoc($query)){
echo "<option value="{$id['id']}">{$id['id']}</option>";
}
echo '</select>';
?>

Update mysql function to mysqli

I would like to update this code to be mysqli, but dont know where to begin.
I know that the connection is handled like this , buts thats as far as iv got.
$connection = mysqli_connect('localhost', 'admin', 'admin', 'database_name');
Function to be updated:
$cn=mysql_connect('localhost', 'admin', 'admin') or die(mysql_error());
mysql_select_db('database_name',$cn) or die(mysql_error());
$sql = "SELECT name FROM category";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
}mysql_free_result($rs);
echo "</select>";
<?php
$sql = mysqli_query($connection, 'SELECT name FROM category');
echo "<select>";
while($row = mysqli_fetch_array($sql)){
echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
}mysql_free_result($sql);
echo "</select>";
?>
$cn=mysqli_connect('localhost', 'admin', 'admin','database_name') or die(mysqli_error($cn));
$sql = "SELECT name FROM category";
$rs = mysqli_query($cn,$sql) or die(mysqli_error($cn));
echo "<select>";
while($row = mysqli_fetch_array($rs)){
echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
}mysqli_free_result($rs);
echo "</select>";
You can learn about mysqli on PHP's website Mysqli Book
But for security and other benefits you should now use PDO instead

How to create select option from database?

I'm working on a form that has 4 different select elements from 2 tables of a database. I haven't done anything like this and I don't really know how to do it.
I have a table called "students" from I need "name" and "class" and a table called "books" from I need "writer" "title" ... all is one select element and all has more than 2 option values.
I've tried with only one sql query and one select but it shows only one option on the site, wether it has about 6 values in the database.
My code:
$sql = "SELECT class
FROM students";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
}
<select id="class" name="class">
<?php print $select_class; ?>
</select>
How would it be correct?
try this
$sql = "SELECT class FROM students";
$result = mysql_query($sql);
echo '<select id="class" name="class">';
while ($row = mysql_fetch_assoc($result)) {
echo "<option value={$row['class']}>{$row['class']}</option>";
}
echo '</select>';
You are overwriting $select_class on each while() loop. You need to concatenate $select_class . Change to $select_class .=
$select_class = "";
while ($row = mysql_fetch_assoc($result)) {
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
}
Changing this:
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
to this:
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
might solve your problem.
Right now you are constantly resetting the value of $select_class, instead of adding to it. The .= assignment should help you get around this.
As always, be sure to up-vote any StackOverflow answers you find useful.
Try this
<?php
$dbhandle = mysql_connect($hostname, $username, $password) or die("can't connect");
$table = "students";
$sql = "SELECT * FROM students";
$result = mysql_query($sql, $dbhandle);
mysql_data_seek($result, 0);
?>
<form>
<select class="dropdown" name="dropdown">
<?php
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['class'] . '">' . $row['class'] . '</option>';
}
}
?>
</select>
</form>

Edit drop down but not showing selected value from mysql data base in php

I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.

Populate a Drop down box from a mySQL table in PHP

I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)
Here's my Query: Select PcID from PC;
You will need to make sure that if you're using a test environment like WAMP set your username as root.
Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);
echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";
?>
Below is the code for drop down using MySql and PHP:
<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q))
{
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>";
}
echo "</select>";
?>
Since mysql_connect has been deprecated, connect and query instead with mysqli:
$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);
And then, if you have more than one option list with the same values on the same page, put the values in an array:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
And then you can loop the array multiple times on the same page:
foreach ($rows as $row) {
print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}
No need to do this:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
You can directly do this:
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
}
At the top first set up database connection as follow:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?>
Then include the following code in HTML inside form
<select name="selected_pcid" id='selected_pcid'>
<?php
while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
$value= $rows['id'];
?>
<option value="<?= $value?>"><?= $value?></option>
<?php } ?>
</select>
However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.
After a while of research and disappointments....I was able to make this up
<?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>
//insert the below code in the body
<table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
<th style="width:20%;">Email</th>
<th style="width:10%;">City/ Region</th>
<th style="width:30%;">Details</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['details']."</td>";
echo "</tr>";
}
?>
</table>
Trust me it works :)
What if you want to use both id and name in the dropdown? Here is the code for that:
$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);
echo "<select id='brandId' name='brandName'>";
while ($row = mysqli_fetch_array($result)) {
unset($id, $name);
$id = $row['BrandID'];
$name = $row['BrandName'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";

Categories