简体   繁体   中英

send data through ajax in react js

The following is my react code

var AddRecord = React.createClass({
    componentDidMount: function() {


    },
    render: function() {
        return (
         <form action="process.php" method="post">
            <table><tr><td>Enter Id</td><td><input type="text" name="Id"/></td></tr>
                   <tr><td>Enter Name</td><td><input type="text" name="name"/></td></tr>
                   <tr><td>Enter Email</td><td><input type="text" name="Email"/></td></tr>
                   <tr><td>Enter Phone</td><td><input type="text" name="Phone"/></td></tr>
                   <tr><td>Enter Marks</td><td><input type="text" name="Marks"/></td></tr>
                   <tr><td></td><td><input type="submit" name="submit" value="submit"/></td></tr>
             </table>
           </form>
        );
    }
});

React.render(<AddRecord/>, document.getElementById('form-data'));

I just want to send this data through AJAX in a json format in react js.

You can use the serialize JQuery function to serialize the code in an handleSumit function. You can also do the serialization by hand.

  <form onSubmit={this._handleSubmit}>
            <input type="text" name="Id"/>      
            <input type="text" name="name"/>
            <input type="submit" value="Submit the form"/>
  </form>

then in the handle submit function you can

_handleSubmit(e){
    e.preventDefault();
  if (valid) {
   //serialize the form and send ajax request
  }
}

jQuery AJAX form data serialize using PHP if you use JQuery to do your ajax request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM