Passing data to a modal with PHP and Ajax

Asked

Viewed 1,057 times

0

I am creating a system and at a certain moment the user can register the measurements of a person. For this, I would like to redeem in the table of people the ID and the Name, so that they are displayed in the modal, but I am not succeeding, even trying in several ways. I am trying to use Ajax, since to include and delete the information I have already used this tool successfully.

My student consultation table is generated with the following actions:

$tabela .= "\n  <td class='grid' style='width:8%' align='center'>";
$tabela .= "    <button class='btn btn-warning' onclick='_editar($codigo)' type='button'><span class='glyphicon glyphicon-edit'></span></button>";
$tabela .= "</td>";
$tabela .= "\n  <td class='grid' style='width:8%' align='center'>";
$tabela .= "    <button class='btn btn-danger' onclick='_excluir($codigo)' type='button'><span class='glyphicon glyphicon-remove-circle'></span></button>";
$tabela .= "</td>";

Where the code (id) is passed as parameter for the edit exclusion methods.

In _edit I have:

function _editar(varFiltro){
	$('#txtID').val(varFiltro);
	
	$.ajax({
		url: 'index.php?controle=Aluno&acao=editar',
            dataType: 'html',
            type: 'POST',
            data: {
				idAluno: varFiltro
            },
            dataType:"json",
            success: function(data) {
                $('#txtID').val(data["id"]);
                $('#txtNome').val(data["nome"]);  
			}
    });
	$('#frmModal').modal('show');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

And on my watch:

    public function editar(){
    $this->modelo('Aluno');

    $idAluno = $_POST['idAluno'];

    $sql  = "select id, nome from aluno where id = $idAluno";

    $result = $this->nome->pesquisar($sql);

    $arr = Array();

    $html = "";

    if($result){
        while($consulta = mysql_fetch_assoc($result)){    
            $arr['id'] = consulta['id'];
            $arr['nome'] = consulta['nome'];
        }
    }

    die(json_encode($arr));

   }

And the view looks like this:

    <div class="modal fade" id="frmModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true" style='max-height: 100%; overflow-y: auto;'>
  <div class="modal-dialog modal-dialog-centered" role="document" style="max-width: 900px;">
    <div class="modal-content">
      <div class="modal-header">
          <table style="width: 100%;">
              <tr  width="100%">
                  <td width="90%">
                       <h3 class="modal-title" id="modalLabel">Cadastro de Medidas</h3>  
                  </td>
                  <td width="10%">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true"><h3>&times;</h3></span>
                      </button>  
                  </td>                            
              </tr>
          </table>
      </div>
      <form id="medida" method="post">
      <div class="modal-body">
            <table class="table table-striped table-bordered table-hover" style="width:100%">
                <tr>
                    <!-- IDENTIFICADOR -->
                    <td class="borda_transparente" style='width:20%'>
                        <div style="width:95%;" class="selectpicker">
                            <label for="txtID">Nº ID</label>
                        </div>
                        <div style="margin-bottom: 10px; width:60%" class="selectpicker">
                            <input class="form-control" type="text" name='txtID' id="txtID" disabled/>
                        </div>
                    </td>

                    <!-- Título do Menu -->
                    <td class="borda_transparente" style='width:100%'>
                        <div style="width:95%;" class="selectpicker">
                            <label for="txtNome">Nome</label>
                        </div>
                        <div style="margin-bottom: 10px; width:95%" class="selectpicker">
                            <input class="form-control" type="text" name='txtNome' id="txtNome" maxlength="40"/>
                        </div>
                    </td>

                </tr>
            </table>    
            </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-success" aria-label="Left Align" onclick="_gravar();"><span class="glyphicon glyphicon-ok" aria-hidden="true" style="margin-right:5px"></span>Gravar</button>          
            <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        </div>
      </form>
      <div id="mensagemModal"></div>
    </div>
  </div>
</div>

Only the ID is filled, according to the image, the name remains blank:

inserir a descrição da imagem aqui

  • if you give a console.log( $('#txtNome').val(data["nome"]); shows the name?

  • You are searching for a loop in php, try to search only one record

  • Makes a console.log(data) in the success and see if JSON is coming right with the name value too.

  • You’ve already observed that you’re repeating dataType twice in the ajax of the _edit function?

  • I’ve tried all the possibilities presented, and it still doesn’t work, I’ve really tried it.

No answers

Browser other questions tagged

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