Home > Back-end >  How to retrieve the value of SELECT tag in reactjs?
How to retrieve the value of SELECT tag in reactjs?

Time:10-31

I have a react project where i want to retrieve the form data. the form contains text and Select tag. i have successfully retrieve the all text fields but i have a problem in retrieving the select tag.

How to get the value of select tag?

As i have tried some previous question but it doesn't helps. i want to retrieve the value with this code

const Register = () => {
  const [newUser, setNewUser] = useState({
    identity: "",
    names: "",
    phone: "",
    email: "",
    city: "",
    address: "",
    subject: "",
    classes: "",
    message: "",
  });



  const handleSubmit = (e) => {
    e.preventDefault();

    axios({
      method: "post",
      url: "/register",
      data: JSON.stringify(newUser),
      headers: { "Content-Type": "application/json" },
    })
      .then((response) => {
        console.log(response);
      })
      .then((data) => {
        console.log(data);
      })
      .catch((error) => {
        if (error.response) {
          console.log(error.response.data);
        }
      });
  };

  const handleChange = (e) => {
    setNewUser({ ...newUser, [e.target.name]: e.target.value });
  };



  return (
    <>
      <div class="container register mt-5">
        <h1 class="well text-register shadow p-4 rounded-3">
          Join as
          <label class="student">
            [
            <img src="https://img.icons8.com/external-vitaliy-gorbachev-lineal-color-vitaly-gorbachev/60/000000/external-man-avatars-vitaliy-gorbachev-lineal-color-vitaly-gorbachev-12.png" />
            Parent/Student
            <img src="https://img.icons8.com/external-justicon-lineal-color-justicon/64/000000/external-student-back-to-school-justicon-lineal-color-justicon.png" />
            ]
          </label>
          <h1 class="well text-register down">
            <i class="fas fa-arrow-right"></i> Hey! If you are looking for right
            tutor near by your location. Don't worry you are at right place.
            <i class="fas fa-arrow-left"></i>
          </h1>
        </h1>

        <div class="col-lg-12 well rounded-3">
          <div class="row mt-5 ">
            <form onSubmit={handleSubmit} method="post" class="shadow p-5">
              <div class="col-sm-12">
                <div class="row">
                  <div class="col-sm-12 form-group">
                    <label>

                      <i class="far fa-dot-circle"></i> Your Identity
                    </label>
                    <select>
                      <option value="student">Student</option>
                      <option value="teacher">Teacher</option>
                    </select>
                    {/* <input
                    type="text"
                    placeholder="Write Student or Parent"
                    class="form-control"
                    name="identity"
                    id="identity"
                    value={newUser.identity}
                    onChange={handleChange}
                  /> */}
                  </div>
                </div>
                <div class="form-group">
                  <label>

                    <i class="far fa-dot-circle"></i> Name
                  </label>
                  <input
                    type="text"
                    placeholder="Enter Your Full Name Here.."
                    class="form-control"
                    name="names"
                    id="names"
                    value={newUser.names}
                    onChange={handleChange}
                  />
                </div>
                <div class="form-group">
                  <label>

                    <i class="far fa-dot-circle"></i> Phone Number
                  </label>
                  <input
                    type="text"
                    placeholder="Enter Phone Number Here.."
                    class="form-control"
                    name="phone"
                    id="phone"
                    value={newUser.phone}
                    onChange={handleChange}
                  />
                </div>
                <div class="form-group">
                  <label>

                    <i class="far fa-dot-circle"></i> Email Address
                  </label>
                  <input
                    type="text"
                    placeholder="Enter Email Address Here.."
                    class="form-control"
                    name="email"
                    id="email"
                    value={newUser.email}
                    onChange={handleChange}
                  />
                </div>
                <div class="row">
                  <div class="col-sm-6 form-group">
                    <label>

                      <i class="far fa-dot-circle"></i> City
                    </label>
                    <input
                      type="text"
                      placeholder="Enter City Name Here.."
                      class="form-control"
                      name="city"
                      id="city"
                      value={newUser.city}
                      onChange={handleChange}
                    />
                  </div>
                  <div class="col-sm-6 form-group">
                    <label>

                      <i class="far fa-dot-circle"></i> Address
                    </label>
                    <textarea
                      placeholder="Enter Address Here.."
                      rows="3"
                      class="form-control"
                      name="address"
                      id="address"
                      value={newUser.address}
                      onChange={handleChange}
                    ></textarea>
                  </div>
                </div>
                <div class="row">
                  <div class="col-sm-6 form-group">
                    <label>

                      <i class="far fa-dot-circle"></i> Subject
                    </label>
                    <input
                      type="text"
                      placeholder="Enter Subject Here.."
                      class="form-control"
                      name="subject"
                      id="subject"
                      value={newUser.subject}
                      onChange={handleChange}
                    />
                  </div>
                  <div class="col-sm-6 form-group">
                    <label>

                      <i class="far fa-dot-circle"></i>Student Class
                    </label>
                    <input
                      type="text"
                      placeholder="Enter Student Class"
                      class="form-control"
                      name="classes"
                      id="classes"
                      value={newUser.classes}
                      onChange={handleChange}
                    />
                  </div>
                </div>

                <div class="form-group">
                  <label>

                    <i class="far fa-dot-circle"></i> Message
                  </label>
                  <textarea
                    placeholder="Enter Any message you have for us..."
                    rows="3"
                    class="form-control"
                    name="message"
                    id="message"
                    value={newUser.message}
                    onChange={handleChange}
                  ></textarea>
                </div>
                <button value="register" type="submit" class="btn btn-lg btn-info">
                  Submit
                </button>

              </div>
            </form>
          </div>

        </div>
      </div>
    </>
  );
};

CodePudding user response:

According to your code you can retrieve the value like this:

<select
  name="identity"
  value={newUser.identity}
  onChange={handleChange}
>
    <option value="student">Student</option>
    <option value="teacher">Teacher</option>
</select>

You can take a look at this sandbox for a live working example.

CodePudding user response:

you have to set the default object property or attach state with the value of DOM Element. so whenever it gets change your dispatcher gets called.

The only thing you are missing is you have not handled the userChange for your select box.

<select 
    name="identity" value={newUser.identity} onChange={handleChange}> 
     <option value="student">Student</option>
     <option value="teacher">Teacher</option>
</select>
  • Related