Results 1 to 2 of 2

Thread: DB info pulled into dropdown - trouble including delete function

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default DB info pulled into dropdown - trouble including delete function

    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.

    Code:
    <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.

  2. #2
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Resolved

    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.

Similar Threads

  1. Trouble Including an External .js file
    By Alice Moore in forum JavaScript
    Replies: 2
    Last Post: 07-14-2010, 08:28 AM
  2. Navigational Menu - how to delete a function?
    By thundercode in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 11-23-2009, 10:40 AM
  3. Delete Function Error
    By Titan85 in forum PHP
    Replies: 17
    Last Post: 09-19-2008, 03:28 PM
  4. including a function from another file.
    By killerchutney in forum PHP
    Replies: 12
    Last Post: 11-12-2007, 05:04 PM
  5. PHP Delete Function
    By Titan85 in forum PHP
    Replies: 19
    Last Post: 10-18-2006, 09:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •