Update or delete depending on which button wag clicked

I have a form where the admin can either update multiple records or delete multiple records depending on which button was clicked.

This is the form:

  <form action="" method="post" name="review-form" id="review-form">
    <ul id="reviews">
      <?php foreach ($reviews as $key => $value): ?>
      <li>
      <h4><span><input name="guestbook_id[]" id="guestbook_id[]" type="checkbox" value="<?php echo $value['guestbook_id']; ?>"></span>&nbsp;<?php echo $value['entry_name']; ?> &nbsp;&nbsp;
      <select name="source_id" id="source_id" class="source">
        <?php foreach($sources as $source): ?>
        <option value="<?php echo $source['source_id']; ?>"><?php echo $source['source_name']; ?></option>
        <?php endforeach; ?>
      </select>
      <span></h4>
      <p><?php echo $value['entry_comment']; ?></p></li>
      <?php endforeach; ?>
    </ul>
    <input name="btnSubmit" type="submit" id="btnSubmit" class="update_button" value="Update entries"><&nbsp;&nbsp;<input name="btnDelete" type="submit" id="btnDelete" class="update_button" value="Delete entries">  
  </form>

I use the following jquery function:

$("#review-form").on("submit", function(e) {
    e.preventDefault();
    $.ajax({
        url : "/admin/manageReviews",
        type: "post",
        data: $(this).serialize(),
        success: function(data){
           window.alert("The review table was successfully updated. Click the OK button to continue.");
           setTimeout(function() {
               location.reload();
           }, 500)     
        },
        error: function(data){
           window.alert("Something went wrong and the online review table was not updated. Click the OK button to try again.");
           setTimeout(function() {
               location.reload();
           }, 500)  
        }
        
    });    
});

This is the manageReviews action:

    public function manageReviewsAction()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            if (isset($_POST["btnSubmit"]))
            {
                $source_id = filter_input(INPUT_POST, 'source_id', FILTER_SANITIZE_NUMBER_INT);
                $update    = $this->page->update_guestbook_entries($source_id,$_POST['guestbook_id']);
            }
            elseif (isset($_POST["btnDelete"]))
            {
                $delete = $this->page->delete_guestbook_entries($_POST['guestbook_id']);
            }
        }
    }

And this are the 2 methods I call in the action:

    public function update_guestbook_entries($source_id,array $ids)
    {
        $ids = implode(', ', $ids);
        
        $sql = "UPDATE guestbook_entries 
                   SET source_id = ?
                     , isActive = 1
                 WHERE guestbook_id IN ($ids)";
        
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array($source_id));
    }
    
    public function delete_guestbook_entries(array $ids)
    {
        $ids = implode(', ', $ids);
        $sql = "DELETE FROM guestbook_entries 
                      WHERE guestbook_id IN ($ids)";
        
        $stmt = $this->pdo->query($sql);
        $stmt->execute();
    }

But what ever I try, neither the update or delete actions are executed. Am I overlooking something?

Thank you in advance

I just found out that if I remove the if(isset) statements

 if (isset($_POST["btnSubmit"]))
 {
 }
 elseif (isset($_POST["btnDelete"]))
 {
  }

and just run for example the delete method

    public function manageReviewsAction()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
           $delete = $this->page->delete_guestbook_entries($_POST['guestbook_id']);

        }
    }

and I click on btnDelete that it is working and the same for the update method. So I wonder what can be wrong with those if statements.

Again thank you in advance

I don’t think all browsers send anything from a form for a submit button. try using a radio button with two options (with the default option used for “Update” and have the one submit button. Provided that an option is selected for the radio button, there will be a value for it in $_POST.

Hi. SpacePhoenix. Thanks for the reply. I am not sure if completely understand you?
Can you show me some sort of example?

There was a known bug in certain versions of IE that if the user hit Enter to submit a form instead of pushing the submit button, the button’s existance was not passed from the browser. so neither btnSubmit or btnDelete would be present.

Howerver, I’m gonna guess that because you’re using jquery to serialize the data, it doesnt know which button you clicked (the buttons are simply ‘there’ according to the serialize function, they’re not submittable information because that function has no concept of a given button being pushed, even if the original event trigger does). Try capturing which button was pushed (It… may be inside “e” during the jquery call. Check the object during runtime) and routing that information to your PHP somehow (My suggestion would be to push it onto the end of the URL and check for $_GET[‘action’] or somesuch.)

Your PHP looks fine, it simply isnt getting the data it requires to function correctly.

Hi StarLion. Thanks for the responce. That makes a lot off sense. I will have a look into this. Again thanks a lot

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.