title: Post data to a php api in react native
body: <p>I am trying to use react native to post the data into a php api in a mobile apps. But after I successful scan the barcode it read the data but it could not post the value to the api</p>
  <p>scanner.js</p>
  <pre><code>import React, { useState, useEffect,Component,onMount} from 'react';
  import { Text,TextInput, View, StyleSheet, Button } from 'react-native';
  import { BarCodeScanner } from 'expo-barcode-scanner';
  import {useNavigation} from'@react-navigation/native';
  import {StatusBar} from 'expo-status-bar';
  
  
  
   
  
    export default function Scanner () {
    
    const [hasPermission, setHasPermission] = useState(null);
    const [scanned, setScanned] = useState(false);
    const [userid, setText] = useState('Not yet scanned')
    const [currentDate, setCurrentDate] = useState('');
    const navigation = useNavigation();
  
  
    const askForCameraPermission = () =&gt; {
      (async () =&gt; {
        const { status } = await BarCodeScanner.requestPermissionsAsync();
        setHasPermission(status === 'granted');
      })()
    }
  
    // Request Camera Permission
    useEffect(() =&gt; {
      askForCameraPermission();
    }, []);
    
    
  
    Register = () =&gt; {
      this.state={
        userid:''
      }
      let userid = this.state.userid;
      
  
      
        let InsertAPIURL = &quot;http://localhost/api/insert.php&quot;;
  
        let headers = {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        };
  
        const Data={userid};
  
        fetch(InsertAPIURL, {
          method: 'POST',
          headers: headers,
          body: JSON.stringify(Data)
        })
        .then((response) =&gt;response.json())
        .then((response)=&gt;{
          alert(response[0].Message);
        })
        .catch((error) =&gt; {
          alert(&quot;Error&quot;+error);
        })
      
    }
    
  
    useEffect(() =&gt; {
      var date = new Date().getDate(); //Current Date
      var month = new Date().getMonth() + 1; //Current Month
      var year = new Date().getFullYear(); //Current Year
      var hours = new Date().getHours(); //Current Hours
      var min = new Date().getMinutes(); //Current Minutes
      var sec = new Date().getSeconds(); //Current Seconds
      setCurrentDate(
        date + '/' + month + '/' + year 
        + ' ' + hours + ':' + min + ':' + sec
      );
    }, []);
    // What happens when we scan the bar code
  
    const handleBarCodeScanned = ({ type, data }) =&gt; {
  
     
      setScanned(true);
       
       
       setText(data )
       
     
    };
    
  
   
    // Check permissions and return the screens
    if (hasPermission === null) {
      return (
        &lt;View style={styles.container}&gt;
          &lt;Text&gt;Requesting for camera permission&lt;/Text&gt;
        &lt;/View&gt;)
    }
    if (hasPermission === false) {
      return (
        &lt;View style={styles.container}&gt;
          &lt;Text style={{ margin: 10 }}&gt;No access to camera&lt;/Text&gt;
          &lt;Button title={'Allow Camera'} onPress={() =&gt; askForCameraPermission()} /&gt;
        &lt;/View&gt;)
    }
   
    // Return the View
    return (
       
      &lt;View style={styles.container}&gt;
        &lt;View style={styles.barcodebox}&gt;
          &lt;BarCodeScanner
            onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
            style={{ height: 400, width: 400 }} /&gt;
        &lt;/View&gt;
        
        &lt;Text style={styles.maintext}&gt;{userid + '\n'+currentDate}
      
        &lt;/Text&gt;
  
        
        {
          scanned &amp;&amp; &lt;Button title={'Scan again?'} onPress={() =&gt; setScanned(false)} color='tomato' /&gt;
           
        }
       
        {
          scanned &amp;&amp; &lt;Button title={'OK'}onPress={Register}/&gt; 
           
        }
      &lt;/View&gt;
    );
  }
  
  
  
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
    },
    maintext: {
      fontSize: 16,
      margin: 20,
    },
    barcodebox: {
      alignItems: 'center',
      justifyContent: 'center',
      height: 300,
      width: 300,
      overflow: 'hidden',
      borderRadius: 30,
      backgroundColor: 'tomato'
    }
  });
  </code></pre>
  <p>api php script</p>
  <pre><code>&lt;?php
      $CN = mysqli_connect('localhost', 'root', '');
      $DB = mysqli_select_db($CN, 'memberdata');
  
      $EncodedData = file_get_contents('php://input');
      $DecodedData = json_decode($EncodedData, true);
  
      $userid = $DecodedData['userid'];
      
  
      $insertMemberData = &quot;insert into member(user) values ('$userid')&quot;;
  
      $register = mysqli_query($CN, $insertMemberData);
  
      if ($register) 
          $Message = &quot;Member has been registered successfully&quot;;
      else 
          $Message = &quot;Server Error... please try latter&quot;;
  
      $Response[] = array(&quot;Message&quot; =&gt; $Message);
      echo json_encode($Response);
  ?&gt;
  </code></pre>
  <p>high chance the error will be in <strong>Register</strong> function and the <strong>return</strong> side</p>
  <p>I am new to react native I hope that someone could point out my mistake or error.</p>