Set default option in php dropdown and retain when submitted query - php

I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";

i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}

Related

I want the default value of my select tag to be a php session [duplicate]

I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}

showing results obtained after $_POST in a new page

I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}

I need some assistance with using a specific php function within an HTML webpage

I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";

Get variable from another table in a WHILE loop

<?php
$id = $_SESSION['user_id'] ;
echo "<form method='post' action='#'>";
echo "</select>
<p>Which Hospital Would You Like to Submit To?</p>";
$queryitem = "SELECT * FROM vendor_hospital WHERE vendor_hospital.user_id = '$id' AND vendor_hospital.approval_status = '1'" or die('MYSQL error: ' . mysql_error());
if ($result = mysql_query($queryitem)) {
if ($success = mysql_num_rows($result) > 0) {
echo "<select name='hospital_name'>";
echo "<option>-- Select A Facility --</option>";
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
} else {
echo "No results found.";
}
} else {
echo "Failed to connect to database.";
}
echo "<input type='submit' value='Submit' name='submit' class='button' /></form>";
?>
For some reason I'm stuck here. I'm just trying to get the manufacturer name to show in my options instead of the manufacturer_id. The manufacturer name is a foreign key in another table so I can't simply call $row[manufacturer_id] in my option tag. What should I do here? My only thought is to run a query inside the option tag for every manufacturer_id listed as a value but I'm sure that is overkill. Can someone point me in the right direction of a more elegant solution than that?
Not really sure what you are trying to do...but try this.
Replace this:
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
With this:
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row['manufacturer_id']."'>".$row['manufacturer_id']."</option>";
}
echo "</select><br><br>";
Also be sure to double check and make sure that you are pulling from the right database, that it is populated, you are calling the right table names, etc...

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']?

Categories