You can use http to post data to PHP
Future<void> sendData() async {
var url = Uri.parse('http://localhost/getdata.php');
var response = await http.post(url, body: {
'email': myemail,
'password': password.text,
});
if (response.statusCode == 200) {
print('Data sent successfully: ${response.body}');
} else {
print('Failed to send data. Status code: ${response.statusCode}');
}
}
You'll need to install and import http import 'package:http/http.dart' as http;
and dependencies: http: ^0.13.3
And the PHP part
<?php
// Set correct content type
header('Content-Type: application/json');
// Connect to your MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(json_encode(['error' => 'Failed to connect to the database']));
}
// Get POST data
$email = $_POST['email'];
$password = $_POST['password'];
// Perform your MySQL query (e.g., insert, update, or search)
$query = "INSERT INTO users (email, password)
VALUES ('$email', '$password')";
if ($conn->query($query) === TRUE) {
echo json_encode(['success' => 'Data inserted successfully']);
} else {
echo json_encode(['error' => 'Failed to insert data']);
}
$conn->close();
?>