php使用隐藏的帖子将动态数据传递给另一个php文件

I'm trying to build a comment system, each comment has a unique id, many comments can be associated with a post, and each post has a unique id. I want to pass the post id to a submit.php file (where comments are update to the database), but no matter what I tried I just can't pass the data. Currently I have something like this:

$sql="SELECT postid,post,pdate FROM posts";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
echo '$row["post"]';
echo '<form action="submit.php" method="POST">';
echo '<input name="comment" type="text" id="comments"></input>';
echo '<input type="hidden" name="id" value="$row["postid"]" />';
echo '<input type="submit" value="enter comments" />';

}

?>

for testing purpose I have submit.php as follows,

    <?php
    $ha=$_POST['id'];
    echo $ha;
    ?>

data of postid is not passed, and I just got "$row[" as output.

inside the while loop if I say $haha=$row["postid"]; echo "$haha"; then each individual post id will be printed correctly, but I just cannot pass the data to submit.php file.

update: I just changed my code to :

echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';

Now a number is succesfully passed to submit.php, the problem is ,it's always "3". My post id ranges from 3 to 13, post with id=3 is at the bottom of the page and post with id=13 is at the top.However,if I write a comment at the post with id=13(same issue occur to other posts as well), after clicking submit, the data passed to submit.php is always 3. Is there something wrong with the while loop?

Another update: it's always 3 because i forget to close the form tag, now everything worked perfectly

you're using single quote, so you cannot insert variables inside of string, use

echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';

Try adding a conditional at the start of your file like this just to be sure the form is actually submitted properly:

<?php
if(isset($_POST['submit_form'])) {
    $ha=$_POST['id'];
    echo $ha;
}

with your button like this

<input type="submit" value="enter comments" name="submit_form"/>

and please close your form tag.

In PHP, you can wrap a string in single-quotes ('), or double quotes (").

When you use single quotes, the string is not interpreted - this means that all the characters are left intact, and no variables are parsed.

When you use double quotes, any variables in the string will be replaced with their value.

In your case, you're using single quotes, so your variable is not being interpreted and converted. Instead, use double quotes:

$sql="SELECT postid,post,pdate FROM posts";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    echo $row["post"];
    echo '<form action="submit.php" method="POST">';
    echo '<input name="comment" type="text" id="comments"></input>';
    echo "<input type=\"hidden\" name=\"id\" value=\"{$row["postid"]}\" />";
    //Alternatively, keep the single quotes and use the concatenation method:
    //echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';
    echo '<input type="submit" value="enter comments" />';

    //Also, if you're opening a form tag in this loop, be sure to close it
    echo '</form>';
}

Some other things to note:

  • When you use double quotes to wrap your string, and you have double quotes inside your string, you must escape them (using a \). Notice name="id" became name=\"id\"; and
  • When referencing an item in an array within a string, you can either use string concatenation to ensure the full variable is interpreted correctly (value=\"" . $row["postid"] . "\"), or you can leave the variable in place and wrap it in curly brackets - which is my preference and is what is used above. If you're going to use the concatenation method, then you can keep the single quotes wrapping everything else - there are no variables to parse.
  • When echoing a variable value, you don't need to wrap it in anything - notice I removed the quotes from the first echo.

Here is PHP's documentation on strings, including single and double quoted strings: http://php.net/manual/en/language.types.string.php.

And here is PHP's documentation on string operators: http://php.net/manual/en/language.operators.string.php.

You can pass value as follow,

echo '<input type="hidden" name="id" value="' . $row["postid"] . '" >';