Forum Moderators: coopster

Message Too Old, No Replies

posting variables

         

derek mcgilvray

9:03 pm on Aug 9, 2009 (gmt 0)

10+ Year Member



Hi folks,

I've tried everything but I think my logic's a little wrong - can I just run this by you please, and I would appreciate any comments.

I have userid and comments that I want to post. Now these 2 variables come from a form which is laid out in a table of two columns (userid and comments) on my editing page.
The table is lots of <textarea>s which display userid and comments already in the database. I want to edit only some of the <textarea>s each time I visit the editing page, and then they should be updated.

//check for posted data
if (isset($_POST['submit'])) {

$userid=$_POST['userid'];
$comments=$_POST['comments'];
foreach($userid as $userid){
foreach($comments as $comments){
echo"$comments $userid<br />";
}
}

Now this is obviously wrong because although the comments process correctly, the userid is the same for each line.

Any poiners? Any help gratefully received.

whoisgregg

10:09 pm on Aug 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you show us how you define the "name" attribute on the <textarea> elements? :)

derek mcgilvray

6:41 am on Aug 10, 2009 (gmt 0)

10+ Year Member



Here is what I use to display and collect the data before passing through the aforementioned script.

<form id="thisfile.php" method="post"
action="<?php $_SERVER['PHP_SELF']; ?>">
<table>

<?php
while($i<$num){
$userid=mysql_result($result,$i,"userid");
$comments=mysql_result($result,$i,"comments");

//display this data
echo"<tr><td>
<input type=\"hidden\" name=\"userid\" value=\"$userid\">
<textarea rows=\"5\" cols=\"100\" name=\"comments[]\">$comments</textarea>
</td></tr>";
$i++;
}

?>
</table>

Any ideas?

daveVk

7:45 am on Aug 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Having the the same variable name on both sides of the "as" is questionable

Try

foreach($userid as $u){
foreach($comments as $c){
echo "$c $u<br />";
}
}

derek mcgilvray

7:58 am on Aug 10, 2009 (gmt 0)

10+ Year Member



Thanks for your comments, but that's just repeating the comments for every userid.

I want to get the right comments from the form for each userid, and it's driving me crazy.

Where should I look from here?

daveVk

10:55 am on Aug 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure maybe

foreach($userid as $i=>$u){
echo "$comments[$i] $u<br />";
}

OR

$i=0;
foreach($userid as $u){
echo "$comments[$i++] $u<br />";
}

omoutop

11:58 am on Aug 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try to define a unique name to each textarea

while($i<$num)
{
<textarea rows=\"5\" cols=\"100\" name=\"comments".$i."\">$comments</textarea>
}

This will create you $i textareas

Where you proccess your form data you can do a simple loop through all the names to see which one was submitted and maniluate that field only

As an alternate mehtod
you can assign each textarea field to a seperate form
There pass some unique var as a hidden field so as you know which textarea is being submitted

derek mcgilvray

3:29 pm on Aug 10, 2009 (gmt 0)

10+ Year Member



Thanks everyone for your comments. omoutop, I used your idea and it worked great thanks:

while($i<$num)
{
<textarea rows=\"5\" cols=\"100\" name=\"comments".$i."\">$comments</textarea>
}

omoutop

5:55 am on Aug 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



glad you sort this out :)