Pengaturan

Gambar

Lainnya

Tentang KASKUS

Pusat Bantuan

Hubungi Kami

KASKUS Plus

© 2024 KASKUS, PT Darta Media Indonesia. All rights reserved

AD13LAvatar border
TS
AD13L
[Ask] Input form PHP, data file txt, data tidak sama
Gan, ane buat form pake PHP, data disimpan di file.txt. Gimana caranya supaya inputan form tidak bisa sama (data sudah ada)? Mohon bantuannya. Terima Kasih

form.php
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="form">
<h3>Data Input</h3>
<form action="save_json.php" method="post">
Nama tempat: <input type="text" name="title" id="title" /></br>
Latitude: <input type="text" name="lat" id="lat" /></br>
Longitude: <input type="text" name="lng" id="lng" /></br>
Deskripsi: <input type="text" name="description" id="description" /></br>
Kategori: <input type="text" name="category" id="category" /></br>
<input type="submit" id="submit" value="Submit" />
</form>
<div>
</body>
</html>


save_json.php
Code:
<?php
// Append new form data in json string saved in text file
// From: http://coursesweb.net/php-mysql/

// path and name of the file
$filetxt = '/opt/lampp/htdocs/project/data.txt';

// check if all form data are submited, else output error message
if(isset($_POST['title']) && isset($_POST['lat']) && isset($_POST['lng']) && isset($_POST['description']) && isset($_POST['category'])) {
// if form fields are empty, outputs message, else, gets their data
if(empty($_POST['title']) || empty($_POST['lat']) || empty($_POST['lng']) || empty($_POST['description']) || empty($_POST['category'])) {
echo 'Semua field harus diisi';
}
else {
// gets and adds form data into an array
$data = array(
'title'=> $_POST['title'],
'lat'=> (float) $_POST['lat'],
'lng'=> (float) $_POST['lng'],
'description'=> $_POST['description'],
'category'=> $_POST['category'],
);

// path and name of the file
$filetxt = '/opt/lampp/htdocs/project/data.txt';

$arr_data = array(); // to store all form data

// check if the file exists
if(file_exists($filetxt)) {
// gets json-data from file
$jsondata = file_get_contents($filetxt);

// converts json string into array
$arr_data = json_decode($jsondata, true);
}

// appends the array with new form data
$arr_data[] = $data;

// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

// saves the json string in "data.txt" (in "dirdata" folder)
// outputs error message if data cannot be saved
if(file_put_contents('/opt/lampp/htdocs/project/data.txt', $jsondata)) echo 'Data berhasil disimpan';
else echo 'Tidak dapat menyimpan data di "/opt/lampp/htdocs/project/data.txt"';
}
}
else echo 'Form tidak terkirim';
?>


data.txt
Code:
[
{
"title": "test",
"lat": -6.984,
"lng": 110.41,
"description": "test",
"category": "test"
}
]
0
1.6K
1
GuestAvatar border
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Urutan
Terbaru
Terlama
GuestAvatar border
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Komunitas Pilihan