[英]Submit button with dynamic name with php variable
I was trying to name my submit buttons by fetched data from the database using a while loop.我试图通过使用 while 循环从数据库中获取数据来命名我的提交按钮。 but I don't know how to name it and put it in an isset($_POST[] method. here is my code.但我不知道如何命名并将其放入 isset($_POST[] 方法中。这是我的代码。
<form method="POST" action="" enctype="multipart/form-data">
<?php
$stats = $conn->query("SELECT * FROM recruitment_status order by id asc");
while ($row = $stats->fetch_assoc()) :
$status_label=$row['status_label'];
?>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn-block btn-sm btn filter_status" type="submit" name="<?php echo $status_label ?>"><?php echo $status_label ?></button>
</div>
</div>
<?php endwhile; ?>
</form>
if(ISSET($_POST[$status_label]))
{
echo $row['status_label'];
}
<form method="POST" action="" enctype="multipart/form-data">
<?php
$stats = $conn->query("SELECT * FROM recruitment_status order by id asc");
while ($row = $stats->fetch_assoc()) :
$status_label=$row['status_label'];
?>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn-block btn-sm btn filter_status" type="submit" name="button_<?php echo $status_label ?>"><?php echo $status_label ?></button>
</div>
</div>
<?php endwhile; ?>
</form>
<>php
foreach($stats as $row){
if(ISSET($_POST['button_'.$row['status_label']]))
{
echo $row['status_label'];
}
}
?>
Your code just after you should be inside of php it seems rather entangled in html您的代码应该在 php 之后,它似乎与 html 纠缠在一起
<form method="POST" action="" enctype="multipart/form-data">
<?php
$stats = $conn->query("SELECT * FROM recruitment_status order by id asc");
while ($row = $stats->fetch_assoc()) :
$status_label=$row['status_label'];
?>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn-block btn-sm btn filter_status" type="submit" name="<?php echo $status_label ?>"><?php echo $status_label ?></button>
</div>
</div>
<?php endwhile; ?>
</form>
<?php
if(ISSET($_POST[$status_label]))
{
echo $row['status_label'];
}
?>
Its easier to read and understand (the own code (later)) if you split PHP and HTML, if you can.如果可以的话,如果您拆分 PHP 和 HTML,它更易于阅读和理解(自己的代码(稍后))。
First we get the data into an array,首先我们将数据放入一个数组中,
and then we just need to loop through it in the HTML part.然后我们只需要在 HTML 部分循环遍历它。
Note: please use htmlspecialchars
on all values you print to the browser to prevent XSS.注意:请在打印到浏览器的所有值上使用htmlspecialchars
以防止 XSS。
<?php
// ... On top of the script ...
// Get the values before (not in) the HTML (for readable reasons).
$stmt = $conn->query("SELECT * FROM `recruitment_status` ORDER BY `id` ASC;");
$recruitmentStatuses = $stmt->fetch_all(MYSQLI_ASSOC);
?>
<form method="POST" action="" enctype="multipart/form-data">
<?php foreach ($recruitmentStatuses as $status) : ?>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn-block btn-sm btn filter_status"
type="submit"
name="<?php echo htmlspecialchars($status['status_label']) ?>"><?php echo htmlspecialchars($status['status_label']) ?></button>
</div>
</div>
<?php endforeach; ?>
</form>
<?php
foreach ($recruitmentStatuses as $status) {
if (isset($_POST[$status['status_label']])) {
echo $status['status_label'];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.