Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function novo_campo_woocommerce( $checkout ) {
  2. echo '<select id="estados" class="form-control" name="estados">
  3. <option selected disabled>Escolha seu estado</option>';
  4. $conexao = new PDO('mysql:host=localhost;dbname=teste', 'user', 'password');
  5. $sql = ('SELECT * FROM tb_estado ORDER BY nome ASC');
  6. $sql = $conexao->prepare($sql);
  7. $sql->execute();
  8. $lista = $sql->fetchAll();
  9. for ($i=0; $i < count($lista); $i++) {
  10. $uf = $lista[$i]['uf'];
  11. $nome = $lista[$i]['nome'];
  12. echo "<option value='$uf'>$nome</option>";
  13. }
  14. echo '</select>';
  15. echo '<select class="form-control" name="cidades" id="cidades" required>
  16. <option value="" selected disabled>Selecione o estado primeiro</option>
  17. </select>';
  18. echo '<select class="form-control" name="universidades" id="universidades" required>
  19. <option value="" selected disabled>Selecione a cidade primeiro</option>
  20. </select>';
  21. ?>
  22. <script>
  23. $(function(){
  24. $("#estados").change(function(){
  25. var uf = $(this).val();
  26. $.ajax({
  27. type: "POST",
  28. url: "func/exibe_cidade.php?uf="+uf,
  29. dataType:"text",
  30. success: function(res){
  31. $("#cidades").children(".cidades").remove();
  32. $("#universidades").children(".universidades").remove();
  33. $("#cidades").append(res);
  34. console.log(res);
  35.  
  36. }
  37. });
  38. });
  39. $("#cidades").change(function(){
  40. var id = $(this).val();
  41. $.ajax({
  42. type: "POST",
  43. url: "func/exibe_universidade.php?id="+id,
  44. dataType:"text",
  45. success: function(res){
  46. $("#universidades").children(".universidades").remove();
  47. $("#palestras").children(".palestras").remove();
  48. }
  49. });
  50. });
  51. });
  52. </script>
  53. <?php
  54. }
  55.  
  56. <?php
  57.  
  58. require_once "conn.php";
  59.  
  60. $uf = $_GET['uf'];
  61. $sql = 'SELECT * FROM tb_cidade WHERE uf = ? ORDER BY nome';
  62. $stm = $conexao->prepare($sql);
  63. $stm->bindValue(1, $uf);
  64. $stm->execute();
  65. $lista = $stm->fetchAll();
  66. for($i=0;$i<count($lista);$i++){
  67. $id = $lista[$i]['id'];
  68. $nome = $lista[$i]['nome'];
  69.  
  70. echo '<option value="'.$id.'" class="cidades">'.$nome.'</option>';
  71. }
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement