1

这是我需要的输出:

{
    "album": "Text_Input",
    "artwork": "DefaultURL/http://www.ggcc.tv/ArmyoftheLord/Army%20of%20the%20Lord.png/OR Overwrite with Uploaded Image",
    "church": "City Name And State Wich can be selected from Dropdown Menu",
    "cityartwork": "Default URL Will Be Set/ This input is hidden",
    "des": "Text_Input_For_Description",
    "release_date": "February 24th 2013 ",
    "tracks": [
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        },
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        },
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        },
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        },
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        },
        {
            "name": "Text_Input",
            "url": "File Upload of .MP3 which should be saved on server and its new url should be inputed here by the script"
        }
    ]
}

我需要一个表单来收集所有这些信息,将其保存到数据库并将收集到的条目输出到服务器上的单个 JSON 文件中,以便我可以在我正在处理的应用程序中使用该 .json 文件。

4

3 回答 3

2

您可以使用以数组索引 ex: 命名的表单元素创建一个表单, <input type="text" name="record[album]"/>依此类推。

将表单发布到服务器后,您json_encode($_POST['record'])当然可以在验证后使用 来获取所需的 json 输出。

于 2013-06-03T03:28:49.807 回答
2

尝试这个:

<?php
$minimum_tracks=1;
$maximum_tracks=10;

$tracks=isset($_GET['tracks'])?$_GET['tracks']:0;

if (is_numeric($tracks) && $tracks>=$minimum_tracks && $tracks<=$maximum_tracks) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $_POST['cityartwork']="Default Set from PHP";
        $_POST['tracks']=array();
        $_POST['artwork']='http://www.ggcc.tv/ArmyoftheLord/Army%20of%20the%20Lord.png';
        if ($_FILES['artwork']['size']!=0) {
            move_uploaded_file($_FILES['artwork']['tmp_name'],"artworks/".$_FILES['artwork']['name']);
            $_POST['artwork']=$_SERVER['HTTP_HOST']."/artworks/".$_FILES['artwork']['name'];
        } 
        for ($i=0;$i<$tracks;$i++) {
            $filename="tracks/".$_FILES['tracks']['name'][$i];
            $_POST['tracks'][$i]=array(
                "name"=>$_POST['track_names'][$i],
                "url"=>$_SERVER['HTTP_HOST']."/".$filename
            );
            move_uploaded_file($_FILES['tracks']['tmp_name'][$i],$filename);
        }
        unset($_POST['track_names']); 
        echo json_encode($_POST);
        exit;
    } else {
        ?><!DOCTYPE html>
<html>
    <head>
        <title>New Album</title>
    </head>
    <body>
        <form method="post" action="" enctype="multipart/form-data">
            Album Name: <input type="text" name="album"><br>
            Artwork: <input type="file" name="artwork"><br>
            Church: <select name="church"><option value="New York NY">New York NY</option><option value="Los Angeles CA">Los Angeles CA</option></select><br>
            Description: <br><textarea name="des"></textarea><br>
            Release Date: <input type="date" name="release_date"><br>
            Tracks: <br><br><?php
                for ($i=1;$i<=$tracks;$i++) {
                    echo 'Track '.$i.'<br><input type="text" name="track_names[]"><input type="file" name="tracks[]"><br><br>';
                }
                ?>
            <input type="submit">
        </form>
     <?php 
        exit;
     }
} else {
?>
<!DOCTYPE html>
<html>
    <head>
        <title>New Album</title>
    </head>
    <body>
        How many tracks are in this album?
        <form action="" method="get">
            <select name="tracks">
            <?php
                for ($i=1;$i<$maximum_tracks;$i++) {
                    echo '<option value='.$i.'>'.$i.'</option>';
                }
            ?>
            </select><br>
            <input type="submit">
        </form>
    </body>
</html>
<?php
}
?>
于 2013-06-03T03:51:37.973 回答
1

添加到 Shoan 的答案:您还需要创建一个带有 javascript 处理程序的按钮来创建更多输入,以便用户可以根据需要添加更多轨道。附加输入需要如下所示:

<input type="text" name="record[tracks][n][name]"/>

n下一首曲目的号码在哪里。

于 2013-06-03T03:33:38.937 回答