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
– Costamilam
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.
– Wellington Telles Cunha
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
– Fernando
Javascript and Java are very different things
– Costamilam
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...
– Wellington Telles Cunha
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
– Wellington Telles Cunha
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,
– Fernando