Skip to content

Dev #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_API = http://localhost:80/react-contact-form/api/contact/index.php
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
## Contact form React Component with PHP mail api.

This is code for tutorial in my blog post [Create Contact form with react and php](https://blog.bitsrc.io/how-to-build-a-contact-form-with-react-js-and-php-d5977c17fec0).

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)

## How to start
## Usage of complete project.

Please clone this repo and in the project root directory, Please run:

### `npm install`

In the project directory, you can run:
please go to the root fo the project and run npm install to install all the nessery npm modules.

### `npm start`

Expand All @@ -14,14 +21,29 @@ The page will reload if you make edits.<br>
You will also see any lint errors in the console.


![Alt text](/screenshot.png?raw=true "Screenshot")

## Usage only the component

Please download 'src/components/Form/' folder.

You can use the form component like this `<Form config={config} />`

####props

`config - object`

Example of config prop

<div align="center">
<a href="https://www.paypal.me/craftcode"><img alt="Header" src="https://cdn-images-1.medium.com/max/806/1*G95uyokAH4JC5Ppvx4LmoQ.png" width="40%"></a>
<a href="https://www.paypal.me/craftcode"><img alt="Header" src="/config-obj.png?raw=true" width="70%"></a>
</div>


<div align="center">
<a href="https://www.paypal.me/craftcode"><img alt="Header" src="/screenshot.png?raw=true" width="80%"></a>
</div>


## Contributors

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand All @@ -32,4 +54,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!


<div align="center">
<a href="https://www.paypal.me/craftcode"><img alt="Header" src="https://cdn-images-1.medium.com/max/806/1*G95uyokAH4JC5Ppvx4LmoQ.png" width="20%"></a>
</div>
3 changes: 1 addition & 2 deletions api/contact/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@
"message" => "Something went wrong"
]
);
}
?>
}
Binary file added config-obj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3,074 changes: 2,427 additions & 647 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.6.3"
},
"devDependencies": {
"axios": "^0.18.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"prop-types": "^15.7.2",
"react-scripts": "2.1.1"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>React PHP Contact form | Tutorial</title>
</head>
<body>
<noscript>
Expand Down
83 changes: 0 additions & 83 deletions src/App.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/App.test.js

This file was deleted.

92 changes: 92 additions & 0 deletions src/components/Form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React from "react";
import PropTypes from "prop-types";
import axios from "axios";
import "./styles.css";

/**
* @component Form
* @props
*/
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {
fname: "",
lname: "",
email: "",
message: "",
mailSent: false,
error: null
};
}

handleFormSubmit = e => {
e.preventDefault();
axios({
method: "post",
url: `${process.env.REACT_APP_API}`,
headers: { "content-type": "application/json" },
data: this.state
})
.then(result => {
this.setState({
mailSent: result.data.sent
});
console.log(this.state);
})
.catch(error => this.setState({ error: error.message }));
};

render() {
const { title, successMessage, errorMessage, fields } = this.props.config;
return (
<div className="App">
<h2>{title}</h2>
<div>
<form action="#">
{fields &&
fields.map(fields => {
return (
<React.Fragment>
{fields.type !== "textarea" ? (
<React.Fragment>
<label>{fields.label}</label>
<input
type={fields.type}
className={fields.klassName}
placeholder={fields.placeholder}
value={this.state.fname}
onChange={e => this.setState({ fname: e.target.value })}
/>
</React.Fragment>
) : (
<React.Fragment>
<label>{fields.label}</label>
<textarea
className={fields.klassName}
placeholder={fields.placeholder}
onChange={e => this.setState({ message: e.target.value })}
value={this.state.message}
/>
</React.Fragment>
)}
</React.Fragment>
);
})}
<input type="submit" onClick={e => this.handleFormSubmit(e)} value="Submit" />
<div>
{this.state.mailSent && <div className="sucsess">{successMessage}</div>}
{this.state.error && <div className="error">{errorMessage}</div>}
</div>
</form>
</div>
</div>
);
}
}

export default Form;
//propTypes for the component
Form.propTypes = {
config: PropTypes.object.isRequired
};
File renamed without changes.
14 changes: 0 additions & 14 deletions src/index.css

This file was deleted.

23 changes: 14 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import Form from '../src/components/Form';

ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: http://bit.ly/CRA-PWA
serviceWorker.unregister();
const config = {
api: `${process.env.REACT_APP_API}`,
title: 'Contact Me',
successMessage: 'Thank you for contcting me.',
errorMessage: 'Sorry we have some problems.',
fields: [
{ id: 1, label: 'First Name', type: 'text',placeholder:'Your First Name', isRequired: true , klassName:'first-name-field'},
{ id: 2, label: 'Last Name', type: 'text', placeholder: 'Your Last Name', isRequired: true , klassName:'last-name-field'},
{ id: 3, label: 'Email', type: 'email', placeholder: ' Your Email', isRequired: true , klassName:'email-field'},
{ id: 4, label: 'Message', type: 'textarea',placeholder:'Write something.....', isRequired: true , klassName:'message-field'}
]
}
ReactDOM.render(<Form config={config} />, document.getElementById('root'));
7 changes: 0 additions & 7 deletions src/logo.svg

This file was deleted.

Loading