-1

I'm trying to do a signup form in my nativescript app, i'm using fetch api to POST the data. But right now it just populates the database without actually uploading the data. this is what i've tried

JS

const phpData = {
                email: "user@nativescript.org",
                password: "password",
                name: "Adekunle Adeyeye",
                number: "07019888741",
            }
            /*email = viewModel.getViewById("email").value;
            password = viewModel.getViewById("password").value;
            name = viewModel.getViewById("name").value;
            number = viewModel.getViewById("number").value;*/

            fetch("https://goodsbuy.000webhostapp.com/register.php", {
                method: 'POST',
                body: JSON.stringify(phpData),
                headers:{
                    "content-type": "application/json; charset=UTF-8"
                }
            })
            .then(response => response.json())
            .then(json => console.log(json))
            .then((phpData) => {
                console.log('Success:', phpData);
                this.set("processing", false);
                Toast.makeText("Successful").show();
                this.isLoggingIn = true;
              })
              .catch((error) => {
                console.error('Error:', error);
                this.set("processing", false);
                Toast.makeText("Account Already exists").show();
                this.isLoggingIn = false;
              });

PHP

$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
      $sql = "INSERT INTO comedyapp(`name`, `email`, `number`, `password`) VALUES ('$name', '$email', '$number', '$password')";
        if ($conn->query($sql) === TRUE) {
            echo '{"items":'. json_encode('Good') .'}'; 
        } else {
            echo '{"items":'. json_encode('Bad') .'}';
        }

Please assist me.

kunlee
  • 525
  • 2
  • 12

1 Answers1

0

Replace

$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];

with

extract(json_decode(file_get_contents('php://input'),true));

it will automatically declares the variables.

flash
  • 391
  • 2
  • 6
  • It's way better to keep the array as an array in one variable than using `extract()`. Specially when the data comes from the front end. – M. Eriksson Jan 28 '20 at 16:20
  • yes, agree with your comment. will look forward to it. – flash Jan 28 '20 at 16:23
  • 1
    Like the documentation for [extract()](https://www.php.net/manual/en/function.extract.php) says: _"Warning Do not use extract() on untrusted data, like user input (e.g. $_GET, $_FILES)."_ (that includes post data) – M. Eriksson Jan 28 '20 at 16:26
  • 1
    thank you very much @MagnusEriksson. will definitely keep it in mind. – flash Jan 28 '20 at 16:27