mysql php jquery hide show select - php

I know a little bit of PHP programming, but none of JQuery.
I have build a <select> dropdown menu with data from MySql db.
But now I want a button or something next to the <select> option, like a + sign or something that will give me a extra <select> option. And that this can come as many time as you want.
Ps. I'm sorry for my English.
Here is a picture of how I want this to be.
and then next you will get this.
And here is the code what I already have made.
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database')
or die ('Cannot connect to db');
$result = $conn->query("select id, name from table");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>

You can use check box instead of dropdown for multiple selection
while ($row = $result->fetch_assoc()) {
unset($id, $name);//use if you want
$id = $row['id'];
$name = $row['name'];
echo '<input name="city[]" type="checkbox" value="'.$name.'" />'.$name;
}

Related

<select> accessing values with $_POST

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.

Populate dropdown from database and set default value

Right now I have a working solution for populating an HTML <select>/<option>-dropdown with the content through PHP/MYSQLI from my database: listoption.
The database:
DATABASE NAME:
# listoption
TABLES:
# ID INT(11) *Primary AI
# listoption_item VARCHAR(255)
Here's the other code (not the mysqli connect but everything afterwards..)
<?php
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
But the problem is now that I want to have one of these options that are populated through that query to be selected. And the option to be selected should be determed by a parameter in the URL, for example: index.php?id=1.
So now I need to somehow add a IF/ELSE and a $_GET['id']; into the code to make it identify if the ID from the database is the same as the populated item and then set it to selected.
Any idéas? Thanks!
You can do that like given below:
<?php
$result = $mysqli->query("select * from listoption");
$id = ($_GET['id'])? $_GET['id'] : '';
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
$sel = ($id == $row['id'])? 'selected="selected"':'';
echo '<option value="'.$listoption_item.'" '.$sel.'>'.$listoption_item.'</option>'; // $sel will deside when to set `selected`
}
echo "</select>";
?>
You can rewrite the code as follows:
<?php
$id = $_GET['id'];
$select = "";
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$row_id = $row['ID'];
if($row_id == $id){
$select = "selected";
}
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'" selected="'.$select.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
Use the following code:-
<?php
$selectedId = isset($_GET['id'])?$_GET['id']:0;
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.' .(($selectedId>0)?:" selected ":"").'">'.$listoption_item.'</option>';
}
echo "</select>";
?>

match dropdown from retrieved result from mysql

I have an error here, the data retrived from mysql database and listed in listbox or dropdown, when i need to update my form i get the result after refresh.
I have tried but need to sortout
<?
include("connect.php");
mysql_select_db("joblisting") or die(mysql_error());
$result = mysql_query("SELECT * FROM positiontitle") or die(mysql_error());
$ans=$positiontitle;
echo $ans;
?><select size=1 name="positiontitle" id="positiontitle"><?
echo '<option value="'.$ans.'">';
echo "</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>";
echo $row['positiontitle'];
echo "</option>";
}
echo "<br />";
?>
where are you defining $positiontitle ? shouldn't it be $_POST['positiontitle']?

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