Jump to content

Variables inside the $_POST function parameters -- can it be done?


Recommended Posts

    I'm new at this  :shy: 

 

    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!  :D

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

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 />';
    }
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.