dryk50495 2012-06-26 04:17
浏览 135
已采纳

如何在php中传递checked复选框的值

Hi i am generating checkbox list by using a for each loop, I want to pass the value of checked checkbox into database.

foreach($this->lis as $lst)
 {?>
  <tr>
    <td>
     <input type="checkbox" name="list" value="1" />
         <label for="list_32"><?php echo $list->nList ?></label>
    </td>
  </tr>
 <?php } ?>

when i checked the checkbox i want to pass the label name into the database can someone help.

  • 写回答

4条回答 默认 最新

  • drzrzzkh462254 2012-06-26 04:37
    关注

    Ok, you've got a few problems there..

    Firstly, please avoid opening php tags and closing them constantly. Just echo back the HTML. Going in and out of php so often will cause performance issues.

    So, first off, you need to set up a form on here, your form needs to point to a second page (or self-process using an isset function, but let's go with the former solution for clarity). The second page is what's going to dump things into the DB for you based on what the user ticked.

    So, here's the HTML for your form;

    <form action="database.php" method="POST">
        <div>
            <label for="checkbox1">This is the first option:</label>
            <input type="checkbox" id="checkbox1" name="checkbox1" />
        </div>
        <div>
            <label for="checkbox2">This is the second option:</label>
            <input type="checkbox" id="checkbox2" name="checkbox2" />
        </div>
        <div>
            <input type="submit" />
        </div>
    </form>
    

    Again, I'd recommend echo'ing this back in your foreach loop - a short function could accomplish this for you.

    So, once someone hits the "Submit" button, they'll be taken to your second page (database.php as we've called it), and their options will be stored in the POST array. For good practice, always use POST rather than GET when dealing with DB entries.

    So on this page, we'd check what checkboxes were selected, and then update the db as required;

    if (isset($_POST['checkbox1']))
    {
        // user ticked checkbox1
        $sql = "UPDATE table
                SET 'checkbox1' = 1
                WHERE 'user' = 'username';"
        mysqli_query($sql); 
    }
    

    Note that's just a rough solution - you haven't told me exactly how you're doing this, so I'm assuming your DB is tied to user's votes etc. You can also update checkbox1 by incrementing the value.. etc etc.

    Hope this helps.

    • Eoghan
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址