通过表单更新JSON? [关闭]

                <div class="grid--cell fl1 lh-lg">
                    <div class="grid--cell fl1 lh-lg">
                        As it currently stands, this question is not a good fit for our Q&amp;A format. We expect answers to be supported by facts, references,   or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question   can be improved and possibly reopened, <a href="/help/reopen-questions">visit the help center</a> for guidance.

                    </div>
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-06-03 08:18:26Z" class="relativetime">6 years ago</span>.</div>
        </div>
    </aside>

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

</div>

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)

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)