douwan2664 2016-10-25 13:44
浏览 41
已采纳

存储要在以后的表单中使用的用户名

I have a form where I enter a username and it gets the users information. after calling for the users information I have another form wtih a checkbox for "banned" where I can ban or unban the user. But how can I store the entered username from the first form, to later use it in the second form where I check the banned checkbox?

code:

<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();
?>

This is the second form where I want to have the username stored to re use i to update the "banned" checkmark:

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: title in

if($title == 'Head Admin' || $title == 'Admin'){

Is your logged in title, so this will only be an option if you are either Head Admin or Admin

  • 写回答

2条回答 默认 最新

  • dongshenghe1833 2016-10-25 13:52
    关注

    You can create a hidden text field to store it, then pass it via the POST data to the next stage:

    (instead of using datausername here, you should be using the row ID returned from your SQL query)

    <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>
    

    Then in the next stage $_POST['datausername'] will exist.

    OR

    Use sessions (the better solution to be honest, less prone to hacking) For example, you can start a new session, shove some data into it and unset the session after your finished with it.

    In your index.php / config.php / whatever

    session_start();
    

    In your php file:

    $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'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真