2013-06-03 35 views
1

更新JSON數據這是輸出我需要:創建表單與MySQL

{ 
    "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文件。

+0

是軌道的數目不變或變化? – Fallexe

+0

@Fallexe變量 – Michael

回答

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 
} 
?> 
+0

完全驚人! – Michael

2

您可以使用以數組索引ex命名的表單元素創建表單: <input type="text" name="record[album]"/>依此類推。

將表單發佈到服務器後,經過驗證,您可以使用json_encode($_POST['record'])獲得所需的json輸出。

1

要添加到Shoan的答案:您還需要創建一個帶有JavaScript處理程序的按鈕以創建更多輸入,以便用戶可以根據需要添加更多軌道。額外的輸入都需要這個樣子:

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

哪裏n是下一首曲目的數量。