content logo

React Forms:

Simple Ajax Form with React JS

In this post, we have a simple but useful form using React. This form can be used for login. In this form there is a field to enter the username. The button for logging in to the online form is yellow. The button itself and the whole form also have an outer shadow. This is a responsive form.

#

Responsive React Form Code

#

Ajax Form Example

#

HTML Form Validator

#

React JS Ajax

<!-- This script got from frontendfreecode.com -->
<div class="react-form-container"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
html {
  box-sizing: border-box;
  font-size: 16px;
}
*,
*:after,
*:before {
  box-sizing: border-box;
}
body {
  font: 100% "Roboto", arial, sans-serif;
  background: #f5f5f5;
}
form {
  padding: 2rem;
  margin-top: 2rem;
  margin-right: auto;
  margin-left: auto;
  max-width: 23.75rem;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
}
h1 {
  margin-top: 0;
  margin-bottom: 3.236rem;
  text-align: center;
  font-size: 1.618rem;
}
.form-group {
  padding: 0;
  border: 0;
}
.form-group + .form-group {
  margin-top: 1rem;
}
label {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  touch-action: manipulation;
}
input,
textarea {
  display: block;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-size: 1rem;
  line-height: 1.25;
  color: #55595c;
  background-color: #fff;
  background-image: none;
  background-clip: padding-box;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #eee;
  border-left: 0;
  border-radius: 3px;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
input:focus,
textarea:focus {
  outline: 0;
  border-bottom-color: #ffab00;
}
textarea {
  resize: vertical;
}
.btn {
  display: inline-block;
  padding: 0.75rem 1rem;
  margin-top: 1.618rem;
  font-weight: 400;
  text-align: center;
  text-transform: uppercase;
  color: #fff;
  vertical-align: middle;
  white-space: nowrap;
  background-color: #ffab00;
  border: 1px solid transparent;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  cursor: pointer;
  user-select: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
.btn:focus, .btn:hover {
  background-color: #ffc142;
  box-shadow: 0 18px 35px rgba(50, 50, 93, 0.1), 0 8px 15px rgba(0, 0, 0, 0.07);
}
.btn:focus {
  outline: 0;
}
const reactFormContainer = document.querySelector('.react-form-container');
class ReactFormLabel extends React.Component {
    constructor() {
        super();
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("label", { htmlFor: this.props.htmlFor }, this.props.title));
    }
}
class ReactForm extends React.Component {
    constructor() {
        super();
        this.state = {
            name: ''
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }
    /**
      @this {ReactForm}
    */
    handleChange(ev) {
        let newState = {};
        newState[ev.target.name] = ev.target.value;
        console.log(newState);
        this.setState(newState);
    }
    handleSubmit(ev, msg) {
        ev.preventDefault();
        let formData = {
            formSender: this.state.name
        };
        if (formData.formSender.length < 1) {
            return false;
        }
        console.log('Send via ajax!');
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("form", { className: "react-form", onSubmit: this.handleSubmit }, /*#__PURE__*/
                React.createElement("h1", null, "Say Hi!"), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formName", title: "Full Name:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formName", className: "form-input", name: "name", type: "text", required: true, onChange: this.handleChange, value: this.state.name })), /*#__PURE__*/
                React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                    React.createElement("input", { id: "formButton", className: "btn", type: "submit", placeholder: "Send message" }))));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ReactForm, null), reactFormContainer);
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<style>
html {
  box-sizing: border-box;
  font-size: 16px;
}
*,
*:after,
*:before {
  box-sizing: border-box;
}
body {
  font: 100% "Roboto", arial, sans-serif;
  background: #f5f5f5;
}
form {
  padding: 2rem;
  margin-top: 2rem;
  margin-right: auto;
  margin-left: auto;
  max-width: 23.75rem;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
}
h1 {
  margin-top: 0;
  margin-bottom: 3.236rem;
  text-align: center;
  font-size: 1.618rem;
}
.form-group {
  padding: 0;
  border: 0;
}
.form-group + .form-group {
  margin-top: 1rem;
}
label {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  touch-action: manipulation;
}
input,
textarea {
  display: block;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-size: 1rem;
  line-height: 1.25;
  color: #55595c;
  background-color: #fff;
  background-image: none;
  background-clip: padding-box;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #eee;
  border-left: 0;
  border-radius: 3px;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
input:focus,
textarea:focus {
  outline: 0;
  border-bottom-color: #ffab00;
}
textarea {
  resize: vertical;
}
.btn {
  display: inline-block;
  padding: 0.75rem 1rem;
  margin-top: 1.618rem;
  font-weight: 400;
  text-align: center;
  text-transform: uppercase;
  color: #fff;
  vertical-align: middle;
  white-space: nowrap;
  background-color: #ffab00;
  border: 1px solid transparent;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  cursor: pointer;
  user-select: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
.btn:focus, .btn:hover {
  background-color: #ffc142;
  box-shadow: 0 18px 35px rgba(50, 50, 93, 0.1), 0 8px 15px rgba(0, 0, 0, 0.07);
}
.btn:focus {
  outline: 0;
}
</style>

</head>
<body>
<div class="react-form-container"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
const reactFormContainer = document.querySelector('.react-form-container');
class ReactFormLabel extends React.Component {
    constructor() {
        super();
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("label", { htmlFor: this.props.htmlFor }, this.props.title));
    }
}
class ReactForm extends React.Component {
    constructor() {
        super();
        this.state = {
            name: ''
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }
    /**
      @this {ReactForm}
    */
    handleChange(ev) {
        let newState = {};
        newState[ev.target.name] = ev.target.value;
        console.log(newState);
        this.setState(newState);
    }
    handleSubmit(ev, msg) {
        ev.preventDefault();
        let formData = {
            formSender: this.state.name
        };
        if (formData.formSender.length < 1) {
            return false;
        }
        console.log('Send via ajax!');
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("form", { className: "react-form", onSubmit: this.handleSubmit }, /*#__PURE__*/
                React.createElement("h1", null, "Say Hi!"), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formName", title: "Full Name:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formName", className: "form-input", name: "name", type: "text", required: true, onChange: this.handleChange, value: this.state.name })), /*#__PURE__*/
                React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                    React.createElement("input", { id: "formButton", className: "btn", type: "submit", placeholder: "Send message" }))));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ReactForm, null), reactFormContainer);
</script>

</body>
</html>
Preview