Reputation: 25
I'm trying hard with displaying details from a selected option in a drop down list for my project.
I have a drop down list which is populated from a MYSQLi query. I want the user to select an option and the values associated pulled from the database and displayed to the user.
The dynamically populated drop down list is for "FirstName" of persons(Table Name) and when a user selects a name from the drop down list I want the record of that person to be to be displayed.
The code below is for dynamically populating the drop down list. The user clicks the button and goes on the next page which should create a table with the results. There is no Error, but no result as required too.
drop down code
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<form name="form_update" method="post" action="update_test.php">
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//This creates the drop down box
echo "<select name= 'FirstName'>";
echo '<option value="">'.'--- Please Select Person ---'.'</option>';
$query = mysqli_query($con,"SELECT FirstName FROM persons");
$query_display = mysqli_query($con,"SELECT * FROM persons");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['id']."'>".$row['FirstName']
.'</option>';
}
echo '</select>';
?> <input type="submit" name="submit" value="Submit"/>
</form><br/><br/>
<a href="main.html"> Go back to Main Page </a>
</body>
</html>
View Code
<title>Update Data</title>
</head>
<body>
<!--<table>
<tr>
<td align="center"> From Database </td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td>First Name</td>
<td>Last Name </td>
<td> Gender </td>
<td> Subject </td>
<td> Hobbies </td>
</tr>
-->
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//$name = mysqli_real_escape_string($con,$_POST['select']);
// $fetch = mysqli_query($con,"SELECT * FROM persons WHERE FirstName='".$name."'");
// $row_display=mysqli_fetch_assoc($fetch);
if(isset($_POST['select']))
{
$name = mysqli_real_escape_string($con,$_POST['select']);
$fetch = "SELECT * FROM persons WHERE FirstName = '".$name."'";
$result = mysqli_query($con,$fetch);
//display the table
echo '<table border="1">'.'<tr>'.'<td align="center">'. 'From Database'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td>'.'First Name'.'</td>'
.'<td>'.'Last Name'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'
. 'Subject'. '</td>'.'<td>'. 'Hobbies' .'</td>'.'</tr>';
while($data = mysqli_fetch_row($result))
{
echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td><td>$data[4] </td></tr>");
}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
<!--
</table>
</td>
</tr>
</table>
-->
<br/>
<a href="update.php"> Go back to Main Page </a>
</body>
</html>
Upvotes: 0
Views: 15173
Reputation: 1574
It display data of particular user to same page
Your Drop down box looks like
<select id="c-name" name="name" onChange="reload()">
<option value="">Select Name</option>
</select>
In Javascript write reload() function
function reload() {
var val=document.getElementById("c-name").value;
self.location="yourfilename.php?fname=" + val ;
//alert(val);
}
On ther page fetch fname variable & display data for that variable that is fetch from database
In PHP
if(isset($_REQUEST['fname'])) {
//your code
}
Upvotes: 0
Reputation: 1
There are few suggestions
In your query add the id which you have set as the value for the options
echo "<option value='". $row['id']."'>".$row['FirstName']
so update it as follows:
$query = mysqli_query($con,"SELECT id,FirstName FROM persons");
You are checking for the wrong POST var 'select'
, it should be 'FirstName' which is the name of the select field that you have used below:
echo "<select name= 'FirstName'>
so instead of the following code:
if(isset($_POST['select']))
{
$name = mysqli_real_escape_string($con,$_POST['select']);
$fetch = "SELECT * FROM persons WHERE FirstName = '".$name."'";
$result = mysqli_query($con,$fetch);
Also note that in the where clause you should check for the id not the firstName
use this corrected code instead :
if(isset($_POST['FirstName']))
{
$name = $_POST['FirstName'];
$fetch = "SELECT * FROM persons WHERE id = '".$name."'";
$result = mysqli_query($con,$fetch);
Upvotes: 0
Reputation: 360572
You're using the wrong fieldname in your view code:
echo "<select name= 'FirstName'>";
^^^^^^^^^
v.s.
$name = mysqli_real_escape_string($con,$_POST['select']);
^^^^^^
Forms are submitted with fieldname=value
pairs, NOT the name of the tag that the input came from.
Upvotes: 2