I'm totally new with frontend frameworks. I'm currently building my portfolio with Vue.js and I have a contact form in it where I want to send the data the user put (name, email, message) to a PHP file (or whatever) with Axios so I can send it to my email. How can I achieve that?
Can I just create a "sendEmail.php" file somewhere and do a POST request to it with Axios, vue-resource or whatever? That's what I tried but I get a 404 (Not Found) error, I can't reach the file.
EDIT: So apparently Vue itself can't read PHP files so I had to put mine to WAMP and then call it with Axios. Next I didn't get a 404 error anymore but a CORS policy error instead:
I followed this Enable CORS with wamp on windows 8 and I think it KINDA worked since I'm now able to do GET requests but not POST requests
To answer to @Jijin, here's my Axios request and PHP file:
axios
.post("http://localhost/test/sendEmail.php", {
name: this.form.name,
email: this.form.email,
message: this.form.message
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
<?php
header('Access-Control-Allow-Origin: *');
echo "Huh hello?";
EDIT 2: It works! I added header("Access-Control-Allow-Headers: Content-Type, origin");
on top of my PHP file and I can now do POST requests!
So now I guess I can get the data from my PHP file and send the email. I will try, thanks guys!