Skip to content

Commit 31d77b8

Browse files
committed
Initial Commit
1 parent 4829aa3 commit 31d77b8

3 files changed

Lines changed: 91 additions & 20 deletions

File tree

api/processform.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
header("Access-Control-Allow-Origin: *");
3+
$rest_json = file_get_contents("php://input");
4+
5+
if (empty($_POST['name']) && empty($_POST['phone']) && empty($_POST['email'])) die();
6+
7+
if ($_POST)
8+
{
9+
10+
// set response code - 200 OK
11+
12+
http_response_code(200);
13+
$name = $_POST['name'];
14+
$to = "kay@dispostable.com";
15+
$from = $_POST['email'];
16+
17+
// data
18+
19+
$msg = $_POST['name'] . $_POST['phone'];
20+
21+
// Headers
22+
23+
$headers = "MIME-Version: 1.0\r\n";
24+
$headers.= "Content-type: text/html; charset=UTF-8\r\n";
25+
$headers.= "From: <" . $from . ">";
26+
mail($to, $name, $msg, $headers);
27+
28+
// echo json_encode( $_POST );
29+
30+
echo json_encode(array(
31+
"sent" => true
32+
));
33+
}
34+
else
35+
{
36+
// tell the user about error
37+
echo json_encode(["sent" => false, "message" => "Something went wrong"]);
38+
}
39+
40+
?>

src/App.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
color: #61dafb;
2929
}
3030

31+
input[type="text"], input[type="submit"], input[type="email"] {
32+
padding: 10px 15px;
33+
}
34+
input[type="text"], input[type="email"] {
35+
width: 300px;
36+
}
37+
3138
@keyframes App-logo-spin {
3239
from {
3340
transform: rotate(0deg);

src/App.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
import React from 'react';
2-
import logo from './logo.svg';
32
import './App.css';
43

5-
function App() {
6-
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
23-
);
4+
5+
class App extends React.Component {
6+
7+
SubmitForm(event) {
8+
event.preventDefault();
9+
10+
var myHeaders = new Headers();
11+
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
12+
13+
var urlencoded = new URLSearchParams();
14+
urlencoded.append("name", this.name.value);
15+
urlencoded.append("phone", this.phone.value);
16+
urlencoded.append("email", this.email.value);
17+
18+
var requestOptions = {
19+
method: 'POST',
20+
headers: myHeaders,
21+
body: urlencoded,
22+
redirect: 'follow'
23+
};
24+
25+
// * Always make sure your php file is located inside an apache server
26+
// ? You can decide to put the whole project folder inside an apache server.
27+
28+
fetch("http://localhost/my-app/api/processform.php", requestOptions)
29+
.then(response => response.text())
30+
.then(result => console.log(result))
31+
.catch(error => console.log('error', error));
32+
}
33+
34+
35+
render(){
36+
return (
37+
<div className="App">
38+
<form onSubmit={(e) => this.SubmitForm(e) } >
39+
<h2>Message US</h2>
40+
<input type="text" placeholder="Name" ref={(input) => {this.name = input}} /><br />
41+
<input type="text" placeholder="Phone" ref={(input) => {this.phone = input}} /><br />
42+
<input type="email" placeholder="Email" ref={(input) => {this.email = input}} /><br />
43+
<input type="submit" value="Send Message" />
44+
</form>
45+
</div>
46+
)
47+
}
2448
}
2549

2650
export default App;

0 commit comments

Comments
 (0)