Log in

View Full Version : DB info pulled into dropdown - trouble including delete function



?foru
08-02-2016, 03:00 PM
I have the following code that pulls the Full Name into a dropdown list. Once you select a name it uses jquery to populate all the info for that data row in the database.

I had this coded at the bottom of the "info" variable to go to a separate delete page to delete that specific ID. Due to a recent re-direct the client added I need to combine the delete function all into the same page.


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>

$(document).on("change", "#full_name", function(){

var elem = $(this),
full_name = elem.val();

$(".info").hide(200); /* HIDE ALL OTHER INFORMATION */

$(".info-"+full_name).show(200); /* SHOW THE INFO OF THE SELECTED FULL NAME */

});

</script>
<script>
$(function() {
$('.confirm').click(function() {
return window.confirm("Are you sure?");
});
});
</script>
</head>

<body>
<select name="full_name" id="full_name">
<option selected="selected">Select an Employee</option>
<?php
$connection = new mysqli("host", "user", "pass", "dbname");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$info = ''; /* WE'LL BE STORING THEIR INFORMATION HERE */

$stmt = $connection->prepare("SELECT id, 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, employee_enroll, date_today FROM Entries"); /* SEE HOW TO ESTABLISH CONNECTION TO YOUR DATABASE USING mysqli AT THE BOTTOM */
$stmt->execute();
$stmt->bind_result($id, $fullname, $dob, $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, $employee_enroll, $date_today); /* CORRESPONDS TO THE SELECTED COLUMNS FROM YOUR QUERY */
while($stmt->fetch()){

echo '<option value="'.$id.'">'.$fullname.'</option>';
$info .= '<div class="info info-'.$id.'" style="display:none">
Date of Birth: '.$dob.'<br>
Sex: '.$sex.'<br>
Work Location: '.$work_location.'<br>
Tel. Phone (work): '.$work_phone.'<br>
Hire Date: '.$hire_date.'<br>
Coverage Choice: '.$coverage_choice.'<br>
Network Choice: '.$network_choice.'<br>
Plan Choice: '.$plan_choice.'<br>
Dependant 1 Name : '.$dependant_name_1.'<br>
Relationship to Dependant 1: '.$dependant_relationship_1.'<br>
Dependant 1 Date of Birth: '.$dependant_dob_1.'<br>
Dependant 1 Sex: '.$dependant_sex_1.'<br>
Dependant 2 Name : '.$dependant_name_2.'<br>
Relationship to Dependant 2: '.$dependant_relationship_2.'<br>
Dependant 2 Date of Birth: '.$dependant_dob_2.'<br>
Dependant 2 Sex: '.$dependant_sex_2.'<br>
Dependant 3 Name : '.$dependant_name_3.'<br>
Relationship to Dependant 3: '.$dependant_relationship_3.'<br>
Dependant 3 Date of Birth: '.$dependant_dob_3.'<br>
Dependant 3 Sex: '.$dependant_sex_3.'<br>
Dependant 4 Name : '.$dependant_name_4.'<br>
Relationship to Dependant 4: '.$dependant_relationship_4.'<br>
Dependant 4 Date of Birth: '.$dependant_dob_4.'<br>
Dependant 4 Sex: '.$dependant_sex_4.'<br>
Employee Enroll/Decline: '.$employee_enroll.'<br>
Signature Date: '.$date_today.'<br>
<div><a class="confirm button" style="text-decoration:none; color:#000;" href="?id='.$id.'">Delete this receord</a></div>
</div>';
if( isset($_GET['id'] ) && is_numeric( $_GET['id'] ) ){
mysql_query("DELETE FROM Entries WHERE id=$id");
}
}
$stmt->close();

echo '</select>';
echo $info;
echo $info;/* DISPLAY THE INFO, BUT NOT REALLY BECAUSE THEY ARE HIDDEN */
?>

I highlighted in bold/italics what I added, and as of now the information is shown twice and the DELETE query doesn't execute. The id variable is passed into the "info" variable which displays all the information into a <div>.

I just need help grabbing the id from the info variable and running the DELETE query after the "Delete this record" link is clicked.

Any help would be appreciated, thank you.

?foru
08-02-2016, 05:40 PM
I just used an ajax call to hit the delete.php file and it got around the re-direct that was set on the server.