herdhemhola 0 Newbie Poster

I am having problem in submitting this form when I select "1" on the number of setting in html form. The submit button is working if I select 2 in the number of sitting from html form as it will show all the form fields but when I select 1, submit form is not working. I am new to PHP

This is my html form

<form name="register" method="POST" action="process.php">
<label>Select Number of Sitting(s)</label>
<select name="sitting" id="sitting" class="form-control" onchange="showDiv(this)">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<div class="output1" id="div1" name="onesitting">
<label>School Name</label>
<input type="text" name="schoolname[]" id="schoolname1" class="form-control" placeholder="School Name" required>
<label>Exam Type</label>
<select name="examboard[]" id="examboard1" class="form-control" required>
    <option value="">Exam Type</option>
    <option value="WAEC">WAEC(SSCE)</option>
     <option value="WAEC">WAEC(GCE)</option>
        </select>
    </div>
<div class="form2a" id="div2a" name="twositting">
<h4 style="text-align:center; margin-top: 10px; margin-bottom: 5%; background-color:#0d4115; width: auto; color: white;">SECOND SITTING</h4>
<label>School Name</label>
<input type="text" name="schoolname[]" id="schoolname2" class="form-control" placeholder="School Name" required>
<label>Exam Type</label>
<select name="examboard[]" id="examboard2" class="form-control" required>
<option value="">Exam Type</option>
<option value="WAEC">WAEC(SSCE)</option>
<option value="WAEC">WAEC(GCE)</option>
</select>
 </div>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example">
 <tr>
            <td width="12%"><label class="control-label">S/NO</label></td>
            <td width="53%"><label class="control-label">SUBJECTS</label></td>
            <td width="35%"><label class="control-label">GRADE</label></td>
        </tr>
        <tr>
            <td>1</td>
            <td>
                <?php include("config.php");
                    $sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
                    $query = mysqli_query($conn, $sql);
                    $count  = mysqli_num_rows($query);
                 ?>
                <select name="subjects[]" class="form-control" id="subject">
                    <option value="" selected="selected">Select subject</option>
                    <?php 
                        if($count > 0){
                            while($row = mysqli_fetch_assoc($query)){
                            $subject_id = $row['subject_id'];
                            $subject_name = $row['subject_name'];
                            echo "<option>" . $row['subject_name'] . "</option>";
                            }
                        }else{
                            echo "<option value = ''>Subject not available</option>";
                        }
                    ?>
                </select>
            </td>
            <td>
                <select name="grades[]" class="form-control">
                    <option value=""> Select</option>
                    <option value="A1">A1</option>
                    <option value="B2">B2</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>2</td>
            <td>
                <?php include("config.php");
                    $sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
                    $query = mysqli_query($conn, $sql);
                    $count  = mysqli_num_rows($query);
                ?>
                <select name="subjects[]" class="form-control" id="subject">
                    <option value="" selected="selected">Select subject</option>
                    <?php 
                        if($count > 0){
                            while($row = mysqli_fetch_assoc($query)){
                            $subject_id = $row['subject_id'];
                            $subject_name = $row['subject_name'];
                            echo "<option>" . $row['subject_name'] . "</option>";
                            }
                        }else{
                            echo "<option value = ''>Subject not available</option>";
                        }
                    ?>
                </select>
            </td>
            <td>
                <select name="grades[]" class="form-control">
                    <option value=""> Select </option>
                    <option value="A1">A1</option>
                    <option value="B2">B2</option>
                </select>
            </td>
        </tr>
        <tr>
    </table>
    <br>
    <input type="submit" name="submit" value="submit">
</form>

I used this javascript and CSS to show/hide the form fields based on the number of select at the top of the form.

function showDiv(select) {
  if (select.value == 2) {
    document.getElementById('div2a').style.display = "block";
  } else {
    document.getElementById('div2a').style.display = "none";
  }
}
<style>
.form2a {
  display: none;
}

</style>

The code below is my php code to process the form

 <?php
    $sitting = $_POST['sitting'];
    $schoolname = implode("," , $_POST['schoolname']);
    $examboard = implode("," , $_POST['examboard']);
    $examdate = implode("," , $_POST['examdate']);
    $examno = implode("," , $_POST['examno']);
    $subjects = implode("," , $_POST['subjects']);
    $grades = implode("," , $_POST['grades']);
    if(isset($_POST['submit'])){
        $stmt = $conn->prepare("INSERT INTO qualifications(sitting, schoolname,
        examboard, examdate, examno, subjects, grades) VALUES(?, ?, ?, ?, ?, ?,?)");
        $stmt->bind_param("sssssss", $sitting, $schoolname, $examboard, $examdate,
        $examno,  $subjects, $grades);
        $stmt->execute();
        $result = $stmt->affected_rows;
        $stmt -> close();
        $conn -> close();
        if($result)
        {
        header("location:success.php"); // user will be taken to the success page
        }
        else{
            echo "Oops. Something went wrong. Please try again";
        }
    }
?>

I was able to submit the form if I select 2 which shows all the form field on the number of sitting but the submit button is not working when I select "1" on the number of sitting which will hide some form. I just need conditional statement sample which will enable me to submit the form depending on the number of setting i select i.e whether "2" or "1" on the number of sittings

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.