-1

I was try to get CompanyID and JobListingID from the while loop so after clicking apply button it can pass the data to another table called application, but somehow it only pass the latest JobListingID with the CompanyID from the joblisting table to the application How am I suppose to do to make it pass the data that I need?

<?php
include ("Jobseeker_session.php");
require_once('connect.php');


?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<html>
<head>
    <title>Job Seeker Website</title>
    <link rel="stylesheet" href="style.css">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
</head>

<body>
    <div class="sidebar">
        <div class="logo-details">
          <i class='#' ></i>
          <span class="logo_name">Job Seekeer</span>
        </div>
          <ul class="nav-links">
            <li>
              <a href="JobSeekerProfile.php" >
              <i class='bx bxs-user-detail' ></i>               
              <span class="links_name">Profile</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerAboutUs.php" >
              <i class='bx bxs-user-account' ></i>              
              <span class="links_name">About Us</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerJobListing.php">
              <i class='bx bx-list-ul' ></i>                  
              <span class="links_name">Job Listing</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerCompanyListing.php">
              <i class='bx bx-list-ul' ></i>                  
              <span class="links_name">Company Listing</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerFavourite.php">
              <i class='bx bxs-bookmark' ></i>
              <span class="links_name">Favourite</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerApplication.php">
              <i class='bx bxs-business'></i>
              <span class="links_name">Application</span>
              </a>
            </li>
           
            <li>
              <a href="JobSeekerReportHistory.php">
              <i class='bx bxs-time'></i>
              <span class="links_name">History Report</span>
              </a>
            </li>

            <li>
              <a href="JobSeekerMessage.php">
              <i class='bx bxs-chat' ></i>
              <span class="links_name">Message</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerResume.php">
              <i class='bx bx-paperclip'></i>
              <span class="links_name">Resume</span>
              </a>
            </li>
    
            <li class="log_out">
              <a href="#">
              <i class='bx bx-log-out'></i>
              <span class="links_name">Log out</span>
              </a>
            </li>
          </ul>
    </div>
    
          <nav>
        
            <div class="sidebar-button">
              <i class='bx bx-menu sidebarBtn'></i>
              <span class="dashboard">Dashboard</span>
            </div>
        
            <div class="search-box">
              <input type="text" placeholder="Search...">
              
            </div>
        
            <div class="profile-details">
              <span class="admin_name"></span>
              
            </div>
          </nav>

          <section class="home-section">
            <div class="home-content">
                <div class="all-boxes">
                    <div class="title">Job Listing</div>
                    
                        <div class="applicant-boxes">
                            <form method="post">
                            <div class="listing-boxes">
                                    <div class="left">
                                        <?php
                                        $sql = "select * from joblisting";
                                        $result = mysqli_query($connect,$sql);
                                        
                                        
                                        while($row = mysqli_fetch_assoc($result))
                                        {
                                        $eee = $row['JobListingID'];
                                        $new = $row['CompanyID']; 
                                        $sql2 = "select * from company_info WHERE CompanyID = '$new'";
                                        $result2 = mysqli_query($connect,$sql2);
                                        $row2 = mysqli_fetch_assoc($result2);
                                        if(is_array($row2)){ 
                                        ?>
                                        <div class="job-card">
                                            <div class="img">
                                                <img src="<?php echo $row2["CompanyLogo"]; ?>">
                                            </div>
                                            <div class="content">
                                                <h2 ><a href="JobSeekerJobDescription.php?jobid=<?php echo $eee;?>"><?php echo $row["JobTitle"]; ?></a></h2>
                                                <p><b>Company:</b> <?php echo $row2["CompanyName"]; ?><?php echo $new;?></p>
                                                <p><b>Location:</b> <?php echo $row["JobLocation"]; ?><?php echo $eee;?><?php echo $new;?></p>
                                                <p><b>Salary:</b> <?php echo $row["JobSalary"]; ?><?php echo $eee;?></p>
                                                <div class="action">
                                                
                                                <button type="submit" class="btn btn-primary" name="apply">Apply<?php echo $eee;?><?php echo $new;?></button>
                                                <button type="submit" class="btn btn-primary" name="favourite">Add To Favourite<?php echo $eee;?><?php echo $new;?></button>
                                                <a href="JobSeekerReport.php?joblistingid=<?php echo $eee;?>">Report Job</a>
                                                
                                            </div>
                                            </div>
                                            
                                        </div>
                                        <?php
                                        
                                                }
                                                
                                        
                                        }
                                        ?>
                                        
                                    </div>
                                
                            </div>
                            </form>
                        </div>
                </div>
                </div>
            </div>
            
          </section>          
            
            </div>
            
</body>
</html>
<?php
                                        
                                    
                                    
    if(isset($_POST['apply']))
    {date_default_timezone_set("Asia/Kuala_Lumpur");
        $date=date('d/m/Y H:i:s');
        $query = "INSERT INTO application(DateApplied,ApplicationStatus,Job_SeekerID,CompanyID,JobListingID)
        VALUES('$date','In Progress','$jobseekerid','$new','$eee')";
        $result = mysqli_query($connect,$query);
        }
?>          
                            





<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>
3
  • The form doesn't have any <input> fields to hold the parameters that you're submitting. How are you expecting to get anything from the form?
    – Barmar
    Commented Jun 29, 2023 at 0:20
  • The variables in the INSERT query aren't from the form submission. They're just the last values that you assigned to those variables in the loop.
    – Barmar
    Commented Jun 29, 2023 at 0:21
  • Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough!
    – Dharman
    Commented Jul 22, 2023 at 14:19

1 Answer 1

1

You don't need a form to do this. Easiest would be to generate url's with the parameters needed. Here's an example:

<?php
$path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $params);

var_dump($params); // Kint debugger
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php for ($i = 1; $i <= 2; $i++) : ?>
        <h2>Listing <?= $i ?></h2>
        <ul>
            <?php for ($j = 1; $j <= 3; $j++) : $id = uniqid(); ?>
                <li>
                    <p>Job <?= $j ?></p>
                    <a href="<?= $path ?>?action=apply&id=<?= $id ?>&listid=<?= $i ?>">Apply</a> | <a href="<?= $path ?>?action=favourite&id=<?= $id ?>&listid=<?= $i ?>">Favourite</a>
                </li>
            <?php endfor; ?>
        </ul>
    <?php endfor; ?>
</body>

</html>

Demo here

Common practice is that getting info/data uses GET requests and sending data for saving / mutation is done via POST requests, hence the url's with parameters.

If you do want to POST the parameters rather than via GET, you could put form tags around every item and put the id and listid in hidden inputs, so that you post one form on clicking the button for an item. In effect, you would have as many forms in the page as there are items.

But since you have more actions per item (apply and favourite), you could also consider POSTing via javascript with fetch and handling the result for favourite-action without refresh. Effectively creating a sort of API for your application.

Not the answer you're looking for? Browse other questions tagged or ask your own question.