Envío de varios valores con un formulario a través de un bucle

What im trying to accomplish is sending an id and the review content to a page where it can be updated. The content is displayed correctly. However only the last value is always posted, so for some reason the variable is overwritten, as I can see when i test the output on the next page (updatereview.php). How can I send the Id for each review content? my code so far below:

<?php
while($row2 = mysql_fetch_array($result2))
{
    echo"<tr>";   
    ?><td> <? echo "Comment left for ".$row2['game_name'].", ".$row2['game_platf'] ?> <br />
    <form method="post" action="updatereview.php?id=<? echo $row2['review_id']  ?>"> 
    <textarea  name= "content" class= "rev" ><? echo $row2['content'] ?></textarea></td> 
    <td><input name="Submit1" type="submit" value="Update" />||</td>  
    <? echo"</tr>";
}
echo "</table>";

preguntado el 01 de febrero de 12 a las 04:02

Do you want to update one review or all reviews per submit? -

this code outputs all the reviews with an update/delete option for each. So each submit should carry the review id -

3 Respuestas

Well this is what I have come up with, putting the code just incase someone knows of a more efficient way. Ive used Rocky's method but stored the values in an array in the name field, solving the problem of overwriting. I still however think that using get will work instead of making a hidden field, but this worked well, so i'll stick with it. : (sorry for messy indentation, trying my best ;) )

$n = 1; 
echo "<table>";
while($row2 = mysql_fetch_array($result2))
{
    echo"<tr>";
?>
<td>
<? 
    echo "Comment left for ".$row2['game_name'].", ".$row2['game_platf'] 
?>
  <br />
  <form method="post" action="updatereview.php"> 
    <textarea  name= "content[<?php echo $n ?>]" class= "rev" >
    <? echo $row2['content'] ?> 
    </textarea> 

    <input type="hidden" name="review_id[<?php echo $n ?>]" value=<?php echo $row2['review_id'] ?> /></td>

    <td><input name="Submit1" type="submit" value="Update" />||</td> 

  </form>
  <td><a href = "deletereview.php?id=<? echo $row2['review_id'] ?>" >Delete</a></td>  
 <? echo"</tr>"; 
 $n++;    
 }
echo "</table>";

and then in updatereview.php:

foreach($_POST['content'] as $content) { 
    echo "$content"; // to test values
    foreach($_POST['review_id'] as $review_id) { 
        echo "$review_id";
    }

Respondido 01 Feb 12, 20:02

You will need to structure the name attribute values in such a way that they will be considered arrays

<input type="hidden" name="review_id[][<?php echo $n ?>]" value=<?php echo $row2['review_id'] ?> />

What I did on one of my project is to use a uniform name with a certain pattern so that the elements are grouped accordingly

name="review[1][review_id]"
name="review[1][content]"

then for the next record you may be using a different index

name="review[2][review_id]"
name="review[2][content]"

I usually use the id of the row from the database as the index.

Respondido 01 Feb 12, 20:02

the way you were calling your form action basically made your form method 'get'. Put the id variable into a hidden element so that it can post, if you put it in the url of the action you'll only get one id.

This might be better in a foreach

<?php
//use alternate syntax in html pages it's much easier to keep track off
foreach(mysql_fetch_array($result2) as $row2): ?>
<tr>  
    <td> 
    <?php echo "Comment left for ".$row2['game_name'].", ".$row2['game_platf'] ?></td><br />

    <form method="post" action="updatereview.php"> 

    //add hidden input element and give it the value of the id so it will be part of the $_POST array
    <td> <input type="hidden" name="id" value="<?php echo $row2['review_id'] ?>" /></td>

    <td><textarea  name= "content" class="rev" ><?php echo $row2['content'] ?></textarea></td>

    <td><input name="Submit1" type="submit" value="Update" />||</td> 
    </form>
</tr>

<?php endforeach ?>
</table>

now when you post the form the id will be available in the $_POST array.

also the way this is structured you'll get a submit button for each record, move the submit outside the loop if you only want one.

Respondido 02 Feb 12, 10:02

Thanks Rocky. Its intended to send the id through the url for each review and the content as POST. I did change it as you said to send the id as hidden value in form as POST but still only the last review comes up - chucky

first things first, check your data array make sure it has the data you expect. Second check the loop and make sure it's doing what you want. Keep it simple and add back items until you break it. - RockyFord

if you want update and delete buttons, that's easy. Remove all the form stuff and add a table cell for each with a link to the update/delete actions. you can style as buttons if you like. Also with the links you will need to pass the id in the url. This looks like a simple question but it has all kinds of possibilities. - RockyFord

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.