简体   繁体   English

如何捕获用户输入并将其发送到服务器?

[英]How do you capture user inputs and send them to a server?

This may be already answered, but I can't find anything online about capturing user input and submitting the data into a POST request to a server. 可能已经有人回答了,但是我找不到关于捕获用户输入并将数据提交到服务器的POST请求中的任何在线信息。

I am using Material UI/React/JavaScript to implement this. 我正在使用Material UI / React / JavaScript来实现这一点。

<TextField
   id="outlined-email-input"
   label="email name"
   type="email name"
   name="email name"
   autoComplete="email name"
   margin="normal"
   variant="outlined"
   value={this.state.name}
   onChange={this.handleChange('name')}
/>

And I have another dropdown selection box, that allows the user to choose between four inputs. 我还有另一个下拉选择框,允许用户在四个输入之间进行选择。

<FormControl>
                      <InputLabel
                        ref={ref => {
                          this.InputLabelRef = ref;
                        }}
                        htmlFor="outlined-password-selection"
                      >
                        Password
                      </InputLabel>
                      <Select
                        value={this.state.age}
                        onChange={this.handleSubmit}
                        input={
                          <OutlinedInput
                            labelWidth={this.state.labelWidth}
                            name="Password"
                            id="outlined-age-simple"
                          />
                        }
                      >
                        <MenuItem value="BLUE">BLUE</MenuItem>
                        <MenuItem value="PINK">PINK</MenuItem>
                        <MenuItem value="GREEN">GREEN</MenuItem>
                        <MenuItem value="YELLOW">YELLOW</MenuItem>
                      </Select>
                    </FormControl>

Essentially, I want the user to enter their email and select a password, and then use this information in a POST request. 本质上,我希望用户输入电子邮件并选择一个密码,然后在POST请求中使用此信息。 To make this simpler, I want to capture user input and just console.log() them. 为了简化此过程,我想捕获用户输入,然后只对它们进行console.log()。 How do I do this? 我该怎么做呢?

You could create a controlled Form and attach an onSubmit event handler to the Form. 您可以创建一个受控的Form ,并将onSubmit事件处理程序附加到Form。 Once the Form is submitted, then in your handler you would have all the Form values and you can trigger the POST request. 提交表单后,您将在处理程序中拥有所有表单值,并且可以触发POST请求。

Here's a complete and working React example : 这是一个完整且有效的React示例

 class Form extends React.Component { constructor(props) { super(props); this.state = {} this.handleInputChange = this.handleInputChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) } handleSubmit(event) { console.log('Form data:', this.state) event.preventDefault() } // Registering and setting Form fields values and names dynamically handleInputChange(event) { const target = event.target const value = target.type === 'checkbox' ? target.checked : target.value const name = target.name this.setState({ [name]: value }) } render() { return ( <form onSubmit={this.handleSubmit}> <label> Email: <input name="email" type="text" value={this.state.email} onChange={this.handleInputChange} /> </label> <br /> <label> Password: <input name="password" type="password" value={this.state.password} onChange={this.handleInputChange} /> </label> <br /> <button type="submit">Submit</button> </form> ) } } ReactDOM.render( <Form />, document.getElementById('container') ) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将点击捕获为用户手势? - How do you capture a click as a user gesture? 将用户发送到新HTML时如何“记住”用户? - How to 'remember' user when you send them to a new HTML? 如何发送带有用户输入地址的HTML电子邮件? - How do I send an email with HTML with an address the user inputs? 如何每10秒捕获一次帧并将其发送到服务器? - How do i capture frame every 10 seconds and send it to the server? 如何拍摄两个单选按钮的输入并将其输出为图像? - How do you take two radio button's inputs and output them as an image? 如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量? - How do I take user inputs from a textfield and make them variables in a Google Script? 如何在客户端和服务器之间发送和接收相同的图像? - How do you send and receive the same image between the client and the server? 你如何在窗口捕获mouseout? - How do you capture mouseout on window? 用户不提交新项目详细信息时如何捕获? - How to capture new item details when a user does not submit them? 是否有任何 javascript 库来捕获鼠标/键盘事件并将它们发送到外部服务器? - Is there any javascript library to capture mouse/keyboards events and send them to external server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM