0

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'];
}
2
  • So it not shows name of button or what? Commented Nov 20, 2021 at 21:40
  • Please use htmlspecialchars() to output all values in HTML to prevent XSS. Commented Nov 20, 2021 at 23:33

3 Answers 3

1

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'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.