PHP One Table Calls Another Dynamic Table

Asked

Viewed 34 times

0

I am needing that when the user selects a combo item that the next combo is loaded with the data related to the first.

I already did that:

    <?php
        session_start();
    ?>
    <!DOCTYPE HTML> 
    <html>
        <head>
            <title>PHP form select box example</title>
        </head>

        <body>

        <?php
            $selected='';
            function get_options($select)
            {
                $countries=array ('United States'=>1,'United Kingdom'=>2,'France'=>3,'Mexico'=>4,'Russia'=>5,'Japan'=>6);
                $options='';
                while(list($k,$v)=each($countries))
                {
                    if($select==$v)
                    {
                        $options.='<option value="'.$v.'" selected>'.$k.'</option>';
                    }
                    else
                    {
                        $options.='<option value="'.$v.'">'.$k.'</option>';
                    }
                }
                return $options;
            }
            if(isset($_POST['countries']))
            {
                $selected= $_POST['countries'];
                $_SESSION['pais'] = $selected;  
            }

            function get_cidade()
            {
                if(isset($_SESSION['pais'])){
                $pais = $_SESSION['pais'];
                $options2='';

                if($pais==1)
                {
                    $options2.="<option value='New York' selected>New York</option>\n";
                    $options2.="<option value='San Francisco'>San Franciso</option>\n";
                    $options2.="<option value='Washington'>Washington</option>\n";
                }
                if($pais==2)
                {
                    $options2.="<option value='London'>London</option>";
                    $options2.="<option value='Manchester'>Manchester</option>";
                    $options2.="<option value='Edimburgo'>Edimburgo</option>";
                }
                if($pais==3)
                {
                    $options2.="<option value='Paris'>Paris</option>";
                    $options2.="<option value='Lyon'>Lyon</option>";
                    $options2.="<option value='Marselha'>Marselha</option>";
                }
                if($pais==4)
                {
                    $options2.="<option value='Cuidad del Mexico'>Cuidad del Mexico</option>";
                    $options2.="<option value='Guadalajara'>Guadalajara</option>";
                    $options2.="<option value='Monterrei'>Monterrei</option>";
                }
                if($pais==5)
                {
                    $options2.="<option value='Moscou'>Moscou</option>";
                    $options2.="<option value='Samara'>Samara</option>";
                    $options2.="<option value='Kaluga'>Kaluga</option>";
                }
                if($pais==6)
                {
                    $options2.="<option value='Toquio'>Toquio</option>";
                    $options2.="<option value='Quioto'>Quioto</option>";
                    $options2.="<option value='Hiroshima'>Hiroshima</option>";
                }

                return $options2;
                }
            }
        ?>
        <form name="frm1" action="testetabelas7.php" method="post">
            <form name="fcountries" id="frm2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <label for='formCountries[]'>Select the countries that you have visited:</label><br>
                <select name="countries" onchange="form.submit();" >
                    <?php echo get_options($selected); ?>
                </select><br>
            </form>
            <p>Cidade</p>
            <form name="cities" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <select name="cidade">
                    <?php echo get_cidade(); ?>
                </select> 
            </form>
            <input form="frm1" type="submit" value="Gravar" >
        </form>


        </body>
    </html>

The problem is that when I change the option Submit is not being for the internal form Submit, but for the external form.

I’ve tried this.form.Ubmit()

I don’t know what to call giving Ubmit in the internal form.

The tested file7.php:

    <!DOCTYPE HTML> 
    <html>
        <head>
            <title>PHP form select box example</title>
        </head>

        <body>

        <?php
            $co = $_POST['countries'];
            $cy = $_POST['cidade'];
            echo '<p>'.$co.'</p>';  
            echo '<p>'.$cy.'</p>';  
        ?>  

        </body>
    </html>
  • I suggest you only have one form and use ajax to insert the select options

  • Could you give a simple, practical example? I am avoiding the maximum javascript, because the old version of this was too slow and heavy (100% java) and we are migrating to PHP that for now proved many times faster.

  • 1

    Remember that Javascript will take all server load and network traffic, so try to check what you consider a "heavy" application... Is it heavy for the server or client? If you are using Ajax try using this example: https://itsolutionstuff.com/post/php-how-to-make-dependent-dropdown-listusing-jquery-ajaxexample.html

  • Javascript and Java are very different things

  • Heavy... and slow...the patches made the project impossible.... I know there is controversy... but simple things were taking 3 to 5 minutes to open...

  • Fernando, very good, but that’s why I avoid the "javas" you have to use the libraries: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>..... This is not very pleasant, because there are people who will use the cell phone.... the less stuff the better and faster.... but I’ll try...thanks for now... is that everyone thinks it’s impossible in PHP... and I’m almost there

  • Javascript libraries (in the example given by jquery) are included to support asynchronous requests with the server. In this example he used the external library to make it simpler, but you can have this library within your own project. The external css (bootstrap) has also been placed only as an example and does not need to be used or can be imported into your project as well,

Show 2 more comments

1 answer

1

If you do not have many records you can do as I put below. Otherwise you will have to make a form with dynamic combobox using Ajax or something similar.

<script>
function pais_onchange(){
    var selPaisValue = document.getElementById("pais").value;
    var selCidade = document.getElementById("cidade");

    for(var i=selCidade.options.length-1;i>=0;i--){
        selCidade.remove(i);
    }

    if(selPaisValue=="1"){
        var option = document.createElement('option');
        option.text = "A1";
        option.value="A1";
        selCidade.add(option);
        option = document.createElement('option');
        option.text = "B1";
        option.value="B1";
        selCidade.add(option);
    }
    else if(selPaisValue=="2"){
        var option = document.createElement('option');
        option.text = "A2";
        option.value="A2";
        selCidade.add(option);
        option = document.createElement('option');
        option.text = "B2";
        option.value="B2";
        selCidade.add(option);
    }
}
</script>

Pais: 
<select id="pais" name="pais" onchange="pais_onchange();">
<option value="S" selected="selected">-Escolha o País-</option>
<option value="1">País 1</option>
<option value="2">País 2</option>
</select>

<br />
Cidade: 
<select name="cidade" id="cidade">
</select>

By the code indicated to me seem to be few options of countries and cities, so I decided to leave now for this simplified solution.

  • Great, really good code, the cleanest I’ve seen, now if I go get the Mysql data to create an array? And I have another connection to Postgresql.

  • If you take the BD to put in javascript just make two loops repeating the structure placed. Loop the countries and repeat if(selPaisValue=="$cod_pais") ( ...) when it works well, within this loop make a second loop that creates the options of the cities: var option (...) until selCidade.add(option);

  • remarks: see that from the second country IF insert a ELSE before the next IF (not mandatory, but a good practice even to keep the code faster). also from the first option it is not necessary to declare again the variable.

  • So I’m back to square one.... all java... and this the company does not want... the connection with Mysql and Postgresql of PHP is super simple... I’m sorry... so I put in PHP and not in the Java group

  • We are not talking about Java but Javascript which are totally different things. You will have no option to do this without reloading the page only using PHP, without Javascript (which is what runs in the client to communicate with the server or does the control directly in the client).

  • The is not possible, I already have several sites.... and I managed to reach a good denominator.. the guy above wheel calling each other.. I tested with database and everything... just need now hit the event ONCHANGE... if you get this is ready...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.