What causes the select field value to be combined with the values of other fields in the form?

When I select a value from the dropdown menu, the selected value is being added to the other fields as well. Here is the code snippet that was causing the issue:

import React, { Component } from 'react';
import './C.css';
import {ButtonToolbar,Button} from 'react-bootstrap';
import axios from 'axios';
export default class Coldata extends React.Component {
        constructor(props) {
        super(props);
        this.state = {value: 'select dept'};  
        this.handleChange = this.handleChange.bind(this);
        this.state={
          Regdno:'',
          Yos:'',
          dept:'',
          complaint:''
        };
      }

      onChange = (e) => {
        console.log(e.target.Regdno)
        const state = this.state
        state[e.target.Regdno] = e.target.value;
        this.setState(state);
      }

      onSubmit = (e) => {
        e.preventDefault();
        const data = this.state;
          axios.post('http://localhost:80/create/complaint', data) 
          .then((result) => {
            alert("Complaint created")
          })
      }

      handleChange(event) {
        this.setState({value: event.target.value});
      }

    render() {
      const{Regdno,Yos,dept,complaint}=this.state;

        return (            
    <form onSubmit={this.onSubmit}  className="col" >
<div align="right"><Button bsSize="large" href="/login"  value="Logout"> Logout </Button></div>
    <font size="5" >
  <div className="log">
  <h1> College </h1>
  <p> Regd_no </p>
  <input type="text" name="Regdno" id="regdno" size="10" placeholder="Enter Regd_no" value={this.state.value} onChange={this.onChange} required />
  <p> Year of study </p>
  <input type="text" name="Yos " size="5" placeholder="Enter year of study" value={this.state.value} onChange={this.onChange} />
  <label> Select Dept </label>
 <select value={this.state.value} onChange={this.handleChange} className="op" name="dept" id="dept" required>
    <option value="Cse">Cse</option>
  <option value="Ece">Ece</option>
  <option value="It">IT</option>
  <option value="EEE">EEE</option>
   <option value="Mech">Mech</option>
   <option value="Chem">Chem</option>
  </select>  
  <br />
  <label> Disciplinary Issue </label>
  <input type="text" size="20" name="complaint" value={this.state.value} onChange={this.onChange}  required/>
  <ButtonToolbar>
      <Button bsStyle="primary" bsSize="lg" type="submit" value="submit" block> ADD </Button>
    </ButtonToolbar>
   
          </div>
          </font>
        </form>
        );
      }
    }
.col{
margin: 0;
padding: 0;
background: url(./Nature.jpeg);
background-size: contain;
background-position: center;
font-family: sans-serief;
height: 768px
}
.log{
width: 320px;
height:570px;
background: rgba(0,0,0,0.5);
color: #fff;
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%,-50%);
box-sizing: border-box;
    padding: 70px 30px;
}
h1{
margin:0;
padding: 0 0 10px;
position: relative;
text-align:center;
font-size:22px;
}
.log p{
margin: 0;
padding: 0;
font-weight:bold;
}
.log input{
width:100%;
margin-bottom:10px;
}
.log input[type="text"],input[type="password"] :focus
{
border:none;
border-bottom: 1px solid #fff;
background:transparent;
outline:none;
height:60 px;
color: #fff;
font-size: 22px;
box-sizing: border-box; 
}
.log input[type="submit"]
{
 border:none;
 outline:none;
 background: #1c8adb;
 color: #fff;
 font-size: 20px;
 margin-top: 10px;
}
.op {
margin-top: 10px;
width:auto;
color: red;
height: 40px;
}

https://i.sstatic.net/5q6Oh.png

A warning message stating, "A component is changing an uncontrolled input of type text to be controlled." It's advised to decide between using a controlled or uncontrolled input element for the component's lifetime.

Answer №1

Erase the value={this.state.value} and onChange={this.onChange} attributes from the input fields with type="text".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to incorporate padding into an Angular mat tooltip?

I've been attempting to enhance the appearance of the tooltip dialog window by adding padding. While adjusting the width and background color was successful, I'm encountering difficulties when it comes to styling the padding: https://i.sstatic.ne ...

Error Encountered: An <Route> should always serve as a child element within a <Routes> container, not displayed independently. Kindly encase your <Route> within a <Routes> element

Encountering an error after implementing Private Route. The issue arises when attempting to access the Profile page which is nested in the application's structure. App.js import './App.css'; import { BrowserRouter, Route, Routes } from &ap ...

When using React / Next.js, the .map() function may not rerender the output based on changes in the state array if the keys remain the same

In my react component, Matches.js, I handle the display of tournament matches sorted by rounds. The data is fetched from a parent component through nextjs SSR and passed as props to the child component. To avoid unnecessary requests upon data changes like ...

Create a row in React JS that includes both a selection option and a button without using any CSS

My dilemma involves a basic form consisting of a select element and a button. What I want to accomplish is shifting the position of the form to the right directly after the select element https://i.sstatic.net/3gfkO.png Below is the code snippet that I ha ...

Tips on managing authentication flow in React and Firebase when waiting for a cookie before setting the authContext and login state

I am facing an issue with my code. I have a createUser function that communicates with an express server via axios to create a Firebase authenticated user, generate a token, and send the token back to the user as a cookie. Additionally, I have a getLogged ...

Using CSS, create a layout with a small 2x2 box positioned beside a larger 2x2 box

https://i.sstatic.net/nC2PI.png Can a flexible ul li structure be achieved with flexbox? I attempted to set the width of the squares to 25% and make the 1st, 3rd, or 5th one 50% wide using a combination of float:left, clear[left|right], but the last squar ...

The size of table td is less than 100%

I'm having trouble getting my td to fill 100% of my table. I have tried using the following code: .table-scroll tbody { display: block; overflow-y: scroll; height: calc(100% - 130px); } Even with the display block property, the td element is s ...

The footer seems to be malfunctioning

Let's start with the code: CSS: *{ margin:0; padding:0; } body,html{ height:100%; } body{ background:url('../images/bg.png'); background-position:center; background-repeat:no-repeat; background-attachment:fixed; height:100%; ...

Trouble persisting state in React

Upon opening a page, I have code that initializes a List Array as follows: const [myArray, updateArray] = useState([]); useEffect(() => { var myItem= [...array1]; if (state.pageNumber == 1 myItem= [...array2]; else if (state.p ...

Display a preloader image while waiting for the content to load using jQuery or AJAX

Hey there, I could really use some help with a little issue. I've been trying to use the click() and load() functions to bring my content into a specific CSS class. $("#feed").click(function(){ $(".homefeed").load("feedcontainer.php"); ...

CSS transformation: skewing without any blurring effects

Is there a way to create tilted drivers with borders and text without sacrificing the sharpness of the font and border? I have used the transform: skew(); CSS rule for tilting, but it has caused blurriness. How can I add font smoothing and maintain sharp b ...

Issues with overlapping floating labels in Bootstrap 5

Is there a way to display longer text without it being cut off or truncated, especially in input fields where the full content is necessary for clarity? It can be frustrating when only part of the text is visible. This issue arises from the Bootstrap 5 e ...

The problematic max-width with srcset attribute

Here's the HTML markup I'm working with: <picture> <source srcset="img/home-page/placeholders/placeholder_2560.jpg" media="(max-width: 2560px)"> <source srcset="img/home-page/placeholders/placeh ...

Struggling to increment the badge counter in a React Typescript project

I'm a bit unsure about where to apply the count in my Badge Component. The displayValue should include the count, but I'm struggling with how to implement it. UPDATE: The counter is now in place, but when I decrease the count and it reaches 0, t ...

Error: Unable to locate module: 'fs' in 'node_modulesdotenvlib' - Next.js

I'm currently incorporating dotenv into my React project to use for API_URL. However, when I attempt to implement it in the index.js file within Next.js, I encounter the following error: Module not found: Can't resolve 'fs' in 'nod ...

What is the best way to exhibit information retrieved from my express server on the React frontend?

I've been working on connecting an Express backend with MySQL data and a React frontend for iterating through the information. While I haven't encountered any errors, nothing seems to display on React. What could be missing? The following code ...

What steps should I take to incorporate Bootstrap into my React application effectively?

Just starting out with create-react-app and playing around with some code I generated. However, I can't seem to get my react-bootstrap to register properly. Any insights or suggestions would be greatly appreciated from those more experienced than myse ...

Unable to interact with menu in Internet Explorer

I am facing an issue with my code that should create a dropdown menu when hovered on, but it is not working properly in Internet Explorer. Instead of dropping down the menu, IE pushes it to the right, making it impossible to interact with. Surprisingly, th ...

Using React Material Design UI with the Routing feature

Currently, I am in the process of developing a simple React application with the assistance of the Material-UI library. After utilizing the create-react-app example provided, I attempted to integrate Router into it for seamless navigation between compone ...

Leap over in a similar fashion to the provided demonstration

In my attempt to create a unique project, I have created this CodePen example. The goal is to have one ball on the circle that remains in a fixed position, while another rotating main ball must avoid touching it at all costs. Points are awarded for success ...