PHP a Table calls another PivotTable

0

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

I've already done this:

    <?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 the submit is not being for the submit of the internal form, but for the external form.

I've tried this.form.submit ()

I do not know how to call give submit in internal form.

The testfile7.php file:

    <!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>
    
asked by anonymous 26.04.2018 / 15:39

1 answer

1

If you do not have many records you can do the way I put it 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 given I seem to be few options of countries and cities, so I have already decided to start with this simplified solution.

    
26.04.2018 / 15:53