I have a select file on 4 pages in 1 of them works and the other three does not answer the query

Asked

Viewed 34 times

0

function recuperaRacas($idAnimal) {
        $idAnimal = strip_tags($idAnimal);
        $query = "select raca_id 'idRaca', raca 'raca' from raca_nimal where id_nimal = " .$idAnimal. " order by raca;";
        $queryBuscaRaca = $this->conexaoMysql->query($query);
        $queryBuscaRaca->execute();
        $listaRacas = array();
        while ($row = $queryBuscaRaca->fetch(PDO::FETCH_ASSOC)) {
            $listaRacas[] = array('idRaca' => utf8_encode($row["idRaca"]),
                'raca' => utf8_encode($row["raca"]));
        }
        return $listaRacas;
    }

// Model

// controller start

case 'buscaRaca':
        $idAnimal = strip_tags($_REQUEST['idAnimal']);
        $listaRacas = $Doacao->recuperaRacas($idAnimal);
        $html = "";
        foreach ($listaRacas as $raca) {
            $html .= "<option value='" . $raca['idRaca'] . "'>" . $raca['raca'] . "</option>";
        }
        echo $html;


        break;
    }

// controller end

// beginning view

<div class="col-xs-8">
                    <div class="form-group">
                        <label>Qual a raça?</label>
                        <select class="" id="raca" name="raca">
                            <option value="">Selecione uma opcao</option>
                            <?php foreach ($listaRacas as $raca): ?>
                                <?php if ($raca['idRaca'] == $raca['idRaca']): ?>
                                    <option selected="selected" value='<?= $raca['idRaca'] ?>'><?= $raca['raca'] ?></option>";
                                <?php else: ?>
                                    <option value='<?= $raca['idRaca'] ?>'><?= $raca['raca'] ?></option>";
                                <?php endif ?>
                            <?php endforeach ?>
                        </select>
                    </div>
                </div>


    $(document).ready(function () {
        if ($('#tipo_nimal').val() != "") {
            buscarRacas();
        }
    });

    function buscarRacas() {
        var idAnimal = $('#tipo_nimal').val();
        $.ajax({
            url: '../Controller/DoacaoController.php',
            type: 'POST',
            dataType: 'html',
            data: {action: 'buscaRaca', idAnimal: idAnimal},
            success: function (data) {
                $('#raca').empty().append(data);
            }
        });

    }


//

I wanted to remind you that I am using the same codes for the 4 pages, can this give anything? or should popular select in the 4 ?

  • What error are you experiencing? Any Exception in PHP?

  • So he doesn’t show me any mistakes, just doesn’t give me the result in select, to understand well what I’m doing, I’m populating a select Type of Animal and Appear Breeds of these animals, 1 View he showed right, in the other 3 did not show

  • Have you tried adding the error() function there in your ajax? Try adding the command: console.log(date) at the beginning of the Javascript Success() function.

  • He gave me this error here (searchRacas is not defined at Htmlselectelement.onchange)

  • But I believe it’s not, because that mistake would give the same thing in the first one that worked.

  • This is indicative of the fact that you are not finding the javascript function buscarRacas when running the onChange() event of your select. Please post the complete code of your javascript so we can analyze the error better.

  • But how in one he executes and another he does not execute? Come on, I gave a copy and paste so that they were the same selects in the 4 pages, in 1 only worked, because in the others they would not be right? some query is not hitting?

  • Unfortunately, I can’t say exactly what the problem is. But it is very likely that the error is at another point in the code and not in the generation of Select itself.

  • Do you want to take a look at the code? could you pass me your email if possible? if it’s no bother, I just need to finish these 4 selects and I’m struggling for days

  • the most ideal would be for you to post as little code as possible to identify the error. If this is not possible, I can only tell you the general error handling: put console.log() throughout the application until you find the error. But from what you said, I’m pretty sure the error is some javascript function that you put in the onChange() event of this SELECT.

  • Well I have it here $query = "select raca_id 'idRaca', raca 'raca' from raca_nimal Where id_nimal = " . $idAnimal. " order by raca;"; that would be the select

  • Select I speak is HTML. Your PHP code seems to be all right. Your problem is Javascript code.

  • I sent a reply with the Code that is right, in which I copied and pasted only that final part until I got Javascript

  • $(Document). ready(Function () { if ($('#typo_nimal').val() != "") { searchRacas(); } }); Function buscarRacas() { var idAnimal = $('#typo_nimal'). val(); $.ajax({ url: './Controller/Doacaocontroller.php', type: 'POST', dataType: 'html', date: {action: 'searchRaca', idAnimal: idAnimal}, Success: Function (date) { $('#raca').Empty().append(data); } }); } (The script is pulling from the controller)

  • I tried to change the syntax and it didn’t work either

Show 10 more comments
No answers

Browser other questions tagged

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