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

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.