mkab 0 Light Poster

I have a problem making a select tag in php code dynamic. I'm creating a website (homework) using php and connecting to an oracle database which has all the info needed. Each agency has a list of vehicules.

I created a select option tag for agencies and for vehicules. I want the data for vehicules to change depending on what agency the user chooses. So if the user chooses agency A, the vehicules that should be available on the select option tag for vehicules should be that of agency A1 and no other agency. So far I have all the vehicules of all the agencies in the select tag.

I tried creating a function that checks the user's input an selects the vehicule from the agency the user chose. Then it initialise a $query in function. And then use that query to fill in my select tag. Here is the code:

function chooseVehicule(){
                    if( isset($_POST['agence']) && $_POST['agence']=='CARPORT'){
                        echo isset($_POST['agence']);
                        $query='SELECT v.marque, modele, ag.nom_agence, v.num_immatriculation from vehicules v, agences ag
                                WHERE v.id_agence=ag.id_agence
                                AND ag.nom_agence=="CARPORT"';
                    }
                }

then i call the function when needed.

But it gives me

Undefined index: agence

error. Any ideas please? Here is my complete code.

<?php

                function chooseVehicule(){
                    if( ($_POST['agence'])=='CARPORT'){
                        $query='SELECT v.marque, modele, ag.nom_agence, v.num_immatriculation from vehicules v, agences ag
                                WHERE v.id_agence=ag.id_agence
                                AND ag.nom_agence=="CARPORT"';
                    }
                }
        echo '<h3>'; echo 'Pour la location d\'une vehicule, veuillez remplir ce formulaire';echo'</h3>';


               /*CLIENTS*/
               $query="SELECT nom_client from CLIENTS";
               $state=oci_parse($conn, $query);
               oci_execute($state, OCI_COMMIT_ON_SUCCESS);

               echo '
                   <form action="location.php" method="post">
                   <p><label for="client">Select your Name</label>
                   <select name="client" id="client">';

               while(($row = oci_fetch_array($state, OCI_BOTH))){
                   echo'<option value="'.$row[0].'">'.$row[0]; echo'</option>';
               }
               echo'</select></p>'; 
               echo '</form>';


               /*AGENCES*/
        $query="SELECT nom_agence from AGENCES";
        $state=oci_parse($conn, $query);
                oci_execute($state, OCI_COMMIT_ON_SUCCESS);
                echo '
                    <form action="location.php" method="post">
                        <p><label for="agence">Select a Company</label>
                        <select name="agence" id="agence">';

                        while(($row = oci_fetch_array($state, OCI_BOTH))){
                            echo '<option value="'.$row[0].'">'.$row[0]; echo'</option>';
                        }

                    echo '</select></p>';
                    echo '</form>';


                /*VEHICULES*/

               $query="SELECT marque, modele, nom_agence, num_immatriculation from vehicules v, agences ag
                       WHERE v.id_agence=ag.id_agence
                       AND type_vehicule='UTILITAIRE'
                       order by nom_agence";

               $state=oci_parse($conn, $query);
               oci_execute($state, OCI_COMMIT_ON_SUCCESS);

               echo '
                   <form action="location.php" method="post">
                   <p><label for="vehicule">Select a Vehicule</label>
                   <select name="vehicule" id="vehicule">';
               echo '<optgroup label="Utilitaire">';
               while(($row = oci_fetch_array($state, OCI_BOTH))){
                   echo '<option value="'.$row[3].'">'.$row[2]. ' ' . $row[0] . ' ' . $row[1]; echo '</option>';
               }
               echo '</optgroup>';

               $query="SELECT v.marque, v.modele, ag.nom_agence, v.num_immatriculation from vehicules v, agences ag
                       WHERE v.id_agence=ag.id_agence
                       AND type_vehicule='VOITURE'
                       order by nom_agence";

               echo '<optgroup label="Voiture">';
               $state=oci_parse($conn, $query);
               oci_execute($state, OCI_COMMIT_ON_SUCCESS);
               while(($row = oci_fetch_array($state, OCI_BOTH))){
                   echo '<option value="'.$row[3].'">'.$row[2]. ' ' . $row[0] . ' ' . $row[1]; echo '</option>';
               }
               echo '</optgroup>';

               echo'</select></p>'; 
               echo '</form>';

    oci_free_statement($state);
    oci_close($conn);
        ?>

I have a form for agency which has the id="agence" in its select tag. I also have a form for vehicules which also has its select tag. What I want is that: the vehicules that should show in the select tag for vehicule should depend on the agency chosen. So if I chose CARPORT for example then only the vehicules in the agency CARPORT should be shown in the vehicule select tag

Dani AI

Generated

— quick diagnosis: three separate things in the posted code explain the behavior.

  • Accessing $_POST['agence'] without checking whether it exists causes the "Undefined index" notice.
  • Each select is echoed inside its own <form>, so the vehicle-rendering code never sees the submitted agency value.
  • The SQL shown in the snippet uses C-style comparison/quoting mistakes; Oracle string literals use single quotes and server-side values must be bound (do not concatenate raw POST data).

A simple, safe server-side pattern (single form, auto-submit on agency change, bind variables, HTML-escape output):

<?php
$selected = $_POST['agence'] ?? '';
// build agency list
$st = oci_parse($conn, "SELECT nom_agence FROM AGENCES ORDER BY nom_agence");
oci_execute($st);
echo '<form method="post">';
echo '<select name="agence" id="agence" onchange="this.form.submit()">';
while ($r = oci_fetch_array($st, OCI_ASSOC)) {
  $name = htmlspecialchars($r['NOM_AGENCE']);
  $sel = ($name === $selected) ? ' selected' : '';
  echo "<option value=\"$name\"$sel>$name</option>";
}
echo '</select>';

// if an agency is chosen, bind it and fetch matching vehicles
if ($selected) {
  $q = "SELECT marque, modele, num_immatriculation FROM vehicules v JOIN agences ag ON v.id_agence=ag.id_agence WHERE ag.nom_agence = :agency";
  $s = oci_parse($conn, $q);
  oci_bind_by_name($s, ':agency', $selected);
  oci_execute($s);
  echo '<select name="vehicule" id="vehicule">';
  while ($v = oci_fetch_array($s, OCI_ASSOC)) {
    $label = htmlspecialchars($v['MARQUE'].' '.$v['MODELE']);
    echo "<option value=\"{$v['NUM_IMMATRICULATION']}\">$label</option>";
  }
  echo '</select>';
}
echo '</form>';
?>

AJAX alternative (keeps the page from reloading): have a small endpoint that returns JSON for a given agency, then populate the vehicule select with fetch() on agence change.

Checklist / cautions:

  • Always check isset or use the null coalescing operator before reading $_POST.
  • Use oci_bind_by_name to avoid SQL injection and to let Oracle reuse parsed statements.
  • Escape output with htmlspecialchars to avoid XSS.
  • Keep agency and vehicle selects in the same form or use an AJAX endpoint so the chosen agency is available when building vehicle options.
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.