Dynamic form in html ,jquery/ajax and php

Asked

Viewed 119 times

0

I don’t have much knowledge in js/ajax, so I n able to get it to remove 2 dynamic element, just one, I think q might be some "beast" error more as n know the right language I’m not able to solve alone, when it is only a field equal to the last of the lecture it removes quiet, when it has 2 elements it removes only the first

          $(function(){
            $("#estados").change(function(){
              var uf = $(this).val();
              $.ajax({
                type: "POST",
                url: "func/exibe_cidade.php?uf="+uf,
                dataType:"text",
                success: function(res){
                  $("#cidades").children(".cidades").remove();
                  $("#universidades").children(".universidades").remove();
                  $("#palestras").children(".palestras").remove();
                  $("#cidades").append(res);

                }
              });
            });
            $("#cidades").change(function(){
              var id = $(this).val();
              $.ajax({
                type: "POST",
                url: "func/exibe_universidade.php?id="+id,
                dataType:"text",
                success: function(res){
                  $("#universidades").children(".universidades").remove();
                  $("#palestras").children(".palestras").remove();
                  $("#universidades").append(res);

                }
              });
            });
            $("#universidades").change(function(){
              var id = $(this).val();
              $.ajax({
                type: "POST",
                url: "func/exibe_palestra.php?id="+id,
                dataType:"text",
                success: function(res){
                  $("#palestras").children(".palestras").remove();
                  $("#palestras").append(res);

                }
              });
            });
          });
<div class="form-group">
                  <label class="col-md-4 control-label">Estado</label>  
                  <div class="col-md-4 inputGroupContainer">
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
                            <select id="estados" class="form-control" name="estados">
                            <option selected disabled>Escolha seu estado</option>
                            <?php
                              require_once "func/conn.php";
                              $sql = ('SELECT * FROM tb_estado ORDER BY nome ASC');
                              $sql = $conexao->prepare($sql);
                              $sql->execute();
                              $lista = $sql->fetchAll();
                              for ($i=0; $i < count($lista); $i++) { 

                                $uf = $lista[$i]['uf'];
                                $nome = $lista[$i]['nome'];
                            ?>

                            <option value="<?php echo $uf?>"><?php echo utf8_encode($nome)?></option>

                            <?php } ?>
                        </select>
                     </div>
                  </div>
               </div> 
              <div class="form-group">
                  <label class="col-md-4 control-label">Cidade</label>  
                  <div class="col-md-4 inputGroupContainer">
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
                        <select class="form-control" name="cidades" id="cidades" required>
                          <option value="" selected disabled>Selecione o estado primeiro</option>
                        </select>
                     </div>
                  </div>
               </div>  
                <div class="form-group">
                  <label class="col-md-4 control-label">Universidade</label>  
                  <div class="col-md-4 inputGroupContainer">
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
                        <select class="form-control" name="universidades" id="universidades" required>
                          <option value="" selected disabled>Selecione a cidade primeiro</option>
                        </select>
                     </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-md-4 control-label">Palestra</label>  
                  <div class="col-md-4 inputGroupContainer">
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
                        <select class="form-control" name="palestras" id="palestras" required>
                          <option value="" selected disabled>Selecione a universidade primeiro</option>
                        </select>
                     </div>
                  </div>

ai in case the part of php is working like what I want, the part that n works is to remove the field that would

    $("#universidades").children(".universidades").remove();
$("#palestras").children(".palestras").remove();
$("#universidades").append(res);

it back in php database

<?php

require_once "conn.php";

$uf = $_GET['uf'];
$sql = 'SELECT * FROM tb_cidade WHERE uf = ? ORDER BY nome';
$stm = $conexao->prepare($sql);
$stm->bindValue(1, $uf);
$stm->execute();
$lista = $stm->fetchAll();
for($i=0;$i<count($lista);$i++){ 
	$id = $lista[$i]['id'];
	$nome = $lista[$i]['nome'];

	echo utf8_encode('<option value="'.$id.'" class="cidades">'.$nome.'</option>');
	}
?>

  • What is the value of res? Add the result of console.log(res) in the question

  • in the console only favicon error appears

  • What are the 2 dynamic elements that you refer to?

  • in vdd are 4 fields one depends on the other, state>city>university>lecture, the code works the only problem is when I change the state only reset cities, in the other field keeps the faculty/lecture of that state, this code here works for 1 remove 1 field only "$("#universities"). Children(".universities"). remove();" if I duplicate it and only change the id it n makes the function of it

  • In Ajax of the cities you are sending uf and in others id.

  • the more I’m dealing with in php, I recover the city by Uf, the problem is only at the time of removing, when I use the remove function, it only goes in 1 line, as I said above, if I duplicate the line it removes only the first to 2 line it ignores... I do not understand why

Show 1 more comment
No answers

Browser other questions tagged

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