Tristan.Seabrook Posted July 24, 2010 Share Posted July 24, 2010 I'm new at this I've created a file upload feature that uses JavaScript to create extra fields when the user clicks a button. The fields are automatically named when they're created, so the first song name field is named 'name_1' and the second, 'name_2', and so on. Because users can upload any number of files, I wanted to create a .php file that would automatically store all submitted fields. Unfortunately, it doesn't seem to work: //This returns results: $song = $_FILES['song_1']; $song_name = $_POST['name_1']; $song_artist = $_POST['artist_1']; $song_order = $_POST['order_1']; if ($song){echo "Song: " . $song . "<br />";} if ($song_name){echo "Song Name: " . $song_name . "<br />";} if ($song_artist){echo "Song Artist: " . $song_artist . "<br />";} if ($song_order){echo "Song Order: " . $song_order . "<br />";} //This returns nothing: for ($i=1; $i<20; 0) { $s = "'" . "song_" . $i . "'" . "<br />"; $n = "'" . "name_" . $i . "'" . "<br />"; $a = "'" . "artist_" . $i . "'" . "<br />"; $o = "'" . "order_" . $i . "'" . "<br />"; $song = $_FILES[$s]; $song_name = $_POST[$n]; $song_artist = $_POST[$a]; $song_order = $_POST[$o]; if ($song){echo "Song: " . $song . "<br />";} if ($song_name){echo "Song Name: " . $song_name . "<br />";} if ($song_artist){echo "Song Artist: " . $song_artist . "<br />";} if ($song_order){echo "Song Order: " . $song_order . "<br />";} $i++; }; Can anyone spot a mistake or suggest a better way of doing this? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/208767-variables-inside-the-_post-function-parameters-can-it-be-done/ Share on other sites More sharing options...
kenrbnsn Posted July 24, 2010 Share Posted July 24, 2010 You're loop code is completely wrong. Try something like this: <?php for ($i=1; $i<20; ++$i) { if ($_POST['song_' . $i]){ echo "Song: " . $_POST['song_' $i] . "<br />"; } if ($_POST['name_' . $i){ echo "Song Name: " . $_POST['name_' . $i] . "<br />"; } if ($_POST['artist_' . $i){ echo "Song Artist: " . $_POST['artist_' . $i . "<br />"; } if ($_POST['order_' . $i){ echo "Song Order: " . $_POST['order_' . $i . "<br />"; } } ?> You could also do something like this: <?php $pv = array('song'=>'Song', 'name'=>'Song Name', 'artist'=>'Song Artist', 'order'=>'Song Order'); for ($i=1; $i<20; ++$i) { foreach($pv as $p => $t) { if ($_POST[$p .'_' . $i]){ echo "$t: " . $_POST[$p . '_' $i] . "<br />"; } } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/208767-variables-inside-the-_post-function-parameters-can-it-be-done/#findComment-1090632 Share on other sites More sharing options...
wildteen88 Posted July 24, 2010 Share Posted July 24, 2010 Name your fields as an array, eg File: <input type="file" name="song[1][file]" /><br /> Name: <input type="text" name="song[1][name]" /><br /> Artist: <input type="text" name="song[1][artist]" /><br /> Order: <input type="text" name="song[1][order]" /> You'll want to increment the number in the square bracket (song[1]) for each upload. Now when you process your form you'll do something like this if(isset($_POST['submit'])) { foreach($_POST['song'] as $key => $song) { echo "Song File upload: " . $_FILES['song']['name'][$key]['file'] . "<br />"; echo "<pre>" . print_r($_FILES['song'], true) . "</pre>"; echo "Song Name: " . $song['name'] . "<br />"; echo "Song Artist: " . $song['artist'] . "<br />"; echo "Song Order: " . $song['order'] . "<br />"; echo '<hr />'; } } Link to comment https://forums.phpfreaks.com/topic/208767-variables-inside-the-_post-function-parameters-can-it-be-done/#findComment-1090635 Share on other sites More sharing options...
Tristan.Seabrook Posted July 24, 2010 Author Share Posted July 24, 2010 Thanks so much, works beautifully! Link to comment https://forums.phpfreaks.com/topic/208767-variables-inside-the-_post-function-parameters-can-it-be-done/#findComment-1090636 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.