0

This react code I am writing for accepting the data from test.php

  var BasicInputBox = React.createClass ({
                    render: function (){
            //getting the input field to display the details
                      return (
                       <div>
                        <tr><td>
                          <label>{this.props.label}</label></td>
                           <td><input type="text" onChange = {this.props.valChange} value = {this.props.value}/></td>
                        </tr>
                       </div>
                      );
                    }
                });
                var DataInTableFormat = React.createClass({
                    getInitialState: function() {
                      return {
                        phpData : [],//initialize the data and the id
                        id : ''
                      };
                    },
                    componentDidMount: function() {
                      var url = document.URL;  
                      var currentId = url.substring(url.lastIndexOf('?') + 4);//getting the current ID of the student to and send it to the test.php through AJAX.
                      var data = {
                      idtoUpdate: currentId
                      };
                      $.ajax({
                        type: 'POST',
                        url: 'test.php',
                        data: data
                        })//post the ID through AJAX to test.php this will works fine.
                        .done(function(data) {

                        })
                        .fail(function(jqXhr) {
                        console.log('failed to register');
                        });
//getting the data returned from the test.php
                      $.get(this.props.source, function(result) { ////storing the JSON data in result
                          var collection = JSON.parse(result); // //coverting JSON data to Javascript object
                          if (this.isMounted()) {//checking for component mount.
                            this.setState({
                              phpData: collection //collecting the all JSON data
                           });
                          }
                        }.bind(this));
                      },
                    fetchData : function(){
                      console.log("onClick fetch")
                    },
                    nameChange: function(event){
                         this.setState({name: event.target.value})
                    },
                    render : function(){ 
                      DBdata = this.state.phpData || [];
                      return(
                        <div>
                              <table>
                                {DBdata.map(function(d){
                                 return(
                                    <BasicInputBox label="Student Name :" valChange={this.nameChange} value={d.name}/>
                                    )}
                                 )}
                                    <tr><td><td><button name="fetch" value="fetch" onClick={this.fetchData}>fetch</button></td></td></tr>
                                    <tr><td><td><button name="submit" value="submit">update</button></td></td></tr>
                                 </table>
                              </div>
                          )
                      }
                   });
                  React.render(
                    <DataInTableFormat source="http://localhost/PHP-React-Demo/test.php" />,
                      document.getElementById('Table-data')
                    );

When I am changing the path to the index.php(which prints the all data in a JSON form it works nice. And when test.php(which returns the single student information then I am getting the error

5
  • what is the error you get? Commented Dec 16, 2015 at 5:59
  • SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data Commented Dec 16, 2015 at 6:15
  • Notice: Undefined index: idtoUpdate in D:\wamp\www\PHP-React-Demo\test.php on line 6 Commented Dec 16, 2015 at 6:15
  • i could be wrong but it could be that your php returns an HTML page as a response rather than a JSON string. try doing console.log(result); before doing JSON.parse() and check the console panel of your browser for the output. Commented Dec 16, 2015 at 6:24
  • Possible duplicate of Print the JSON data in a text field in React JS Commented Dec 16, 2015 at 11:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.