Advertisement
shopnilSS

Take Form data input in React

May 31st, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react'
  2.  
  3. class StudentForm extends Component{
  4.     constructor(){
  5.         super()
  6.         this.state = {
  7.             firstName: "", //text
  8.             lastName: "", //text
  9.             fatherName: "", //text
  10.             motherName: "", //text
  11.             permanentAddress: "", //text
  12.             currentAddress: "", //text
  13.             contactNumber:"", //text
  14.             dateOfBirth:"", //text
  15.             sex:"", //radio
  16.             userId: "", //text
  17.             email:"", //text
  18.             userType:"" ,//section //text
  19.             imageUpload: null ,//image upload //file
  20.             imageUploadTwo : null, //demo image upload
  21.             password : "", //password
  22.             retypePassword : "" //retype password
  23.         }
  24.     }
  25.  
  26.     //take all data input handler
  27.     dataChangeHandler = (event) => {
  28.         this.setState({
  29.             [event.target.name]: event.target.value //store the input value dynamic way by input data name
  30.         })
  31.     }
  32.  
  33.     //take the image input handler
  34.     imageUploadController = (event) => {
  35.         this.setState({
  36.             [event.target.name] : URL.createObjectURL(event.target.files[0])
  37.         })
  38.     }
  39.  
  40.    
  41.     //form submission handler
  42.     submissionHandelar = (event) => {
  43.         event.preventDefault() //to stop the auto load during submission
  44.  
  45.         const {firstName, lastName, fatherName, motherName, permanentAddress, currentAddress, contactNumber, dateOfBirth, sex, userId, email, userType, imageUpload, password, retypePassword} = this.state //get the data from state value
  46.  
  47.         let data = {
  48.             personalInfo: {
  49.                 name: {
  50.                     FirstName: firstName,
  51.                     LastName: lastName
  52.                 },
  53.                 contact: {
  54.                     permanentAddress,
  55.                     currentAddress,
  56.                     mobileNo: contactNumber
  57.                 },
  58.                 profilePic: imageUpload,
  59.                 FatherName: fatherName,
  60.                 MotherName: motherName,
  61.                 email,
  62.                 dateOfBirth,
  63.                 sex
  64.             },
  65.             userId,
  66.             userType: userType,
  67.             password,
  68.             retypePassword
  69.         } //format of my model schema
  70.  
  71.         const dataInJsonFormat = JSON.stringify(data) //conver the data in to  json format
  72.         console.log(dataInJsonFormat); //give the data in json format
  73.     }
  74.    
  75.    
  76.     render(){
  77.         return(
  78.             <div>
  79.                 <form className = {`mb-5`}>
  80.                     <div className="form-group">
  81.                         {/* name*/}
  82.                         <label htmlFor="exampleInputEmail1">Name</label>
  83.                         <input type="text" className="form-control " id="exampleInputEmail1"  placeholder="First Name" name = "firstName" onChange = {this.dataChangeHandler} value = {this.state.firstName}></input>
  84.                         <br />
  85.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Last Name" name = "lastName" value = {this.state.lastName} onChange = {this.dataChangeHandler}></input>
  86.                     </div>
  87.                     {/* personalInfo  */}
  88.                     <div className="form-group">
  89.                         <br />
  90.                         <label htmlFor="personal info">Personal Information</label>
  91.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Father Name" name = "fatherName" value = {this.state.fatherName} onChange = {this.dataChangeHandler}></input>
  92.  
  93.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Mother Name" value = {this.state.motherName} name = "motherName" onChange = {this.dataChangeHandler}></input>
  94.  
  95.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Permanent Address" name = "permanentAddress" value = {this.state.permanentAddress} onChange = {this.dataChangeHandler}></input>
  96.  
  97.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Current Address" name = "currentAddress" value = {this.state.currentAddress} onChange = {this.dataChangeHandler} ></input>
  98.  
  99.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Contact Number" name = "contactNumber" value = {this.state.contactNumber} onChange = {this.dataChangeHandler}></input>
  100.                     </div>
  101.  
  102.                     <br />
  103.                     <div className="form-group">
  104.                         {/* date of birth  */}
  105.                         <label htmlFor="dateOfBirth">Date of Birth</label>
  106.                         <input type="date" className="form-control" id="exampleInputPassword1" placeholder="Date of Birth" name = "dateOfBirth" value = {this.state.dateOfBirth} onChange = {this.dataChangeHandler}></input>
  107.                     </div>
  108.  
  109.                     <br />
  110.  
  111.                     {/* sex  */}
  112.                     <label htmlFor="sex">Sex</label>
  113.                     <div className="form-check">
  114.                         {/* female  */}
  115.                         <input className="form-check-input" type="radio" name="sex" id="exampleRadios1" value = "female" onChange = {this.dataChangeHandler}></input>
  116.                         <label className="form-check-label" htmlFor="exampleRadios1">
  117.                             Female
  118.                         </label>
  119.                     </div>
  120.                     {/* male  */}
  121.                     <div className="form-check">
  122.                         <input className="form-check-input" type="radio" name="sex" id="exampleRadios2" value="male" onChange = {this.dataChangeHandler}></input>
  123.                         <label className="form-check-label" htmlFor="exampleRadios2">
  124.                             Male
  125.                         </label>
  126.                     </div>
  127.                    
  128.                     <br />
  129.  
  130.                     {/* user id  */}
  131.                     <div className="form-group">
  132.                         <label htmlFor="UserId">User Id</label>
  133.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="User ID" name = "userId" value = {this.state.userId} onChange = {this.dataChangeHandler}></input>
  134.                     </div>
  135.                    
  136.                     <br />
  137.                     {/* password  */
  138.                     <div className="form-group">
  139.                         <label htmlFor="exampleInputPassword1">Password</label>
  140.                         <input type="password" className="form-control" id="exampleInputPassword1" placeholder="Password" name = "password" onChange = {this.dataChangeHandler} value = {this.state.password}></input>
  141.                     </div>}
  142.  
  143.                     {/* retype password  */}
  144.                     <div className="form-group">
  145.                         <input type="password" className="form-control" id="exampleInputPassword1" placeholder="Retype Password" name = "retypePassword" value = {this.state.retypePassword} onChange = {this.dataChangeHandler}></input>
  146.                     </div>
  147.                    
  148.                     <br />
  149.                     {/* email  */}
  150.                     <div className="form-group">
  151.                         <label htmlFor="email">Email</label>
  152.                         <input type="text" className="form-control" id="exampleInputPassword1" placeholder="Email" name = "email" value = {this.state.email} onChange = {this.dataChangeHandler}></input>
  153.                     </div>
  154.                     <br />
  155.  
  156.                     {/* user type  */}
  157.                     <div class="form-group">
  158.                         <label for="exampleFormControlSelect1">User Type</label>
  159.                         <select class="form-control" id="exampleFormControlSelect1" name = "userType" value = {this.state.userType}  onChange = {this.dataChangeHandler} >
  160.                             <option value = "student">Student</option>
  161.                             <option value = "teacher">Teacher</option>
  162.                         </select>
  163.                     </div>
  164.  
  165.                     <br />
  166.                     {/* image upload one */}
  167.                     <label htmlFor="proPicUpload">Profile Picture</label>
  168.                     <div className="input-group mb-3">
  169.                         <div className="custom-file">
  170.                             <input type="file" className="custom-file-input" id="inputGroupFile01" name = "imageUpload" onChange = {this.imageUploadController}/>
  171.                             <label className="custom-file-label" htmlFor="inputGroupFile01">Choose file</label>
  172.                         </div>
  173.                     </div>
  174.  
  175.                     <br />
  176.                     {/* image upload two  */}
  177.                     <label htmlFor="demoPicUpload">Demo Picture upload</label>
  178.                     <div className="input-group mb-3">
  179.                         <div className="custom-file">
  180.                             <input type="file" className="custom-file-input" id="inputGroupFile01" name = "imageUploadTwo" onChange = {this.imageUploadController} />
  181.                             <label className="custom-file-label" htmlFor="inputGroupFile01">Choose file</label>
  182.                         </div>
  183.                     </div>
  184.  
  185.                    
  186.                     {/* submit button  */}
  187.                     <button type="submit" className="btn btn-primary" onClick = {this.submissionHandelar}>Submit</button>
  188.                 </form>
  189.             </div>
  190.         )
  191.     }
  192. }
  193.  
  194. export default StudentForm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement