0
I am creating a dynamic form that should work according to what is selected in the select. I don’t know what it is, but the variable type_info should receive the value of the cod_type_info and if it is equal to 4, it should give a Submit and if it is one of the other three options, it goes to different part of the form.
<?php
$tipo_info = 0;
echo "<fieldset>Selecione, qual o tipo da informação que deseja cadastrar.<br />";
$query = $con->query("SELECT * FROM pv_tipo_info_questionario");
echo "<select>";
echo "<option></option>";
while($reg = $query->fetch_array()) {
echo "<option name='ativo' value='".$reg["cod_tipo_info"]."'>".$reg["tipo_info_questionario"]."</option>";
$tipo_info = $reg["cod_tipo_info"];
}
echo "</select>";
echo "<br /><br />";
while(!$tipo_info == '4') {
if ($tipo_info == '1') {
echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
break;
} else if ($tipo_info == '2') {
echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
break;
} else if ($tipo_info == '3') {
echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
break;
}
}
if($tipo_info == '4') {
echo "<button type=\"submit\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
}
The problem is that it is always falling into 4 and is giving a Submit. If it falls into one of the three options, the button makes pagination for dynamic insertion.
– fabricio_wm
Your code actually works, while it ends in 4, so the variable you put there in the loop will always be 4 (in this case). I would use Jquery to solve your problem.
– Diego Vieira
You can show me how to do this?
– fabricio_wm
What does this line mean?
while(!$tipo_info == '4')
Shouldn’t bewhile($tipo_info == '4')
Without the!
, and at the beginning of the code, you assign$tipo_info = 0;
Then compare the field with strings$tipo_info == '4'
....– Marcelo Aymone
While typ_info is different from 4?
– fabricio_wm
Not because if it is equal to 4 that is the finish, must give a Submit.
– fabricio_wm
So it should be like this:
while($tipo_info !== '4')
– Marcelo Aymone
Why don’t you use case switch? it’s faster than while and ifs... it gets more stylish too.
– Marcelo Aymone
@fabricio_wm see if this helps http://jsfiddle.net/dieegov/kN84b/2/
– Diego Vieira
I’ll try then. Thank you.
– fabricio_wm
If you repair, your code will always end on the last option, it will run or not the
while
, but it will always continue for the if at the end. The main question is what context of this, if that’s all it is, can be solved without jquery or even javascript.– Marcelo Aymone