[英]Using Vue.js components in PHP apps
I am using PHP and Vue.js with vue-router. 我正在将PHP和Vue.js与vue-router一起使用。 I've got a php form handler that sends emails. 我有一个发送电子邮件的php表单处理程序。 I would like to redirect the user to a certain component from my vue app when the email is sent. 发送电子邮件后,我想将用户从我的vue应用重定向到某个组件。 Is there a way to make it work? 有办法使它起作用吗? I created a component Thankyou.vue and would like to somehow embed it in my php file. 我创建了一个组件Thankyou.vue,并希望以某种方式将其嵌入到我的php文件中。
if(mail($address, $msg, $headers)) {
header("Location: http://mypage.com/thankyou");
}
I'm not too familiar with vue.js, so don't nail me down on the JS part :-) 我对vue.js不太熟悉,所以请不要在JS部分:-)
Anyway, what you should do is POST your data to PHP with AJAX and return JSON and correct HTTP headers. 无论如何,您应该做的是使用AJAX将数据发布到PHP,并返回JSON和正确的HTTP标头。
POST the data in JavaScript and handle the response promise: 在JavaScript中发布数据并处理响应承诺:
this.$http.post('//someurl.bla', { key: value })
.then((response) => {
// show your thank you message
})
.catch(error => {
console.log(error);
});
Handle errors in PHP: 处理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.