使用post方法将变量的值传递到下一页


Passing value of variable to next page using post method

在下面的代码中,我想使用post方法将变量$user_id传递到下一页,假设我的下一页是followers.php。整个代码是链接本身,我想把post方法在它,以便在点击链接,变量也传递给follower.php。不要使用会话

<a href='follower.php' style='text-decoration:none;'>
<h2>Followers</h2>
<?php
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?></a>

我猜post方法的代码是正确的:

 <form method="get" action="follower.php">
 <input type="hidden" name="varname" value="user_id">
 <input type="submit">
 </form>   

那么如何把这个post方法放在其他代码中,这样点击,follower.php打开,变量也传递到这个页面。

您正在使用GET方法。改为

<form method="POST" action="follower.php">
 <input type="hidden" name="user_id" value="user_id">
 <input type="submit">
 </form>

同时将隐藏字段的名称改为user_id。现在接收为

<?php
$user_id = $_POST['user_id'];
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?>

注意:上面的查询容易受到SQLI的攻击。最好使用Prepared语句

您正在谈论post方法,但在您的代码中您正在使用GET方法。您可能需要考虑将其更改为POST。这样,表单中填写的内容就不会输入URL。(更安全一点)。之后,您可以调用全局变量$_POST['user_id']

链接不能POST数据,只能GET数据。

使用:

<a href='follower.php?user_id=1' style='text-decoration:none;'>

和following .php文件中的

$user_id = $_GET['user_id'];