Question

Here is my form html code

<form action="save_json.php" method="post">
Title: <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>
Description: <input type="text" name="description" id="description" /></br>
Category: <input type="text" name="category" id="category" /></br>
<input type="submit" id="submit" value="Submit" />

And, here is my save_json.php code

<?php
        // Append new form data in json string saved in text file

        // path and name of the file
    $filetxt = 'dirdata/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 'All fields are required';
        }
        else {
        // gets and adds form data into an array
        $data = array(
          'title'=> $_POST['title'],
          'lat'=> $_POST['lat'],
          'lng'=> $_POST['lng'],
          'description'=> $_POST['description'],
          'category'=> $_POST['category'],
        );

        // path and name of the file
        $filetxt = 'dirdata/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('dirdata/data.txt', $jsondata)) echo 'Data successfully saved';
        else echo 'Tidak dapat menyimpan data di "dirdata/data.txt"';
      }
    }
        else echo 'Form fields not submited';
    ?>

The output is like this

[
    {
    "title": "title",
    "lat": "-6.9000",
    "lng": "-110.000",
    "description": "description",
    "category": "category"
    }
]

The output that I want is the value at latitude & longitude without double quotes ( " ) like this

[
    {
    "title": "title",
    "lat": -6.9000, // <-- no double quotes
    "lng": -110.000,
    "description": "description",
    "category": "category"
    }
]

Anyone can help me how to solve it? Thank you for any help

Was it helpful?

Solution

$data = array(
          'title'=> $_POST['title'],
          'lat'=> (float) $_POST['lat'],
          'lng'=> (float) $_POST['lng'],
          'description'=> $_POST['description'],
          'category'=> $_POST['category'],
        );

Cast the strings into floats

OTHER TIPS

Have you tried this :

$data = array(
      'title'=> $_POST['title'],
      'lat'=> floatval($_POST['lat']),
      'lng'=> floatval($_POST['lng']),
      'description'=> $_POST['description'],
      'category'=> $_POST['category'],
    );

http://www.php.net/manual/fr/function.floatval.php

EDIT : too late ^^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top