Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

Problem passing the correct value in ID to another page

I am using a form to edit comments and using the hidden input fields to pass the value,

the form is something like this.

<input type="text" name="name" value="<?php echo $name; ?>" class="input" />
<input type="hidden" name="com_name" value="<?php echo $name;?>"/>
<input type="text" name="email" value="<?php echo $email; ?>" class="input" />
<input type="hidden" name="com_email" value="<?php echo $email;?>"/>
<input type="text" name="phone" value="<?php echo $phone; ?>" class="input" />
<input type="hidden" name="com_phone" value="<?php echo $phone;?>"/>
<input type="text" name="location" value="<?php echo $location; ?>" class="input" />
<input type="hidden" name="com_location" value="<?php echo $location;?>"/>
<input type="hidden" name="com_id" value="<?php echo $id; ?>"/>
<textarea name="comment" cols="" rows="" class="comment-msg"><?php echo $comment; ?>
</textarea>
<input type="hidden" name="com_comment" value="<?php echo $comment;?>"/>
<input name="com_update_1" type="submit" class="btn-70" value="" />      

and i am using the while loop to iterate the value in the form fields. my while loop code is

$query = "SELECT comments.*,
                news.title 
                FROM comments JOIN news ON comments.news_id = news.id
                ORDER BY id DESC LIMIT $from, " . COMM_POST_NUMBER;
      $result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
      $id = $row['id'];
      $date = date("d-F-Y", $row['timestamp']);
      $name = stripslashes($row['name']);
      $email = stripslashes($row['email']);
      $phone = stripslashes($row['phone']);
      $location = stripslashes($row['location']);
      $comment = stripslashes($row['comment']);
      $news_id = $row['news_id'];
      $title = stripslashes($row['title']);
      $approve = $row['approve'];

The Iteration works well and it prints the value from the database perfectly and the hidden input field with value ID holds the correct value in this page i.e(comments.php). but when i want to pass the value to different page <form action="action.php" method="post"> my trouble starts it takes only the first id when i hit submit. i.e id with value 1 and it refuses to take any other values from id.

Here is the code from the view source code of the browser

<div class="comments-toggle">
 <input type="text" name="name" value="My Name is 16" class="input" />
 <input type="hidden" name="com_name" value="My Name is 16"/>
 <input type="text" name="email" value="[email protected]" class="input" />
 <input type="hidden" name="com_email" value="[email protected]"/>
 <input type="text" name="phone" value="919999999999" class="input" />
 <input type="hidden" name="com_phone" value="919999999999"/>
 <input type="text" name="location" value="Somewhere" class="input" />
 <input type="hidden" name="com_location" value="Somewhere"/>
 <input type="hidden" name="com_id" value="16"/>
 <textarea name="comment" cols="" rows=""class="comment-msg">                              
</textarea>
 <input type="hidden" name="com_comment" value=""/>
  <input name="com_update" type="submit" class="btn-70" value="" />                        
   </div>

here is the code from action.php

 if( isset($_POST['com_update'])) {
        echo $id = $_POST['com_id'];
    if( isset($_POST['name'])) {
        echo $name = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['name']))); 
        $query = "UPDATE comments SET name = '$name' WHERE id = '$id'";
        $result = mysql_query($query) or die('Error');
        }

how do i make it pass the correct id values to action.php?

Upvotes: 0

Views: 1032

Answers (1)

Nev Stokes
Nev Stokes

Reputation: 9779

The way to achieve this is to define your form fields as being ready for conversion to PHP arrays:

<input type="hidden" name="com_id[]" value="16"/>

Now, $_POST['com_id'] should be an array of values.

Upvotes: 1

Related Questions