duanji9264 2012-06-27 12:45
浏览 23
已采纳

面向对象的PHP在函数成功时将消息传递给用户

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?

  • 写回答

1条回答 默认 最新

  • duankuang7928 2012-06-27 12:55
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题