iPixel Posted July 6, 2007 Share Posted July 6, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/ Share on other sites More sharing options...
AbydosGater Posted July 6, 2007 Share Posted July 6, 2007 You could loop through all the POST or GET fields with something like the following: foreach( $_POST as $current){ echo $current.'<br />'; } Andy Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-291342 Share on other sites More sharing options...
iPixel Posted July 25, 2007 Author Share Posted July 25, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-307109 Share on other sites More sharing options...
akitchin Posted July 25, 2007 Share Posted July 25, 2007 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. Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-307159 Share on other sites More sharing options...
iPixel Posted July 25, 2007 Author Share Posted July 25, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-307234 Share on other sites More sharing options...
akitchin Posted July 25, 2007 Share Posted July 25, 2007 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. Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-307241 Share on other sites More sharing options...
iPixel Posted July 25, 2007 Author Share Posted July 25, 2007 Ah nice ! ty ty ty ty ty sooooo much ! My only problem was left was that the form's method was get and the foreach was POST Once again TY ! You Rawk ! ~iPixel Link to comment https://forums.phpfreaks.com/topic/58713-solved-retreive-data-from-form-without-knowing-how-many-fields-were-passed/#findComment-307245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.