How to pass data from a React Native app to a PHP script?












0















I'm using this code to pass the userName variable...



return fetch('http://creat1vedesign.com/userTabs4.php', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, {
body: JSON.stringify({
userName: 'carolf'
}),


...to this php script...



<?php
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$userName = $obj['userName'];
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>


...to retrieve data for a specific criteria. But either the React Native is not sending the data or the php script is not receiving it or the syntax is incorrect or I'm doing something else wrong :(



Any thoughts?










share|improve this question























  • You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

    – Devon
    Nov 13 '18 at 18:07











  • Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

    – Solomon Antoine
    Nov 13 '18 at 18:36











  • Could you post complete fetch please?

    – schogges
    Nov 13 '18 at 18:41











  • you can use var_dump($_POST) for see what php is receiving from your react app.

    – Roger Russel
    Nov 13 '18 at 18:46











  • no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

    – Carlos Teixeira
    Nov 13 '18 at 18:58
















0















I'm using this code to pass the userName variable...



return fetch('http://creat1vedesign.com/userTabs4.php', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, {
body: JSON.stringify({
userName: 'carolf'
}),


...to this php script...



<?php
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$userName = $obj['userName'];
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>


...to retrieve data for a specific criteria. But either the React Native is not sending the data or the php script is not receiving it or the syntax is incorrect or I'm doing something else wrong :(



Any thoughts?










share|improve this question























  • You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

    – Devon
    Nov 13 '18 at 18:07











  • Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

    – Solomon Antoine
    Nov 13 '18 at 18:36











  • Could you post complete fetch please?

    – schogges
    Nov 13 '18 at 18:41











  • you can use var_dump($_POST) for see what php is receiving from your react app.

    – Roger Russel
    Nov 13 '18 at 18:46











  • no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

    – Carlos Teixeira
    Nov 13 '18 at 18:58














0












0








0








I'm using this code to pass the userName variable...



return fetch('http://creat1vedesign.com/userTabs4.php', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, {
body: JSON.stringify({
userName: 'carolf'
}),


...to this php script...



<?php
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$userName = $obj['userName'];
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>


...to retrieve data for a specific criteria. But either the React Native is not sending the data or the php script is not receiving it or the syntax is incorrect or I'm doing something else wrong :(



Any thoughts?










share|improve this question














I'm using this code to pass the userName variable...



return fetch('http://creat1vedesign.com/userTabs4.php', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, {
body: JSON.stringify({
userName: 'carolf'
}),


...to this php script...



<?php
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$userName = $obj['userName'];
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>


...to retrieve data for a specific criteria. But either the React Native is not sending the data or the php script is not receiving it or the syntax is incorrect or I'm doing something else wrong :(



Any thoughts?







php react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 17:54









Carlos TeixeiraCarlos Teixeira

103




103













  • You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

    – Devon
    Nov 13 '18 at 18:07











  • Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

    – Solomon Antoine
    Nov 13 '18 at 18:36











  • Could you post complete fetch please?

    – schogges
    Nov 13 '18 at 18:41











  • you can use var_dump($_POST) for see what php is receiving from your react app.

    – Roger Russel
    Nov 13 '18 at 18:46











  • no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

    – Carlos Teixeira
    Nov 13 '18 at 18:58



















  • You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

    – Devon
    Nov 13 '18 at 18:07











  • Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

    – Solomon Antoine
    Nov 13 '18 at 18:36











  • Could you post complete fetch please?

    – schogges
    Nov 13 '18 at 18:41











  • you can use var_dump($_POST) for see what php is receiving from your react app.

    – Roger Russel
    Nov 13 '18 at 18:46











  • no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

    – Carlos Teixeira
    Nov 13 '18 at 18:58

















You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

– Devon
Nov 13 '18 at 18:07





You shouldn't need a while loop and your query is open to SQL injection attacks. Beyond that, are you receiving any error messages?

– Devon
Nov 13 '18 at 18:07













Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

– Solomon Antoine
Nov 13 '18 at 18:36





Not sure if this will fix your issue but I'd recommend using $con->query($sql) within your if statement because you're mixing procedural mysqli and oop mysqli.

– Solomon Antoine
Nov 13 '18 at 18:36













Could you post complete fetch please?

– schogges
Nov 13 '18 at 18:41





Could you post complete fetch please?

– schogges
Nov 13 '18 at 18:41













you can use var_dump($_POST) for see what php is receiving from your react app.

– Roger Russel
Nov 13 '18 at 18:46





you can use var_dump($_POST) for see what php is receiving from your react app.

– Roger Russel
Nov 13 '18 at 18:46













no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

– Carlos Teixeira
Nov 13 '18 at 18:58





no error message... I simply don't get any result from the select... changing to $con->query($sql) didn't help

– Carlos Teixeira
Nov 13 '18 at 18:58












1 Answer
1






active

oldest

votes


















0














You can pass the parameters to the PHP script in the fetch URL by adding them after a '?' like this:



fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
method:'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})

.then( (response) => {
return response.json() })
.then( (json) => {
console.log(json)
});


Just make sure to include a $_GET in your php script:



<?php

$userName= $_GET["userName"];

include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);

$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286893%2fhow-to-pass-data-from-a-react-native-app-to-a-php-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can pass the parameters to the PHP script in the fetch URL by adding them after a '?' like this:



    fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
    method:'POST',
    headers:{
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    },
    })

    .then( (response) => {
    return response.json() })
    .then( (json) => {
    console.log(json)
    });


    Just make sure to include a $_GET in your php script:



    <?php

    $userName= $_GET["userName"];

    include 'DBConfig.php';
    $con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
    $json = file_get_contents('php://input');
    $obj = json_decode($json,true);

    $sql = "select * from Users where userName = '$userName'";
    if ($result = mysqli_query($con,$sql)) {
    while($row = $result->fetch_assoc()) {
    $tem = $row;
    $json = json_encode($tem);
    }
    } else {
    echo "No Results Found.";
    }
    echo $json;
    $conn->close();
    ?>





    share|improve this answer




























      0














      You can pass the parameters to the PHP script in the fetch URL by adding them after a '?' like this:



      fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
      method:'POST',
      headers:{
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      },
      })

      .then( (response) => {
      return response.json() })
      .then( (json) => {
      console.log(json)
      });


      Just make sure to include a $_GET in your php script:



      <?php

      $userName= $_GET["userName"];

      include 'DBConfig.php';
      $con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
      $json = file_get_contents('php://input');
      $obj = json_decode($json,true);

      $sql = "select * from Users where userName = '$userName'";
      if ($result = mysqli_query($con,$sql)) {
      while($row = $result->fetch_assoc()) {
      $tem = $row;
      $json = json_encode($tem);
      }
      } else {
      echo "No Results Found.";
      }
      echo $json;
      $conn->close();
      ?>





      share|improve this answer


























        0












        0








        0







        You can pass the parameters to the PHP script in the fetch URL by adding them after a '?' like this:



        fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
        method:'POST',
        headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        },
        })

        .then( (response) => {
        return response.json() })
        .then( (json) => {
        console.log(json)
        });


        Just make sure to include a $_GET in your php script:



        <?php

        $userName= $_GET["userName"];

        include 'DBConfig.php';
        $con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
        $json = file_get_contents('php://input');
        $obj = json_decode($json,true);

        $sql = "select * from Users where userName = '$userName'";
        if ($result = mysqli_query($con,$sql)) {
        while($row = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);
        }
        } else {
        echo "No Results Found.";
        }
        echo $json;
        $conn->close();
        ?>





        share|improve this answer













        You can pass the parameters to the PHP script in the fetch URL by adding them after a '?' like this:



        fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
        method:'POST',
        headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        },
        })

        .then( (response) => {
        return response.json() })
        .then( (json) => {
        console.log(json)
        });


        Just make sure to include a $_GET in your php script:



        <?php

        $userName= $_GET["userName"];

        include 'DBConfig.php';
        $con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
        $json = file_get_contents('php://input');
        $obj = json_decode($json,true);

        $sql = "select * from Users where userName = '$userName'";
        if ($result = mysqli_query($con,$sql)) {
        while($row = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);
        }
        } else {
        echo "No Results Found.";
        }
        echo $json;
        $conn->close();
        ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 18 '18 at 23:09









        Victor BurnettVictor Burnett

        345




        345






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286893%2fhow-to-pass-data-from-a-react-native-app-to-a-php-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Xamarin.form Move up view when keyboard appear

            JBPM : POST request for execute process go wrong