phorcon3 Posted January 2, 2008 Share Posted January 2, 2008 for example I have several textareas on the same page, and I wanna collect all the data entered in insert into my MySQL database, but the problem is I dun know the name's for each of the textareas just lemme give u an example: <?php $a = mysql_query("SELECT * FROM `table` WHERE `this` = 'that'"); while$b = mysql_fetch_array($a)) { echo '<textarea name="body_'.$b['id'].'" cols="50" rows="3"></textarea>'; } ?> how could I collect all the body_ID#? I figured it out how to do it if there only exists just one textarea: <?php if(isset($_POST)){ foreach($_POST as $value => $item) { $id = explode('_', $value); mysql_query("UPDATE `table` SET `this` = '".$item."' WHERE `id` = '".$id[1]."'"); } } echo '<form method="post" action="/test.php">'; $a = mysql_query("SELECT * FROM `table` WHERE `this` = 'that'"); while($b = mysql_fetch_array($a)) { echo '<div><textarea name="body_'.$b['id'].'" cols="50" rows="3"></textarea></div>'; } echo '<div><input type="submit" value="Submit" /></div></form>'; ?> I hope this makes sense. lol just dun really know how else to explain it Anyway, I'd really appreciate any ideas/help/comments on this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/ Share on other sites More sharing options...
p2grace Posted January 2, 2008 Share Posted January 2, 2008 For each textarea you could also create a hidden field that has the name of the textarea. Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427740 Share on other sites More sharing options...
priti Posted January 2, 2008 Share Posted January 2, 2008 for example I have several textareas on the same page, and I wanna collect all the data entered in insert into my MySQL database, but the problem is I dun know the name's for each of the textareas just lemme give u an example: <?php $a = mysql_query("SELECT * FROM `table` WHERE `this` = 'that'"); while$b = mysql_fetch_array($a)) { echo '<textarea name="body_'.$b['id'].'" cols="50" rows="3"></textarea>'; } ?> how could I collect all the body_ID#? I figured it out how to do it if there only exists just one textarea: <?php if(isset($_POST)){ foreach($_POST as $value => $item) { $id = explode('_', $value); mysql_query("UPDATE `table` SET `this` = '".$item."' WHERE `id` = '".$id[1]."'"); } } echo '<form method="post" action="/test.php">'; $a = mysql_query("SELECT * FROM `table` WHERE `this` = 'that'"); while($b = mysql_fetch_array($a)) { echo '<div><textarea name="body_'.$b['id'].'" cols="50" rows="3"></textarea></div>'; } echo '<div><input type="submit" value="Submit" /></div></form>'; ?> I hope this makes sense. lol just dun really know how else to explain it Anyway, I'd really appreciate any ideas/help/comments on this. Thanks. Well your coding is confused me kindly run this script and you will get the idea how you can get the name of your textarea <?php $a=array('1','2','3');?> <form action="<?php $_SERVER['PHP_SELF']?>" method='post'> <?php foreach($a as $k) { echo '<textarea name="body_'.$k['id'].'" cols="50" rows="3"></textarea>'; ///from your piece of code } echo '<input type="submit" name="submit" value="submit">'; echo '</form>'; if(isset($_POST)) { print_r($_POST); } ?> Hope it helps, Regards Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427947 Share on other sites More sharing options...
kishan Posted January 2, 2008 Share Posted January 2, 2008 keep a hidden variable that gives u the no of text areas in the form and make sure that ur textarea will be of form <textarea name='textarea_1' >....................<textarea name='textarea_2' > Now in the php use that count and use for($i=1;$i<=$cnt;$i++) ... $_POST['textarea_'.$i]; Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427954 Share on other sites More sharing options...
priti Posted January 2, 2008 Share Posted January 2, 2008 Can someone explain why to take hidden values to solve this problem??? Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427972 Share on other sites More sharing options...
Daniel0 Posted January 2, 2008 Share Posted January 2, 2008 You don't need any hidden fields. <?php $textarea_values = array(); foreach($_POST as $key => $value) { if(preg_match('/^body_[0-9]+$/', $key)) { $textarea_values[] = $value; } } ?> But a better option would be to rename the textareas. <?php $a = mysql_query("SELECT * FROM `table` WHERE `this` = 'that'"); while$b = mysql_fetch_array($a)) { echo '<textarea name="body['.$b['id'].']" cols="50" rows="3"></textarea>'; } ?> In that way you can access $_POST['body'] as an array on the next page. Quote Link to comment https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.