我有一个表单,我输入用户名并获取用户信息。在呼叫用户信息后,我有另一个表格,其中包含"banned"
的复选框,我可以禁止或取消用户。但是,如何从第一个表单中存储输入的用户名,以后在我检查禁用复选框的第二个表单中使用它?
代码:
<form action="" method="post" id="msform">
<input type="text" name="datausername" placeholder="Username" maxlength="64" readonly onfocus="this.removeAttribute('readonly');"/>
<input type="submit" name="userdata" value="Get Data" class="databutton">
</form>
<?php
if(isset($_POST['userdata'])){
$datausername = $_POST['datausername'];
$sql = "SELECT * FROM users WHERE username = '$datausername'";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '
<div class="container" style="width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Personal Information</h1>
ID : <div class="fr">'.$row['id'].'</div><br />
E-mail: <div class="fr">'.$row['email'].'</div><br />
Username: <div class="fr">'.$row['username'].'</div><br />
Firstname: <div class="fr">'.$row['firstname'].'</div><br />
Lastname: <div class="fr">'.$row['lastname'].'</div><br />
<br />
Activated: <div class="fr">'.$row['active'].'</div><br />
Banned: <div class="fr">'.$row['banned'].'</div><br />
<br />
<h1>Data Information</h1>
Title: <div class="fr">'.$row['title'].'</div><br />
Rank: <div class="fr">'.$row['rank'].'</div><br />
Level: <div class="fr">'.$row['level'].'</div><br />
Exp: <div class="fr">'.$row['exp'].'</div><br />
<br />
<h1>Forum / Profile stats</h1>
Forum Posts: <div class="fr">0</div><br />
Forum Threaths: <div class="fr">0</div><br />
Comments: <div class="fr">0</div><br />
Awards: <div class="fr">0</div><br />
Friends: <div class="fr">0</div><br />
Followers: <div class="fr">0</div><br />
<br />
<h1>Security Information</h1>
IP: <div class="fr">'.$row['ip'].'</div><br />
Last IP: <div class="fr">'.$row['active_ip'].'</div><br />
Secret Question: <div class="fr">'.$row['question'].'</div><br />
Timestamp: <div class="fr">'.$row['timestamp'].'</div><br />
Activate Link: <div class="fr"><a href="/activate/'.$row['activate_link'].'">link</a></div><br />
<br />
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>';
if($title == 'Head Admin' || $title == 'Admin'){
if($row['banned'] == '1'){
$checkbox = '<label><input type="checkbox" name="confirm" checked/></label>';
} else {
$checkbox = '<label><input type="checkbox" name="confirm" /></label>';
}
echo '
<div class="container" style="margin-top:20px; width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Settings</h1>
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>
';
}
}
} else {
echo '
<div class="container" style="margin:0 auto; width:100%;">
<div class="flag note note--secondary">
<div class="flag__image note__icon">
<i class="fa fa-user"></i>
</div>
<div class="flag__body note__text">
User not found!
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>';
}
}
$db->close();
?>
这是我希望存储用户名以重新使用i更新“禁止”复选标记的第二种形式:
if($title == 'Head Admin' || $title == 'Admin'){
if($row['banned'] == '1'){
$checkbox = '<label><input type="checkbox" name="confirm" checked/></label>';
} else {
$checkbox = '<label><input type="checkbox" name="confirm" /></label>';
}
echo '
<div class="container" style="margin-top:20px; width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Settings</h1>
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>
PS:
中的标题if($title == 'Head Admin' || $title == 'Admin'){
您是否已登录标题,因此,如果您是“管理员”或“管理员”
,这只是一个选项答案 0 :(得分:0)
在表单中添加隐藏字段以包含用户的id
。然后,您可以将其用作第二种形式的更新查询的密钥
<form method="post" action="" id="msform" style="text-align:left;">
<input type="hidden" name="key" value="<?php echo $row['id']; ?>">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
Alternativley使用会话
在表单一中,在脚本顶部添加thsi
<?php
session_start();
然后在会话中加载一个值
$_SESSION['Users_id'] = $row['id'];
现在在第二种形式中,您将在UPDATE中使用该值
<?php
session_start();
if ( isset($_SESSION['users_id']) ) {
$key = $_SESSION['users_id'];
} else {
// we really should not be in this form
header('Location: somewhere_else.php');
}
答案 1 :(得分:0)
您可以创建隐藏文本字段来存储它,然后通过POST数据将其传递到下一个阶段:
(而不是在这里使用datausername,你应该使用从SQL查询返回的行ID)
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
<input type="hidden" name="username" value="<?=$datausername?>">
</form>
然后在下一阶段,$ _POST [&#39; datausername&#39;]将会存在。
OR
使用会话(更好的解决方案是诚实的,不易受黑客攻击) 例如,您可以启动一个新会话,将一些数据推入其中,并在完成后取消设置会话。
在你的index.php / config.php /中
session_start();
在你的php文件中:
$datausername = $_POST['datausername'];
$_SESSION['customdata']['username'] = $datausername;
if($title == 'Head Admin' || $title == 'Admin') {
if ( isset($_SESSION['customdata']) ) {
$username = $_SESSION['customdata']['userid'];
//ban the user here, update SQL etc.
//Unset the session variable
unset($_SESSION['customdata'];
}
}