gpt4 book ai didi

javascript - 如何在yii2中单击按钮时将数据从一个表保存到另一个表?

转载 作者:行者123 更新时间:2023-11-29 09:59:31 24 4
gpt4 key购买 nike

我有 3 个表(在数据库 phpmyadmin 中),分别是:appointmentapproveAppointmentrejectAppointment。每个表中的所有列名称都是相同的。

我目前正在开发学生和讲师之间的预约管理系统,当学生在(前端/查看/预约/创建)中预约时,所有数据都将插入到数据库中的预约表中。

讲师将获取学生向他们预约的所有数据,这些数据将从表“预约”中检索并显示在(使用 CRUD 的前端/ View /预约确认/索引,如图 1 所示)中,并且讲师需要单击按钮即可查看并确认批准或拒绝(如图 2 所示)

当讲师单击按钮批准时,我希望有关约会的所有数据插入到表“approveAppointment”中。如果讲师点击拒绝按钮,则所有数据都会插入到表“rejectAppointment”中。

图1 enter image description here

图2 enter image description here

这是我的预约确认 Controller (actionApprove)代码:

 public function actionApprove($id)
{
$model = new ApproveAppointment();
$model->save();
}

这是approveAppointment Controller 的代码

public function actionApproveAppointment()
{
if (Yii::$app->request->isPost && Yii::$app->request->isAjax) {
$appointment_id = Yii::$app->request->post('appID');
$appointmentModel = $this->findAppointmentModel($appointment_id);

//model instance for the approve model
$model = new ApproveAppointment();
$model->attributes=$appointmentModel->attributes;

//set the response format
Yii::$app->response->format = \yii\base\Response::FORMAT_JSON;

$response['success'] = false;

try {
$model->approveAppointment();
$response['success'] = true;
$response['message'] = "Appointment is approved";
} catch (\Exception $e) {
$response['message'] = $e->getMessage();
}
return $response;
}

这是我的观点的代码

$appID = $model->appID;
$js = <<<JS
$("#approve").on('click',function(){
let data=[];
data.push({name:"appID",value:$appID});
data.push({name:yii.getCsrfParam(),value:yii.getCsrfToken()});
$.ajax({
url: "index.php?r=appointment-confirmation/approveAppointment",
data:data,
type:'POST',
dataType:'json',

}).done(function( data ) {
//display an alert or insert inside the html div for showing

messages
alert(data.message);
});
});

JS;
$this->registerJs($js, \yii\web\View::POS_READY);

以及我 View 中的批准按钮

<?= Html::a('Approve', ['approve', 'id'=>"approve"], ['class' => 'btn btn-primary']) ?>

最佳答案

为什么您需要 3 个类似的表?您可以在预约表中创建“状态”列,并指定值“已批准”、“已拒绝”、“审核”!

关于javascript - 如何在yii2中单击按钮时将数据从一个表保存到另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53286835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com