content logo

React Forms:

React Form with Saving Previously Inputted Data

In this post, we have a form for registering information. This form is gray. By clicking on the button that is available to register information in this form, the information is stored in the box below the form. Below the registration button is another button that can be clicked to view people who have already registered information in this form. The buttons of this form are all blue.

#

React JS Form Code

#

HTML Form Save Data

#

Submit React Form

#

HTML Input Form Saving

<!-- This script got from frontendfreecode.com -->
<div id="root"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
body{
    background-color: #007BA7;
}
#root {
    margin-top : 50px;
}
.center {
    float: none;
    margin: 0 auto;
}
.form-container {
    background-color: #6b6b6b;
    padding: 10px 25px;
    box-shadow: 0px 0px 15px black;
    border-radius: 5px; 
}
.form-container > h2 {
    margin-top: 10px;
    margin-bottom: 20px;
    text-align: center;
    color: black;
}
.form-control {
    margin-bottom: 5px;
}
.user-info-display {
    text-align: center;
    margin-top: 5px;
    background-color: #8a8a8a;
    padding: 5px;
}
.user-info-display > p {
    margin-bottom: 3px;
    margin-top: 3px;
    font-weight: bold;
    font-size: 12pt;
}
class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            firstName: '',
            lastName: '',
            phone: '',
            email: '',
            userData: {
                firstName: '',
                lastName: '',
                phoneNumber: '',
                email: ''
            },
            showFormInput: false
        };
    }
    handleFirstNameChange(event) {
        var firstName = event.target.value;
        this.setState({
            firstName: firstName
        });
    }
    handleLastNameChange(event) {
        var lastName = event.target.value;
        this.setState({
            lastName: lastName
        });
    }
    handlePhoneNumberChange(event) {
        var phone = event.target.value;
        this.setState({
            phone: phone
        });
    }
    handleEmailChange(event) {
        var email = event.target.value;
        this.setState({
            email: email
        });
    }
    handleFormSubmit(event) {
        this.hideFormInput();
        this.setState({
            userData: {
                firstName: this.inputIsEmpty(this.state.firstName) ? '' : this.state.firstName,
                lastName: this.inputIsEmpty(this.state.lastName) ? '' : this.state.lastName,
                phoneNumber: this.inputIsEmpty(this.state.phone) ? '' : this.state.phone,
                email: this.inputIsEmpty(this.state.email) ? '' : this.state.email
            }
        });
        event.preventDefault();
    }
    inputIsEmpty(input) {
        return input === '';
    }
    displayFormInput() {
        this.setState({
            showFormInput: true
        });
    }
    hideFormInput() {
        this.setState({
            showFormInput: false
        });
    }
    render() {
        var displayForm = '';
        if (this.state.showFormInput) {
            var displayFormData = /*#__PURE__*/React.createElement(UserInfoDisplay, { userData: this.state.userData });
        }
        return /*#__PURE__*/(
            React.createElement("div", { className: "row" }, /*#__PURE__*/
                React.createElement("div", { className: "form-container center col-lg-3" }, /*#__PURE__*/
                    React.createElement("h2", null, "React Form"), /*#__PURE__*/
                    React.createElement("form", { onSubmit: this.handleFormSubmit.bind(this) }, /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "first_name", value: this.state.firstName, onChange: this.handleFirstNameChange.bind(this), placeholder: "First Name" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "last_name", value: this.state.lastName, onChange: this.handleLastNameChange.bind(this), placeholder: "Last Name" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "phone_number", value: this.state.phone, onChange: this.handlePhoneNumberChange.bind(this), placeholder: "Phone Number" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "email", value: this.state.email, onChange: this.handleEmailChange.bind(this), placeholder: "Email" }), /*#__PURE__*/
                        React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                            React.createElement("input", { className: "btn btn-primary btn-block", type: "submit", value: "Submit" }))), /*#__PURE__*/
                    React.createElement("div", null, /*#__PURE__*/
                        React.createElement("button", { className: "btn btn-primary btn-block", onClick: this.displayFormInput.bind(this) }, "Show saved input")),
                    displayFormData)));
    }
}
function UserInfoDisplay(props) {
    return /*#__PURE__*/(
        React.createElement("div", { className: "user-info-display" }, /*#__PURE__*/
            React.createElement("p", null, /*#__PURE__*/React.createElement("span", null, props.userData.lastName), ", ", /*#__PURE__*/React.createElement("span", null, props.userData.firstName)), /*#__PURE__*/
            React.createElement("p", null, /*#__PURE__*/React.createElement("span", null, props.userData.phoneNumber), " | ", /*#__PURE__*/React.createElement("span", null, props.userData.email))));
}
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('root'));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.development.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.development.js'></script>
<style>
body{
    background-color: #007BA7;
}
#root {
    margin-top : 50px;
}
.center {
    float: none;
    margin: 0 auto;
}
.form-container {
    background-color: #6b6b6b;
    padding: 10px 25px;
    box-shadow: 0px 0px 15px black;
    border-radius: 5px; 
}
.form-container > h2 {
    margin-top: 10px;
    margin-bottom: 20px;
    text-align: center;
    color: black;
}
.form-control {
    margin-bottom: 5px;
}
.user-info-display {
    text-align: center;
    margin-top: 5px;
    background-color: #8a8a8a;
    padding: 5px;
}
.user-info-display > p {
    margin-bottom: 3px;
    margin-top: 3px;
    font-weight: bold;
    font-size: 12pt;
}
</style>

</head>
<body>
<div id="root"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            firstName: '',
            lastName: '',
            phone: '',
            email: '',
            userData: {
                firstName: '',
                lastName: '',
                phoneNumber: '',
                email: ''
            },
            showFormInput: false
        };
    }
    handleFirstNameChange(event) {
        var firstName = event.target.value;
        this.setState({
            firstName: firstName
        });
    }
    handleLastNameChange(event) {
        var lastName = event.target.value;
        this.setState({
            lastName: lastName
        });
    }
    handlePhoneNumberChange(event) {
        var phone = event.target.value;
        this.setState({
            phone: phone
        });
    }
    handleEmailChange(event) {
        var email = event.target.value;
        this.setState({
            email: email
        });
    }
    handleFormSubmit(event) {
        this.hideFormInput();
        this.setState({
            userData: {
                firstName: this.inputIsEmpty(this.state.firstName) ? '' : this.state.firstName,
                lastName: this.inputIsEmpty(this.state.lastName) ? '' : this.state.lastName,
                phoneNumber: this.inputIsEmpty(this.state.phone) ? '' : this.state.phone,
                email: this.inputIsEmpty(this.state.email) ? '' : this.state.email
            }
        });
        event.preventDefault();
    }
    inputIsEmpty(input) {
        return input === '';
    }
    displayFormInput() {
        this.setState({
            showFormInput: true
        });
    }
    hideFormInput() {
        this.setState({
            showFormInput: false
        });
    }
    render() {
        var displayForm = '';
        if (this.state.showFormInput) {
            var displayFormData = /*#__PURE__*/React.createElement(UserInfoDisplay, { userData: this.state.userData });
        }
        return /*#__PURE__*/(
            React.createElement("div", { className: "row" }, /*#__PURE__*/
                React.createElement("div", { className: "form-container center col-lg-3" }, /*#__PURE__*/
                    React.createElement("h2", null, "React Form"), /*#__PURE__*/
                    React.createElement("form", { onSubmit: this.handleFormSubmit.bind(this) }, /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "first_name", value: this.state.firstName, onChange: this.handleFirstNameChange.bind(this), placeholder: "First Name" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "last_name", value: this.state.lastName, onChange: this.handleLastNameChange.bind(this), placeholder: "Last Name" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "phone_number", value: this.state.phone, onChange: this.handlePhoneNumberChange.bind(this), placeholder: "Phone Number" }), /*#__PURE__*/
                        React.createElement("input", { className: "form-control", type: "text", name: "email", value: this.state.email, onChange: this.handleEmailChange.bind(this), placeholder: "Email" }), /*#__PURE__*/
                        React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                            React.createElement("input", { className: "btn btn-primary btn-block", type: "submit", value: "Submit" }))), /*#__PURE__*/
                    React.createElement("div", null, /*#__PURE__*/
                        React.createElement("button", { className: "btn btn-primary btn-block", onClick: this.displayFormInput.bind(this) }, "Show saved input")),
                    displayFormData)));
    }
}
function UserInfoDisplay(props) {
    return /*#__PURE__*/(
        React.createElement("div", { className: "user-info-display" }, /*#__PURE__*/
            React.createElement("p", null, /*#__PURE__*/React.createElement("span", null, props.userData.lastName), ", ", /*#__PURE__*/React.createElement("span", null, props.userData.firstName)), /*#__PURE__*/
            React.createElement("p", null, /*#__PURE__*/React.createElement("span", null, props.userData.phoneNumber), " | ", /*#__PURE__*/React.createElement("span", null, props.userData.email))));
}
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('root'));
</script>

</body>
</html>
Preview