mercredi 16 juin 2021

How to use checkbox in a form to select one or multiple choices from database?

I have this problem, where I would like to select one or more choices based on what is in the database, but I don't know how to do it. Below I will attach a code similar to the one from the search inputs only that I modified the first 3 lines under "isset" in the hope that maybe it will work, and I would like to know if I could make the code in such a way as to modify it and to work at checkboxes as well.

job-list.php (the form)

        <form action="../pages/jobs-list.php" method="POST">
            <div class="job-listing sidebar-container pr-3 pl-3 pt-2 pb-2" style="box-shadow: none; transform: none">

                <!-- Exceperience level -->
                <div class="sidebar-widget pt-2 ">
                    <h3 class="text-center" style="color: #2a41e8;"><b>Experiența de muncă</b></h3>
                    
                    <div class="switches-list">
                        <div class="switch-container" name>
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="Stagiar"><span class="switch-button"></span> Stagiar</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="Internship"><span class="switch-button"></span> Internship</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="Junior"><span class="switch-button"></span> Junior ( < 2 ani)</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="Middle"><span class="switch-button"></span> Middle (2 < 5 ani)</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="Senior"><span class="switch-button"></span> Senior ( > 6 ani)</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-experience[]" value="ManagerLeader"><span class="switch-button"></span> Manager/ Leader</label>
                        </div>
                    </div>
                </div>
                
                <!-- Job Types -->
                <div class="sidebar-widget">
                    <h3 class="text-center" style="color: #2a41e8;"><b>Tipul jobului</b></h3>

                    <div class="switches-list">
                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-jobType[]"><span class="switch-button"></span> Full Time</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-jobType[]"><span class="switch-button"></span> Part Time</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-jobType[]"><span class="switch-button"></span> Freelance</label>
                        </div>

                        <div class="switch-container">
                            <label class="switch"><input type="checkbox" name="search-jobType[]"><span class="switch-button"></span> Temporary</label>
                        </div>
                    </div>
                </div>

                <div class="text-center">
                    <button type="submit" name="submit-search-left" class="button ripple-effect pb-1 pt-1" onclick="window.location.href='../pages/jobs-list.php'">Caută</button>
                </div>
            </div>              
        </form>

job-list.php (the php)

                else if(isset($_POST['submit-search-left'])) {
                    $searchexperience = mysqli_real_escape_string($conn_search, $_POST['search-experience']);
                    $searchjobType = mysqli_real_escape_string($conn_search, $_POST['search-jobType']);
                    $sql = "SELECT * FROM article WHERE (a_experience LIKE '%$searchexperience%') AND(a_jobType LIKE '%$searchjobType%'";
                    $result = mysqli_query($conn_search, $sql);
                    $queryResult = mysqli_num_rows($result);

                    echo "<p class='alert alert-info mb-4 text-center col-12' style='color: blue;'><b>S-au găsit ".$queryResult." rezultate!</b></p>";

                    if($queryResult > 0) {
                        while($row = mysqli_fetch_assoc($result)) {
                            
                            echo"<a href='../pages/jobs-list/php?title=".$row['a_title']."&date=".$row['a_author']."'><div class='freelancer'>

                                <div class='freelancer-overview pr-0 pb-0 pt-0'>
                                    <div class='freelancer-overview-inner'>
                                        <!-- Bookmark Icon -->
                                        <span class='bookmark-icon'></span>
                                        
                                        <!-- Avatar -->
                                        <div class='freelancer-avatar'>
                                            <a href='single-freelancer-profile.html'><img src='' alt=''></a>
                                        </div>

                                        <!-- Name -->
                                        <div class='freelancer-name height_200'>
                                            <h4 class='pt-3'><a href='../pages/person-profile.php' class='noHoverBlueLine'>".$row['a_author']."</a></h4>
                                            <span>".$row['a_title']."</span>
                                            <p class='font-14 line-height-1_6 pr-3'>".$row['a_text']."... <a href='../pages/person-profile.php' class='readMore2'>Read more</a></p>
                                        </div>
                                    </div>
                                </div>

                                <!-- Details -->
                                <div class='freelancer-details noHoverBlue-a pr-3'>
                                    <div class='freelancer-details-list'>
                                        <ul>
                                            <li>Tip Job <strong class='pt-1';><i class='fas fa-clock'></i> ".$row['a_jobType']."</strong></li>
                                            <li><span style='padding-left: 5px;'>Locația</span><strong><i class='icon-material-outline-location-on'></i> ".$row['a_city']."</strong></li>
                                            <li>Nivel Pregătire <strong class='pt-1 pl-2';><i class='fas fa-user-graduate'></i> ".$row['a_experience']."</strong></li>
                                        </ul>
                                    </div>
                                    <div class='col-lg-9 col-md-6 pr-4' id='view-profile'>
                                        <a href='../pages/person-profile.php' class='noHoverBlueLine button button-sliding-icon ripple-effect'>View Profile <i class='far fa-long-arrow-alt-right'></i></a>
                                    </div>
                                </div>
                            </div></a>";
                        }
                    } else {
                        echo "Nu s-au găsit rezultate bazat pe ceea ce ai căutat!";
                    }
                }

Below I will attach the code from the search inputs to see from where I got the code I tried to modify it to make the checkbox version work(the code above).

if(isset($_POST['submit-search'])) {
                    $searchjob = mysqli_real_escape_string($conn_search, $_POST['search-job']);
                    $searchcity = mysqli_real_escape_string($conn_search, $_POST['search-city']);
                    $sql = "SELECT * FROM article WHERE (a_title LIKE '%$searchjob%' OR a_author LIKE '%$searchjob%' OR a_experience LIKE '%$searchjob%' OR a_jobType LIKE '%$searchjob%') AND a_city LIKE '%$searchcity%'";
                    $result = mysqli_query($conn_search, $sql);
                    $queryResult = mysqli_num_rows($result);

                    echo "<p class='alert alert-info mb-4 text-center col-12' style='color: blue;'><b>S-au găsit ".$queryResult." rezultate!</b></p>";

                    if($queryResult > 0) {
                        while($row = mysqli_fetch_assoc($result)) {
                            
                            echo"<a href='../pages/jobs-list/php?title=".$row['a_title']."&date=".$row['a_author']."'><div class='freelancer'>

                                <div class='freelancer-overview pr-0 pb-0 pt-0'>
                                    <div class='freelancer-overview-inner'>
                                        <!-- Bookmark Icon -->
                                        <span class='bookmark-icon'></span>
                                        
                                        <!-- Avatar -->
                                        <div class='freelancer-avatar'>
                                            <a href='single-freelancer-profile.html'><img src='' alt=''></a>
                                        </div>

                                        <!-- Name -->
                                        <div class='freelancer-name height_200'>
                                            <h4 class='pt-3'><a href='../pages/person-profile.php' class='noHoverBlueLine'>".$row['a_author']."</a></h4>
                                            <span>".$row['a_title']."</span>
                                            <p class='font-14 line-height-1_6 pr-3'>".$row['a_text']."... <a href='../pages/person-profile.php' class='readMore2'>Read more</a></p>
                                        </div>
                                    </div>
                                </div>

                                <!-- Details -->
                                <div class='freelancer-details noHoverBlue-a pr-3'>
                                    <div class='freelancer-details-list'>
                                        <ul>
                                            <li>Tip Job <strong class='pt-1';><i class='fas fa-clock'></i> ".$row['a_jobType']."</strong></li>
                                            <li><span style='padding-left: 5px;'>Locația</span><strong><i class='icon-material-outline-location-on'></i> ".$row['a_city']."</strong></li>
                                            <li>Nivel Pregătire <strong class='pt-1 pl-2';><i class='fas fa-user-graduate'></i> ".$row['a_experience']."</strong></li>
                                        </ul>
                                    </div>
                                    <div class='col-lg-9 col-md-6 pr-4' id='view-profile'>
                                        <a href='../pages/person-profile.php' class='noHoverBlueLine button button-sliding-icon ripple-effect'>View Profile <i class='far fa-long-arrow-alt-right'></i></a>
                                    </div>
                                </div>
                            </div></a>";
                        }
                    } else {
                        echo "Nu s-au găsit rezultate bazat pe ceea ce ai căutat!";
                    }
                }



Aucun commentaire:

Enregistrer un commentaire