How to create a dynamic form in php

Asked

Viewed 1,865 times

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.

  • 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.

  • You can show me how to do this?

  • What does this line mean? while(!$tipo_info == '4') Shouldn’t be while($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'....

  • While typ_info is different from 4?

  • Not because if it is equal to 4 that is the finish, must give a Submit.

  • So it should be like this: while($tipo_info !== '4')

  • Why don’t you use case switch? it’s faster than while and ifs... it gets more stylish too.

  • @fabricio_wm see if this helps http://jsfiddle.net/dieegov/kN84b/2/

  • I’ll try then. Thank you.

  • 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.

Show 6 more comments

1 answer

1

Try this way:

<?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 value='".$reg["cod_tipo_info"]."'>".$reg["tipo_info_questionario"]."</option>";
    $tipo_info = $reg["cod_tipo_info"];
}
echo "</select>";
echo "<br /><br />";

switch($tipo_info){

case 1: 
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary     pull-right\">Avancar</button>";
    break; 
case 2: 
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
case 3: 
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
case 4: 
    echo "<button type=\"submit\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break; 
}
  • I can’t use if instead of while pq otherwise, I get a single item in my select. .

  • you’re right, I edited the answer, always falls in 4??

  • yes. 4 is the Submit pq the <button type = "Submit">. The others are with type=button. They do nothing but paging. Thank you.

  • It will always fall in 4 because while is set the following line $tipo_info = $reg["cod_tipo_info"];

  • Another detail I noticed, is that you’re naming all the <option> with name="ativo", option does not have this property.http://www.w3schools.com/tags/tag_option.asp

  • So. how do I assign the selected value to the variable, Diego?

  • To not do the way I sent you above, you need to send the value to be able to show the button after, get a get or even post and then show the button

  • So I don’t understand what he wants to get with this button... Because no while, will take the value of the last record and check on the switch or if whatever it is.

  • @Marceloaymone, what he really wants is this: http://jsfiddle.net/dieegov/kN84b/2/ select the option in select and show a button (send or forward) in the case of this code when selecting 4 i already do Submit

  • You can also do this with pure javascript, it will give the post on the same page and if it checks which was the option you sent there it shows in echo http://jsfiddle.net/dieegov/9G6ut/

  • Um, I get it, so you really need to use a post or javascript, Fabricio, you know what a server side is, or client side? When you run the script, php will not wait for the user to select, it will already use the value saved in the last instruction to finish the script.

  • Staff. This is a page that already has programming that does more or less like this: http://jsfiddle.net/U7Vvb/ That’s why I didn’t want to touch javascript or jquery, unless it doesn’t interfere with the link code.

  • @Marceloaymone. I know. I think the way is this. How can we do?

  • This one is but current and correct. http://jsfiddle.net/fabricio_wm/U7Vvb/3/

  • Can someone help me, please?

  • I stopped there and it’s working, what happens now?

  • Correct Marceloaymone. I put the code in Jsfiddle so that you see that I have a programming that already works. I need to make my idea work, either through PHP, as I was trying or through Javascript, as @Diegovieira showed. Javascript, you need to have the option of when you choose option 1, it goes to a select and if you choose option 2 goes to a question with radio button. And so I have a dynamic form. Thank you.

  • That’s right @Marceloaymone.

Show 13 more comments

Browser other questions tagged

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