Search code examples
phpformsooppost-redirect-get

Object Oriented PHP to pass message to user upon success of function


I am new to OO php so this may seem basic..

Basically I have a list of courses a user can book. I have got it so the user can remove the course from their list, but I want a message to be displayed to them after they delete. I have done something similar to what I want here:

<form name="removecourse" action="<?php bloginfo('url');?>/user/<?php echo $current_user->first_name ; ?>" method="post">
<input type="hidden" value="<?php the_id();?>" name="courseid" />
<input id="removebutton" type="submit" name="removecourse" value="Remove">
</form>

The form sends the required data to the same page, and at the top of that page is a check to see if the forms post name is present in $_POST[] like so:

    if(isset($_POST['removecourse']) && !empty($_POST['removecourse'])){        
    $courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
    echo $courseManager->delete_response;
}; 

This is where the Class and object part comes in...

    public $delete_response;

function delete_post($postid, $reason){

     //stuff to actually delete the post

    $this->delete_response = 'Thanks, your course has been removed.';

}

So here I am adding a value to the delete_response variable and calling it above at the top of the page. This works, but when I refresh the page the message is still there as I am resubmitting the POST. I am just wondering if what I am doing is along the right track, and how to implement a Post/Redirect/Get type functionallity to stop the messaage from appearing on page refresh?


Solution

  • You have to check, either your course has been already deleted, is it simple as that :).

    Yours displaying it again because:

    if(isset($_POST['removecourse']) && !empty($_POST['removecourse'])){
    //is always true when posted again. 
    }
    

    You have to check the existiance