Jump to content

$_POST + textareas


phorcon3

Recommended Posts

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 :D Anyway, I'd really appreciate any ideas/help/comments on this. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/84037-_post-textareas/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427947
Share on other sites

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];

 

 

Link to comment
https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427954
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/84037-_post-textareas/#findComment-427992
Share on other sites

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.