1

This might be a simple question but have kept me busy for the best part of a hour.

I have the following script which generates all active reservations, in the table reservations

require("../includes/connect.php");
function dispAllReservations(){
    $i= 1;
    global $db;
    $date = date('Y-m-d');
    $sql ="SELECT * FROM
        reservations WHERE pickup_date > '$date'";
    $statement = $db->prepare($sql);
    $statement->execute();
    $bookings = $statement->fetchAll();
    $statement->closeCursor();
    echo'<table cellpadding="0" cellspacing="0" class=
     "table-responsive gzblog-table" id="gzhotel-booking-booking-id">
     <thead>
              <tr>
                <th class="date-th">Reservation ID</th>
                <th class="title-th">Car Group</th>
                <th>Pickup Date</th>
                <th>Return Date</th>
                <th>Customer</th>
                <th>Email</th>
                <th>Amount</th>
                <th></th>
                <th></th>
                <th></th>
               </tr>
       </thead>
        <tbody>';
    foreach($bookings as $booking){
        if($i%2 == 0){
            $class = 'odd';
        }
        else{
            $class = 'even';
        }
        echo' <tr class='.$class.'>';
        echo'<td>'.$booking['res_id'].'</td>';
        echo'<td>'.$booking['car_group'].'</td>';
        echo'<td>'.$booking['pickup_date'].'</td>';
        echo'<td>'.$booking['return_date'].'</td>';
        echo'<td>'.$booking['renter_name'].'</td>';
        echo'<td>'.$booking['email'].'</td>';
        echo'<td>AMOUNT HERE</td>';
        echo'<td><a class="btn btn-primary btn-sm fancybox" href="#email">
        <span class="glyphicon glyphicon-envelope"></span></a>
        </td>';
        echo'<td><a class="btn btn-success btn-sm">
        <span class="glyphicon glyphicon-pencil"></span></a>
        </td>';
        echo'<td><a class="btn btn-danger btn-sm icon-delete">
        <span class="glyphicon glyphicon-trash"></span></a>
        </td>';
        echo'</tr>';
    }//foreach
    echo'</tbody>';
    echo'</table>';
   }//function

The output of the script generates the following

enter image description here

My Problem / Question

When delete button is clicked -- I need to ensure the correct reservation is deleted, same with the edit button, if edit button is clicked I need to ensure the correct user is edited.

What I need to do

  • I'm guessing I would need to find a way to append the corresponding value for each user in the loop to the element.
  • I was thinking of a textbox setting display none and then echoing the value inside the value attribute, but that didn't work...

I'm really stuck here any advise would be appreciated

NOTE: All emails are fictional

3 Answers 3

1

Try this:

<a class="btn btn-primary btn-sm fancybox" href="#email" onclick="onDelete(your-id)">
  <span class="glyphicon glyphicon-trash"></span>
</a>

<a class="btn btn-primary btn-sm fancybox" href="#email" onclick="onEdit(your-id)">
  <span class="glyphicon glyphicon-pencil"></span>
</a>

Create this function:

<script>
    //Delete function
    function onDelete(id){
        $.ajax({
           // do someting
        });
    }

    // Edit function
    function onEdit(id){
        $.ajax({
           // do someting
        });
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You did not tell us at all how do you want to edit/delete you reservations, but based on the tags you used you can take this approach...

1) In your PHP script generate data attributes on rows or cells, like this:

<tr data-id="put_reservation_id_here">

2) And then use jQuery to hook actions to various elements in the table:

$('#gzhotel-booking-booking-id').on('click', '.icon-delete', function() {
   var reservationId = $(this).closest('tr').data('id');
   // now you have reservation ID and do whichever AJAX call with it
});

Comments

0

You can use data attributes in your html and then get the data attribute value using javascript.

Example:

In your button:

<a class="btn btn-danger btn-sm icon-delete" data-id="your_Id"><span class="glyphicon glyphicon-trash"></span></a>

Replace your_Id with the correct id.

Then using javascript on your onClick event:

$(this).data('id'); 

This will retrieve the id that you need to handle the action.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.