|
1 | 1 | import React from 'react'; |
2 | | -import logo from './logo.svg'; |
3 | 2 | import './App.css'; |
4 | 3 |
|
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 | + } |
24 | 48 | } |
25 | 49 |
|
26 | 50 | export default App; |
0 commit comments