0

to cut my story short, I have the following code:

                   if(mysqli_num_rows($results)>0)
               {
                    while($row=mysqli_fetch_assoc($results)){
                       $payrollID = $row['id'];
                       $payMonth = $row['payMonth'];
                       $totalTime = $row['totalTime'];
                       $totalMoney = $row['totalMoney'];
                       $isFinished = $row['isFinished'];
                           echo'  <hr><div class="alert alert-dark" role="alert">
                            <p class="text-center" font-weight:bold>Payroll History</p>
                            </div><hr>';
                
                           echo' <div class="col-md-6">
                           <div class="product-item">
                           <div class="down-content">
                           <a href="#"><h4>Payroll_ID:'.$payrollID.'</h4></a>
                           <br>
                           PayMonth: '.$payMonth.'<br>
                           Total Time: '.$totalTime.'<br>
                           Total Money: ₪'.$totalMoney.'<br>
                           <form action="payrollControls.php" method="post>
                           <input type="hidden" name="id" value="'.$payrollID.'">
                           <input type="submit" name="viewPayroll" value="View Payroll" class="btn btn-secondary">
                           </form>
                           </div>
                           </div>
                           </div>
                           ';

               }
              }

Now the line <a href="#"><h4>Payroll_ID:'.$payrollID.'</h4></a> - prints the $payrollID successfully but down in the form once I used it as a hidden input in the form and the button name its 100% matching what is written in POST request, it still goes into the last last line echo($id) and tells me it is an undefined variable on "payrollControls.php". Here is the code of payrollControls.php:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if(isset($_POST['viewPayroll'])) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        include 'db.php';
        echo' 
            <div class="col-md-12">
            <div class="row">';
        $id = $_POST['payrollID'];
        // and some more code that is not related to my question
    }
    echo'
        </div>
        </div>
        ';
}
else
    echo($id);
Phil
  • 157,677
  • 23
  • 242
  • 245
  • If I failed to explain myself well, I'll try to do it again: Why on payrollControls.php the post request is going to the last else block and not inside the if block? – Ameen Assadi Aug 23 '21 at 00:41
  • Are you using AJAX? – Musa Aug 23 '21 at 00:44
  • 1
    Your hidden input's name is `id`, not `payrollID`. You're also missing the closing `"` in `method="post>` – Phil Aug 23 '21 at 00:45
  • 1
    I updated my comment, you're missing a `"` in your `
    ` tag
    – Phil Aug 23 '21 at 00:49
  • Wow man, I have been coding for like 6 hours straight and just reached a level where I really did not notice that even though I checked much times so I ended up posting it here, thanks alot! – Ameen Assadi Aug 23 '21 at 00:50
  • 2
    I strongly suggest you avoid using `echo` to output great chunks of mostly static HTML. See [this answer](https://stackoverflow.com/a/18140388/283366) for what I consider a much cleaner approach – Phil Aug 23 '21 at 00:53

0 Answers0