I have dropdown to edit a form.
All ISBN records are listed in dropdown. The user will select the ISBN that he want to update. Now how to get which ISBN record was selected by that user?
My logic of dropdown is as follows:
function update()
{
$select_query="Select ISBN from book";
$select_query_run = mysql_query($select_query);
echo"<form method='post' action='update.php'><center>Select ISBN you want to Update: ";
echo "<select name='isbn' id='isbn'>";
while ($select_query_array=mysql_fetch_array($select_query_run) )
{
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
}
echo "</select>";
echo '<br><br><br><input type="submit" value="Update"></center></form>';
} After selecting ISBN the user will be navigated to update php page whoch is as follow:
<?php
$isbn=$_POST['isbn'];
echo "ISBN Selected: ".$isbn;
?>
Output of update page:
ISBN Selected:
Because your value is empty in select box
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
^^
You need to add value in your select box
echo "<option value='".htmlspecialchars($select_query_array['ISBN'])."' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
For this you should try with this:
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
Instead of this:
echo "<option value='".htmlspecialchars($select_query_array['ISBN'])."' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
Related
<select name= "NEE_category">
<?php
$sql = mysqli_query($dbConn, "SELECT catDesc FROM NEE_category");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"NEE_category\">" . $row['catDesc'] . "</option>";
}
?>
</select>
I want to echo the selected option in PHP however using this:
$NEE_category = isset($_REQUEST['NEE_category']) ? $_REQUEST['NEE_category'] : null;
echo "\t<p>Event Category: $NEE_category</p>\n";
Returns: Event Category: NEE_category
How do i return the selected option value and not the name?
You're setting the value to "NEE_category" instead of the values you're pulling from your database.
echo "<option value=\"". $row['catDesc'] ."\">". $row['catDesc'] ."</option>";
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
$x = date("");
function add_nol($number,$add_nol)
{
while (strlen($number)<$add_nol)
{
$number = "0".$number;
}
return $number;
}
for($y=10;$y<=50;$y++)
{
echo "<select name='id'>";
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
echo "</select>";
}
I want to add auto increment numbers into select tag, but I am getting drop down for each numbers from the above code. How to fix this error?
Two things to remember:
The <select> element is used to create a drop-down list.
The <option> tags inside the <select> element define the available options in the list.
With your current code, the generated HTML markup would be similar to this:
<select name='id'>
<option value='010'>010</option>
</select>
<select name='id'>
<option value='011'>011</option>
</select>
...
This is incorrect. You are creating a separate dropdown on each loop iteration. You only need one <select>tag - they should go outside the loop.
echo "<select name='id'>";
for($y=10;$y<=50;$y++)
{
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
}
echo "</select>";
Demo
You are repeatedly creating the select tag inside the loop , thats y the select boxes are created instead of option tags. Edit the for loop as:
echo "<select name='id'>";
for($y=10;$y<=50;$y++){
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
}
echo "</select>";
I need your advice. I fetch data from database to table: ID, Name.
In table are Actions: Delete, Enable, Block. When action Delete
is selected, I would like, that respectively record will be deleted.
However, my script does not work and always delete last record, even I select
another record. I think problem is, that select name and hidden input
name is similar for all records. But I can not find way, how to create
them with different names.
Any advice is welcome.
HTML:
<form method='post'>
<table border='1'>
<tr>
<th> ID </th>
<th> Name </th>
<th> Action </th>
</tr>
Code:
$db = new PDO('mysql:host=localhost;dbname=****;charset=utf8', '**', '**');
$query = $db->query("SELECT ID,statusas,login,vardas,email FROM users");
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
}
echo "<br><input type='submit' name='submit'></table></form>";
if($_POST['submit']) {
if ($_POST['action']== 'delete') {
echo $_POST['id']; // delete query, but now I am just checking if I get a proper ID.
}
}
else {
echo "bad";
}
You are using the same name attribute on every row in the form, so they are being overridden and it's using the last one.
What you could do is either wrap every row in its own form, or you could do something like this, and have only 1 submit button to execute the action on every row:
// remove hidden id element
...
echo "<td><select name='action[" . $row[0] . "]'>"
...
// remove submit button in the loop, but add it after the while loop
...
if (isset($_POST['action']))
{
foreach ($_POST['action'] as $id => $action)
{
if ($action !== 'choose')
{
// do action on the id;
echo $id . " -> " . $action . "<br>";
}
}
}
You put every ID in the form, which results in the CGI to send something like this: id=1, id=2, id=3, etc. PHP then only reads the last ID and deletes that.
To fix it, give each row its own form.
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<form method='post'>";
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
echo "</form>";
}
echo "<br><input type='submit' name='submit'></table>";
I have two comboboxes which are generated by queries from mysql.
When user choose value in first combobox i need to display second combobox, but if nothing is choosen second combox should not be displayed.
The code:
<?php
echo "<form method= \"post\" name=\"formcombo\" action=''>";
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Choose main category</option>";
while($cat2 = mysql_fetch_array($query2)) {
if($cat2['category_id']==#$category)
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option></br>";}
else
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option>";
}
}
echo "</select></br>";
echo "<select name='subcat'><option value=''>Choose subcategory</option>";
while($cat = mysql_fetch_array($query1)) {
echo "<option value='$cat[subcat_name]'">$cat[subcat_name]</option>";
}
echo "</select>";
echo "<input type=\"submit\" value =\"Submit\">";
echo "</form>";
?>
I use javascript to generate combobox values:
<script type="text/javascript">
function reload(form){
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='main.php?cat=' + val ;
}
</script>
If you are okay with jQuery js library, it is easy.
see this jQuery plugin http://plugins.jquery.com/project/selectchain
try the demo provided in this page and use the plugin to accomplish.