简体   繁体   中英

React Form to send data to PHP script which should then send email

I have been struggling with this one quite a bit... Some background - my React front end is running on a LAMP back end (company's set up not my choice) and I currently have the task of creating a contact form that when submitted - will send the form data to a PHP script which should then trigger an email to be sent to a specific inbox we have set up.

I feel as though I understand the form portion correct as I am most comfortable with HTML/CSS/JS/React, but the LAMP stack set up is really throwing me. I'll try to list specific questions clearly below.

  1. How would one directly target a file on an EC2 instance?

I am currently getting a 500 error (failed to load resource) from the server when attempting to target my PHP file. I have tried an absolute path from the root of the server as well as a path from the root directory of the project. Either way it fails.

  1. I am getting a React warning in console "Form Submission failed because form is not connected", but to my knowledge the form is connected... The action attribute is tied to the PHP file that I would like to send data to.

Currently I have an async await call running when the form is submitted which is supposed to make a fetch/POST call to the file and then things should be handled, but again I am having the issues listed above.

I do not want to share publicly share code because this is a company project, but if someone has experience with this type of set up and has time to help someone trouble shoot please let me know. Sorry for the novel folks and thank you for any help!

Edit - is there any type of work around I could use to avoid even sending this to PHP script and generating an email an alternative way besides emailJS?

If you've created a standard html form then the presumably you have action and method attributes on the form.

You would traditionally:

  1. Set the form action attribute to point at your php script. There should be no reason the php script isn't directly accessible unless it's off document root or you have a .htaccess file that is routing all requests through an intermediary (like an index.php file).

  2. Set the form method attribute to POST .

  3. The PHP script directed to in the action should read from $_POST which is an array that will contain any data sent via POST request to your php script.

  4. You can then use a variety of methods for sending the email. Since you've mentioned EC2 you're obviously in AWS. In this case AWS offer a great email service called Simple Email Service. You can find the SDK and example scripts for sending an email via it here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-php.html However, do note that SES requires you to go through a verification process as you will initially be in a sandbox mode that has certain restrictions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM