1

i have some json that i keep updated manually, but as the data is sent to me via e-mail, the more data the comes in the more time i'm spending writing json

is there a way to build a form that will take some data (text & uploads), and write/output JSON into a file or database? - i've tried researching with Ajax, PHP, MySQL and others with no luck, please advise (point me in the right direction)

My Structure:

    {
    "FirstName": "John",
    "LastName": "Doe",
    "Photo": "http://ImageURL",
    "PhoneNumber": [
        {
            "MobilePhone": "555-555-5555",
            "Provider": "Verizon"
        },
        {
            "HomePhone": "555-555-5555",
            "Provider": "AT&T"
        },
        {
            "WorkPhone": "555-555-5555",
            "Provider": "SBC"
        }
    ]
}

Form needed

First name: (Text input)

Last Name: (Text input)

Photo: (File upload)

(Nested under "Phone Number")

Mobile Phone: (Text input)

Provider: (Text input)

Home Phone: (Text input)

Provider: (Text input)

Work Phone: (Text input)

Provider: (Text input)

note: this is dummy data, the actual JSON Dictionary is used for music albums and the address to the file is http://www.godsgypsychristianchurch.net/music.json

Michael
  • 183
  • 3
  • 11
  • 1
    If you have a database, why would you save it in JSON format? Save each form field to an appropriate database column. But if you're saving to a flat file, just write put all the form data into an array and write `json_encode()` to the file. – Barmar Jun 03 '13 at 02:18
  • You have provided almost all the fields necessary to create a table in the database. For constantly updating/growing data, a database is always better. Storing values in JSON or XML should best be kept for static data that does not change a lot. – francisco.preller Jun 03 '13 at 02:28
  • @francisco.preller How would i build a form to input the data into a database, and i need the JSON to output from that database because my website and mobile app i'm working on depends on that data (JSON) – Michael Jun 03 '13 at 02:36
  • You are able to convert PHP array data to JSON as demonstrated on your chosen answer. Instead of retrieving the data, you can just query the database and then convert the results to JSON. – francisco.preller Jun 03 '13 at 03:07

2 Answers2

2

Try this:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    move_uploaded_file($_FILES["Photo"]["tmp_name"],"images/".$_POST['Photo']);

    $filters=array(
        "FirstName",
        "LastName",
        "Photo",
        "MobilePhone",
        "MobilePhoneProvider",
        "HomePhone",
        "HomePhoneProvider",
        "WorkPhone",
        "WorkPhoneProvider"
    );

    $final=array();

    foreach ($filters as $filter) {
        $final[$filter]=$_POST[$filter]?$_POST[$filter]:"";
    }

    $final["PhoneNumber"]=array(
        array(
            "MobilePhone"=>$final["MobilePhone"],
            "Provider"=>$final["MobilePhoneProvider"],
        ),
        array(
            "HomePhone"=>$final["HomePhone"],
            "Provider"=>$final["HomePhoneProvider"],
        ),
        array(
            "WorkPhone"=>$final["WorkPhone"],
            "Provider"=>$final["WorkPhoneProvider"],
        )
    );

    $unsets=array(
        "MobilePhone",
        "MobilePhoneProvider",
        "HomePhone",
        "HomePhoneProvider",
        "WorkPhone",
        "WorkPhoneProvider"
    );

    foreach ($unsets as $unset) {
        unset($final[$unset]);
    }

    echo json_encode($final);
    exit;
}
?><!DOCTYPE html>
<html>
    <head>
        <title>Contact</title>
    </head>
    <body>
        <form action="" method="post" enctype="multipart/form-data">
            First Name: <input type="text" name="FirstName"><br>
            Last Name: <input type="text" name="LastName"><br>
            Photo: <input type="file" name="Photo"><br>
            Mobile Phone: <input type="tel" name="MobilePhone"><br>
            Mobile Phone Provider: <input type="text" name="MobilePhoneProvider"><br>
            Home Phone: <input type="tel" name="HomePhone"><br>
            Home Phone Provider: <input type="text" name="HomePhoneProvider"><br>
            Work Phone: <input type="tel" name="WorkPhone"><br>
            Work Phone Provider: <input type="text" name="WorkPhoneProvider"><br>
            <input type="submit">
        </form>
    </body>
</html>

This is just a starting place, you will need to place image checks, and other various checks to ensure user input is valid. (For this test you should create a folder called images)

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • The form is working perfect, but how can the uploaded files be saved to the server and inputed into the JSON, and how would the JSON file be saved to the server (but all the data needs to be collected to one .JSON file) – Michael Jun 03 '13 at 02:34
  • You're saying multiple users will be placed in one json? It's possible but have you tried a database approach instead? – Dave Chen Jun 03 '13 at 02:49
0

Several libraries like Backbone.js provide helpful ways to do this. Check out the answers to this question (Serialize form inputs to JSON using Backbone.js) and see if that would work for your needs.

That said, have you considered just POSTing the form normally, then saving it to your database in a PHP function? Unless you need to put it into the DB as stringified JSON for some reason (which is not usually recommended) you can just go ahead and save the data to a MySQL database through the normal INSERT calls. (see this tutorial: http://php.about.com/od/phpbasics/ss/mysql_files.htm)

Community
  • 1
  • 1
Don McCurdy
  • 10,975
  • 2
  • 37
  • 75