简体   繁体   中英

html select option tag in php (oracle database)

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( ($_POST['agence'])=='CARPORT'){
                        $query='SELECT marque, modele, nom_agence, num_immatriculation from vehicules v, agences ag
                                WHERE v.id_agence=ag.id_agence
                                AND ag.nom_agence=="CARPORT"';
                    }
                }

then

chooseVehicule();

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 didn't use the function chooseVehicule since it gives me an error. Thanks.

The keys in the $_POST superglobal will be sent to the server with the same names as the form fields. If this form is submitting to itself, you will need to send a field with the name agence to avoid this error. PHP is throwing the error because there is no key agence in the $_POST array.

In order to bypass this check, since this page is processing numerous forms, you can isset each key first before running a check against it:

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

You should do this for all the forms that are sent selectively.

EDIT:

You're going to need to put some state into the page, 'state' in the sense of how far the user has gotten into the selection process. Let's make it so that the form for vehicles only shows if the user has already selected an agency. This form should be wrapped in a conditional.

wrap all the logic for the vehicles form in this conditional:

if(isset($_POST['agence'])){
  $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>';
}

Since this page is procedural, this will make it so that after a selection is made for the agency, then and only then will the vehicle form appear.

We're going to need to make the query for vehicles have a WHERE clause that reflects the selection that was made by the user. It looks as though you are joining for the id across two tables so the where clause will either need to lookup by agency id or agency name, depending on the database design.

If the vehicle form is put in the conditional like this, we can be certain the user has already made a selection of agency and progressed to the second "state" of the form. You'll need to then parameterize your sql statement and pass in the selected id.

For some reason, this isn't set $_POST['agence']. If you do a isset($_POST['agence']) it will return false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM