0

I have a form containing several inputs and I am handling multiple user inputs but I want to display all the inputs when the submit button is clicked.

What do I need to add/change in my code to display the inputs when they are submitted?

class Planning extends React.Component {
   constructor() {
     super()
     this.state = { 
       title: '',
       goal: '',
       tech: '',
       features: '',
       details: ''
    }

     this.handleChange = this.handleChange.bind(this)
     this.handleSubmit = this.handleSubmit.bind(this)
   }

handleChange(event) {

this.setState({ 
  [event.target.name]: event.target.value 
})
}

handleSubmit(event) {
alert(`Plan:  ` )
event.preventDefault()
}
    render() {
      return (
        <form onSubmit={this.handleSubmit}>
        <div>
          <label class="label-title">
            Project Title:</label>
            <input name="title" id="title" type="text" onChange={this.handleChange}/>
            </div>
            <div>
          <label class="label-goal">
          Motivational Goal: </label>
          <input name="goal" id="goal" type="text" onChange={this.handleChange}/>
          </div>
          <div>
        <label class="label-tech">
        Technologies/tools:</label>
        <input name="tech" id="tech" type="text" onChange={this.handleChange}/>
        </div>
        <div>
      <label class="label-features">
      Features:</label>
      <input name="features" id="features" type="text" onChange={this.handleChange}/>
      </div>
      <div>
    <label class="label-details">
    Other details: </label>
    <input name="details" id="details" type="text" onChange={this.handleChange}/>
    </div>
          <input class="submit" type="submit" value="Submit" />
        </form>
      )
    }
  }

1 Answer 1

1

The values are stored in the state so you could

handleSubmit(event) {
  event.preventDefault();
  alert(`Plan:\n${JSON.stringify(this.state,null,2)}`);
}

or the more explicit approach

handleSubmit(event) {
  const {
    title,
    goal,
    tech,
    features,
    details
  } = this.state;
  event.preventDefault();
  alert(`Plan:  
    Title: ${title}
    Goal: ${goal}
    Tech: ${tech}
    Features: ${features}
    Details: ${details}
  `);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I used the second approach and it worked! @GabrielePetrioli

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.