skip to Main Content

can someone help me by seeing what is wrong with the program I created?

  1. Warning: Undefined array key "pertemuan" in D:filexampphtdocsuts_nimkoneksi.php on line 25
  2. Fatal error: Uncaught TypeError: Unsupported operand types: string / string in D:Filexampphtdocsuts_nimkoneksi.php:27 Stack trace: #0 D:Filexampphtdocsuts_nimadd_data.php(7): tambah(Array) #1 {main} thrown in D:Filexampphtdocsuts_nimkoneksi.php on line 27

my Code
koneksi.php

function tambah($data) {
    global $conn;

    $nim = htmlspecialchars($data["nim"]);
    $nama = htmlspecialchars($data["nama"]);
    $jurusan = htmlspecialchars($data["jurusan"]);
    $sks = htmlspecialchars($data['sks']);
    $pertemuan = htmlspecialchars($data['pertemuan']);
    $kehadiran = htmlspecialchars($data['kehadiran']);
    $persen = ($kehadiran / $pertemuan) * 100;
    $persen = min($persen, 100);

add_data.php

<?php
require 'koneksi.php';

if( isset($_POST["submit"]) ) {
    if( tambah($_POST) > 0 ){
        echo "
            <script>
                alert('data berhasil ditambah');
                document.location.href = 'index.php';
            </script>
        ";
    } else {
        echo "
            <script>
                alert('data gagal ditambah');
            </script>
        ";
    }
}
?>

<div class="col-md-3">
                 <div class="form-floating">
                    <select class="form-select" name="sks" id="sks" onchange="updatePertemuan()" required>
                            <option selected hidden>Pilih SKS</option>
                            <option value="2">2 SKS</option>
                            <option value="3">3 SKS</option>
                            <option value="4">4 SKS</option>
                        </select>
                        <label for="sks">SKS</label>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-floating">
                        <input type="text" name="pertemuan" id="pertemuan" 
                        class="form-control" disabled>
                        <label for="pertemuan">Pertemuan</label>
                    </div>
                </div>

   <script>
        function updatePertemuan() {
            const sks = document.getElementById('sks').value;
            let pertemuan = 0;

            if (sks === '2') {
                pertemuan = 14;
            } else if (sks === '3') {
                pertemuan = 21;
            } else if (sks === '4') {
                pertemuan = 29;
            }

            document.getElementById('pertemuan').value = pertemuan;
        }
    </script>

If there is a file/line of code you would like to see please tell me

I want to add the data to the database using PHP code

3

Answers


  1. Chosen as BEST ANSWER

    case solved, I don't know how the "disabled" attribute can't read the data on the input tag. I used

    <input name="pertemuan" id="pertemuan" disabled>
    

    but the data was not read into the $meeting array.

    but if I use

    <input name="pertemuan" id="pertemuan" readonly>
    

    then the data can be read


  2. You are trying to access a key from $data array which is not present on that array. You can simply handle this by using ternary operator.

    So, your code must like this:

    $pertemuan = isset($data[‘pertemuan’]) ? htmlspecialchars($data[‘pertemuan’]) : ”;

    Login or Signup to reply.
  3. The error message indicates that there are issues with accessing array keys in your code, specifically the "pertemuan" key. Let’s troubleshoot:

    Undefined array key "pertemuan": This warning indicates that the "pertemuan" key is not set in the array $data passed to the tambah() function. Make sure that the form field for "pertemuan" is indeed being submitted with the form.

    Fatal error: Uncaught TypeError: Unsupported operand types: This error occurs because the $kehadiran and $pertemuan variables are being treated as strings instead of numbers when performing arithmetic operations. This could happen if the values retrieved from the form are not converted to integers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search