Jump to content

[SOLVED] Retreive Data from form, without knowing how many fields were passed.


iPixel

Recommended Posts

Here's my code !

Basically it creates a table that looks something like this :

 

Image Name        Alt Text

---------------------------------------------------------

123586_01.jpg  |  Alt text goes here whatever it may be.  |

---------------------------------------------------------

123586_02.jpg  |  Alt text goes here whatever it may be.  |

---------------------------------------------------------

 

This gets generated by default by reading files from a folder and inserting them into a

database where image name is image name and alt_txt is defaulted to alt_text_imgname.

 

The Code :

<form action="savealts.php" method="get">
<table cellpadding="0" cellspacing="0" class="loginForm" width="95%" style="margin-top: 10px;">
<tr>
<td bgcolor="0066CB" align="center"><font class="blueheaders">Image Name</font></td>
<td bgcolor="0066CB" align="left"><font class="blueheaders">  Alt Tag</font></td>
</tr>
<?
$i=0;
do  
{
	$i++;
	?>
	<tr bgcolor="#CFF7C1">
	<td class="loginForm2">
	<font class="loginFormTxt">					
	  <? echo $result['g_image']; ?>
	</font>
	</td>
	<td class="loginForm2"> 
	<input type="text" value="<? echo $result['alt_txt']; ?>" id="<? echo $result['alt_txt']; ?>" name="<? echo $result['alt_txt']; ?>" style="font-size: xx-small;" maxlength="225" size="63">
	<input type="hidden" value="<? echo $result['g_image']; ?>" id="<? echo $result['g_image']; ?>" name="<? echo $result['g_image']; ?>">
	</td>
	</tr>
	<?
}
while($result = mysql_fetch_assoc($sql));
?>
<tr bgcolor="#CFF7C1">
<td colspan="2" style="border-right: 1px solid #999999"> </td>
</tr>
<tr bgcolor="#CFF7C1">
<td colspan="2" align="center" class="loginForm2">
<input type="submit" value="Save Alt Tags" style="font-size: xx-small;">
<input type="hidden" value="<? echo $mdl; ?>" id="model" name="model">
<input type="hidden" value="<? echo $count; ?>" id="count" name="count">
</td>
</tr>
</table>
</form>

 

So my problem is as noobish as it may be, im not sure how to go about receiving the submitted form and all of its data, the amount can range from 1 to 100+ text fields,

i assume i somehow have to create an array but i really need a starting point at least.

 

Thanks in advance for your help.

 

~iPixel

  • 3 weeks later...

The way it is now it doesnt quite work, nothing gets printed.

If you look 2 fields are being generated by the do while loop.

Should the ID's of these 2 fields be different or the same?

 

You could loop through all the POST or GET fields with something like the following:

 

foreach( $_POST as $current){
           echo $current.'<br />';
}

 

Andy

an easier method is to give the input fields array names, which tosses an array into the $_POST data and makes things easier to handle:

 

<?php
$i=0;
do  
{
	$id = $result['g_image'];
	$i++;
	?>
	<tr bgcolor="#CFF7C1">
	<td class="loginForm2">
	<font class="loginFormTxt">					
	  <?php echo $id; ?>
	</font>
	</td>
	<td class="loginForm2"> 
	<input type="text" value="<?php echo $result['alt_txt']; ?>" id="alt_txt[<?php echo $id; ?>]" name="alt_txt[<?php echo $id; ?>]" style="font-size: xx-small;" maxlength="225" size="63">
	</td>
	</tr>
	<?php
}
while($result = mysql_fetch_assoc($sql));
?>

 

this saves you the trouble of having hidden inputs, as the image that the alt_txt is for is unambiguous - its the index of the array.  the resulting array would look like this:

 

$_POST['alt_txt'][image_the_text_goes_with] = 'alt text for this image';

 

so you can run a foreach() on $_POST['alt_txt'] and each key will be an image name, its value will be the alt text that goes with the image.

Im a little confused on the retrieval of the info ? the foreach !

 

foreach($_POST['alt_txt'] as $current)
{
           echo $current.'<br />';
}

 

Yes yea i am a newbie :( here's the whole form that posts so savealts.php

 

<form action="savealts.php" method="get">
<table cellpadding="0" cellspacing="0" class="loginForm" width="95%" style="margin-top: 10px;">
     <tr>
          <td bgcolor="0066CB" align="center"><font class="blueheaders">Image Name</font></td>
          <td bgcolor="0066CB" align="left"><font class="blueheaders">  Alt Tag</font></td>
     </tr>
<?
$i=0;
     do  
     {
          $id = $result['g_image'];
          $i++;
          ?>
          <tr bgcolor="#CFF7C1">
               <td class="loginForm2">
               <font class="loginFormTxt">  <?php echo $id; ?></font>		
               </td>
               <td class="loginForm2">  
               <input type="text" value="<?php echo $result['alt_txt']; ?>" id="alt_txt[<?php echo $id; ?>]" name="alt_txt[<?php echo $id; ?>]" style="font-size: xx-small;" maxlength="225" size="63">
               </td>
          </tr>
         <?php
     }
     while($result = mysql_fetch_assoc($sql));
?>
     <tr bgcolor="#CFF7C1">
          <td colspan="2" style="border-right: 1px solid #999999"> </td>
     </tr>
     <tr bgcolor="#CFF7C1">
          <td colspan="2" align="center" class="loginForm2">
          <input type="submit" value="Save Alt Tags" style="font-size: xx-small;">
          <input type="hidden" value="<? echo $mdl; ?>" id="model" name="model">
          <input type="hidden" value="<? echo $count; ?>" id="count" name="count">
          </td>
     </tr>
</table>
</form>

 

$id should be different whereas i can have well over 100 imgs with alt texts assigned to them.

 

Thanks

with that code, $id will change on each input echoed.  here's a sample foreach:

 

foreach ($_POST['alt_txt'] AS $image_name => $alt_text)
{
  echo 'The image '.$image_name.' was given the alternate text: '.$alt_text.'<br />';
}

 

i may be missing exactly what you're after.

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.