[英]Using Vue.js components in PHP apps
我正在將PHP和Vue.js與vue-router一起使用。 我有一個發送電子郵件的php表單處理程序。 發送電子郵件后,我想將用戶從我的vue應用重定向到某個組件。 有辦法使它起作用嗎? 我創建了一個組件Thankyou.vue,並希望以某種方式將其嵌入到我的php文件中。
if(mail($address, $msg, $headers)) {
header("Location: http://mypage.com/thankyou");
}
我對vue.js不太熟悉,所以請不要在JS部分:-)
無論如何,您應該做的是使用AJAX將數據發布到PHP,並返回JSON和正確的HTTP標頭。
在JavaScript中發布數據並處理響應承諾:
this.$http.post('//someurl.bla', { key: value })
.then((response) => {
// show your thank you message
})
.catch(error => {
console.log(error);
});
處理PHP中的錯誤:
/**
* exits the script and returns a HTTP 400 and JSON error message
* @param str $msg error message
*/
function dieError($msg) {
http_response_code('400');
echo json_encode(['error' => $msg]);
exit;
}
// check form data
if (!$_POST['parameter']) {
dieError('Parameter "Parameter" is missing.');
}
// do stuff, build the mail content etc.
// throw an error if the mail is not sent (note the !)
if(!mail($address, $msg, $headers)) {
dieError('Error sending mail');
}
// if the script arrives here, everything was fine, the status code will be 200 OK
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.