Thursday, November 14, 2019

php - get all mysql row data after selecting name from dropdown



Below is the php code I'm using to populate a select dropdown with the variable "full_name".



$sql = "SELECT * FROM Entries";
$result = mysql_query($sql);

echo "";


I would like to be able to select a name from the dropdown list and display all the relevant data for that specific row so it can be easily seen by the admins.



Basically



select name (details populate below)


Name: $full_name
DOB: $dob
Work Phone: $work_phone
etc...


Maybe something like this?



while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo "Name :{$row['full_name']}
".
"DOB : {$row['dob']}
".
"Work Phone : {$row['work_phone']}
".
"--------------------------------
";
}
echo "Fetched data successfully\n";


I just don't know how to tie the dropdown selection to correctly display the information for that person's name. Any help is appreciated!




Thanks













Untitled Document





';

echo $info; /* DISPLAY THE INFOs, BUT NOT REALLY BECAUSE THEY ARE HIDDEN */
?>







id
requested_action
full_name
birth_date
sex
work_location
work_phone
hire_date

coverage_choice
network_choice
plan_choice
dependant_name_1
dependant_relationship_1
dependant_dob_1
dependant_sex_1
dependant_name_2
dependant_relationship_2
dependant_dob_2

dependant_sex_2
dependant_name_3
dependant_relationship_3
dependant_dob_3
dependant_sex_3
dependant_name_4
dependant_relationship_4
dependant_dob_4
dependant_sex_4
spouse_coverage

employee_enroll
signature
date_today
reg_date







Answer



You can achieve this using Javascript. But we will be using a Javascript library called jQuery.



Oh, and don't use deprecated mysql_* extension. We will be using mysqli_* extension instead.



Let's prepare first your HTML by adding an id tag on your field.



echo '';

echo $info; /* DISPLAY THE INFOs, BUT NOT REALLY BECAUSE THEY ARE HIDDEN */


Then, let's create the script to display the information of selected full_name:



 



This is just a simple trick. But I think it is better than doing an Ajax call from every select of full_name.






Establish your connection to your database using mysqli_* extension also:




$connection = new mysqli("Host", "User", "Password", "Database");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...