Home > Software engineering >  Submit button with dynamic name with php variable
Submit button with dynamic name with php variable

Time:11-21

I was trying to name my submit buttons by fetched data from the database using a while loop. but I don't know how to name it and put it in an isset($_POST[] method. here is my code.

<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'];
}

CodePudding user response:

<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'];
  }
}
   ?>

CodePudding user response:

Your code just after you should be inside of php it seems rather entangled in 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'];
}
?>

CodePudding user response:

Its easier to read and understand (the own code (later)) if you split PHP and HTML, if you can.

First we get the data into an array,
and then we just need to loop through it in the HTML part.

Note: please use htmlspecialchars on all values you print to the browser to prevent 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'];
    }
}
  • Related