Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize form data to json react

How I can serialize data in react ?

<form method="post" action="/insert" onSubmit={(e)=>this.addMysql(e)}>
    <label>Username</label>
    <input type="text" id="username" name="nameUser"/>
    <label>Password</label>
    <input type="text" id="password" name="passs"/>
    <button type="submit" type="submit">Dodaj</button>
</form>

This is my function

addMysql(event){
    console.log(event.targe) //this outputs an empty form
    event.preventDefault();
}

I dont want use State. Just onSubmit send all form data to AddMysql

https://medium.com/@snirlugassy/generic-input-handler-with-react-js-44a97e22cd0d

Thanks Sag1v

like image 841
Paweł Baca Avatar asked Sep 18 '26 14:09

Paweł Baca


1 Answers

I suggest you use states because that is the Reacty way. if not, I may suggest a few things.

Normally I would have suggested to use FormData API, but it is not supported by IE and older browsers.

I think this answer should get you going, because its a plain JS version of serialize.

Finnally serialize if you're using jQuery, which I think you're not or you're not supposed to.

like image 80
Minato Avatar answered Sep 20 '26 03:09

Minato