1

I'm working on building a basic form submission app using React, JS, and some basic HTML. Within the app, I'm sending the results of the form submission as a POST request to an API.

The issue I am facing is I am seeing a connection to the API (getting a 200 with each submission) but the values I am inputting to the form are appearing as undefined. I believe this is related to the use of the handleSubmit function in my code, but I cannot figure out how to send the form data itself.

Example of my code below:

class Form extends React.Component {
  constructor(props) {
    super(props);

    // declare input fields in state
    this.state = { value: "" };

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

  // handle change events
  handleChange = (event) => {
    this.setState({
      value: event.target.value,
    });
  };

  // handle submission events
  handleSubmit = (events) => {
    const username = "- username -";
    const password = "- password - ";

    let data = `${username}:${password}`;
    let encodedString = btoa(data);
    let proxyLink = "- proxy URL -";
    let URL = "- endpoint URL -";
    let proxyURL = proxyLink + URL;

    const options = {
      method: "POST",
      body: JSON.stringify(this.state),
      headers: {
        accept: "application/json",
        authorization: `Basic ${encodedString}`,
      },
    };

    fetch(proxyURL, options)
      .then((response) => response.json())
      .then((response) => console.log(response))
      .catch((err) => console.error(err));
    events.preventDefault();
  };

  render() {
    const { values } = this.state;
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          <label>value: </label>
          <input
            type="text"
            value={attibute_name}
            onChange={this.handleChange}
            name="attribute_name"
          />
        </div>
      </form>
    );
  }
}

export default Form;

I've tried changing the way I'm using handleSubmit, even creating it as a variable and using an async function. The idea was similar to this (https://www.techomoro.com/submit-a-form-data-to-rest-api-in-a-react-app/) but it lead to worse results than what I had before.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Have you tried adding events.preventDefault( ) to the top of your handleSubmit function? – smritz Dec 07 '22 at 20:20
  • hey @smritz I just tried this and it did not change my results. I did however rework my handleChange function which is now eliminating the undefined values, but I'm still only getting a 200 ping from the API. handleChange = event => { const thisAttribute = event.target.thisAttribute; this.setState({ [event.target.name]: thisAttribute }); } – samohtebag513 Dec 07 '22 at 20:32
  • Using regular JavaScript's `FormData()` constructor is a popular way to get the values. https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData – Ronnie Royston Dec 07 '22 at 20:34
  • Hey @RonnieRoyston I'm trying this now but how would I use FormData within the handleSubmit function? It looks like FormData isn't being declared anywhere where it would collect the information from the return () – samohtebag513 Dec 07 '22 at 21:06
  • try `handleChange(event) {` instead of `handleChange = event => {` – Omer Dec 07 '22 at 22:24
  • I'm not sure. I use standard HTML/CSS/JavaScript. That code look complicated to me... – Ronnie Royston Dec 07 '22 at 23:18
  • What's `attribute_name`? It's not defined. I think you meant to use `const { value } = this.state` and `value={value}`. You're also missing `'content-type': 'application/json'` in your request headers – Phil Dec 09 '22 at 00:37

0 Answers0