Nothing happens when calling .php file via Ajax call in pure Javascript

0

I'm creating persistence on MySql server for my own web application.

For this I am using xampp and phpmyadmin. I created a database in phpMyAdmin and in my application, in a javascript I have an Ajax request for a .php file that makes this communication with the server.

What happens is that the AjaxCall function in my .js is called, but the Php file passed to it with an action is simply ignored. Nothing happens to php. I put echos in .php and nothing appears. I put syntax errors and the browser did not even accuse. It means that the .php is not being accessed ... But I do not know why.

My Js:

function abreConta(){
    var cpf = document.getElementById('cpf').value;
    var agencia = document.getElementById('agencia').value;
    var conta = document.getElementById('conta').value;

    //As variáveis acima são preenchidas corretamente.

    parms = "&cpf"+cpf+"&agencia"+agencia+"&conta"+conta;
    ajaxCall("Persistencia.php?action=abreConta" +parms, mudaView);


}

function mudaView(listaParams){
    window.alert("Sua conta foi aberta com sucesso. Você será redirecionado ao menu inicial");
    mudaMenu("formularioDeAbertura", "menu1");
}


function ajaxCall(stringCall, callback){
    var httpRequest = new XMLHttpRequest;

    httpRequest.onreadystatechange = function(){
    if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
            console.log("Requisicao http em curso");
          callback(httpRequest.responseText);
        }
    }
    };
    httpRequest.open('GET', stringCall);
    httpRequest.send();
}

My application folder:

MyPersistence.php:

<?phpfunctionconectaDB(){$con=mysqli_connect("localhost","root","","onbank");

    if(!$con){
        echo "<h2>Erro na conexao com a base dados...</h2>"; 
        echo "<h2> Erro " . mysqli_connect_errno() . ".</h2>";
        die();
    }
    $con->set_charset("utf8");
    return $con;
}


    if(@$_REQUEST['action'] == "abreConta")     //recupera lista de nomes das cidades
    {

        con = ConectaDB();
        $cpf = $con->real_escape_string($REQUEST['cpf']);
        $agencia = $con->real_escape_string($REQUEST['agencia']);
        $conta = $con->real_escape_string($REQUEST['conta']);

        mysqli_query($con,"INSERT INTO contas (agencia,conta,dono) VALUES('$agencia','$conta', '$cpf');");]
        $con->close();

        //abreConta();
    }


    if(@_REQUEST['action'] == 'buscaContasCliente')
    {
        $cpf = buscaCpf();
        inicializaListaContas($cpf);

    }

    function buscaCpf(){
        $con = ConectaDB();
        $nome = $con->real_escape_string($REQUEST['nome']);

        $result = mysqli_query($con, "SELECT cpf FROM clientes WHERE nome='$nome';");
        return $result;
    }

? >

My database in phpMyAdmin: Not being changed, due to Persistence.php not being executed.

Note:thebalancecolumnhasautocompletewith0.

IntheNetworktaboff12thefollowingappearswhenIopenanaccount:

    
asked by anonymous 28.05.2018 / 23:27

2 answers

1

Have you checked what's popping up on the broswer network? to verify just need to click f12 and check the network tab. Then perform the ajax request. Should a new field appear, it will probably appear in the PHP code.

Or you can also go directly through the broswer and check for any errors. In that case it would be: link . The errors that are being returned should appear better.

I also recommend changing the form of the request, today in javascript we use a more simplified way to do Ajax on the javascript side, see an example:

<script>

function abreConta(){

    let cpf = "cpf qualquer";
    let agencia = "agencia qualuqer";
    let conta = "conta qualquer";

    let params = "?cpf=" + cpf + "&agencia=" + agencia + "&conta=" + conta;

    ajaxCall(params, function(result) {
        console.log(result);
    });

}

function ajaxCall(params, callback){
    // Faz a requisicao HTTP para o servidor PHP
    fetch("ajax.php" + params)
        .then((response) => {
            alert("-- sucesso --");
        }).catch(() => alert("error"));
}

abreConta();

</script>

This is PHP that is testing this Javascript, I did it as a demonstration:

<?php
$dados = [$_GET["cpf"], $_GET["conta"], $_GET["agencia"]];
echo json_encode($dados);

When you find out what error you are giving in PHP you can update the question if you can not resolve it.

    
29.05.2018 / 02:25
1

The error is in this line:

parms = "&cpf"+cpf+"&agencia"+agencia+"&conta"+conta;

Signals of equal = are missing in the parameters:

             ↓               ↓                 ↓
parms = "&cpf="+cpf+"&agencia="+agencia+"&conta="+conta;

Without = , PHP is not getting the values.

    
29.05.2018 / 03:18