I have a registration page with a form, the person types the year and clicks the button to search the register. With the typed year I have to make the request for PHP to execute the query, return the data and distribute the data to the fields of the form.
I'm trying to make a simple request. The page takes the base year of the form and sends a request with the base year returning the result of the query within PHP. I have no experience with HTML, PHP or javascript and after weeks studying how to do this I got here. The request executes and returns, but the result is not what I want. Using $ .post the result that exits in alert
(I'm printing the result to see if it is returning what I want, I'll handle the query data by distributing them to each form field) is the PHP code of the query. php instead of executing and returning the query. I tried using $ .ajax and the result was even more bizarre because it not only executes query.php, but returns the HTML code of the page itself. I tried doing using XMLHttpRequest () and also returns the code instead of executing. I do not know what I'm doing wrong. Help ...
Edited: I built the code with $_POST
and tag <?php
and the request worked. The problem now is simply to handle the data. I do not know how to return or how to treat. In short: I have the request that makes the query in the DB and returns to the page. The request does not return (or does not execute, I do not know). alert
of $.post
does not execute when I use json_encode
. If I return text
it only returns array
(I saw the variable by F12 and the alert printa that too). What I need is that at the end of it all I have an array (or anything easy to separate) to assign each column value queried in the DB to the specific HTML field.
Using $ .post:
$.post("query.php", {ano_ref: ano_base},
function(retorno){
alert(retorno);
}, "json");
With $ .ajax it looks like this:
$.ajax({
type: "POST",
URL: "query.php",
datatype: "json",
data:{ano_ref: ano_base},
success: function (resposta){
alert(resposta); },
error: function(){
alert("erro de req.");
}
})
Using XMLHttprequest:
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("POST", "query.php", true);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var string = "ano_ref=" + encodeURIComponent(ano_base.toString());
alert(string);
xhttp.send(string);
The query.php:
<?php
include("conexao_postgres.php");
$ano_base = $_POST["ano_ref"];
$sql = "SELECT * FROM public.parametros_estaduais WHERE ano_base = $ano_base";
$resultado = pg_query($dbconn, $sql);
$resultado_array = pg_fetch_row($resultado, 0); // ($resultado)
echo json_encode($resultado);
?>
Enjoying, can I return a single array or just json
same? Is json
too hard to handle? Like I said, each of these query columns goes to a form field, that's all.