0

Friends be patient with me. I'm not even a newbie. I'm developping a website that accesses MySQL database and I'm using this code:

File getdata.php

<?PHP
require_once("php/simfatic-RegistrationForm-dc288cf/source/include/membersite_config2.php");
$mycon = mysqli_connect("localhost", "dbuser", "ammr4024", "gadgetbid") or die("Erro! " . mysqli_error($mycon));
$myque = "SELECT * FROM leilaov WHERE numero12345 = 1";
$resul = $mycon->query($myque);
if(!$resul) { 
    die("Erro no query!" . $fgmembersite->UserId()); 
}
$myrow = mysqli_fetch_array($resul);
$variarr = array('0' => $myrow['fotodet'], '1' => $myrow['anoini'], '2' => $myrow['mesini'], '3' => $myrow['diaini'], '4' => $myrow['horaini'], '5' => $myrow['minutoini']);
echo json_encode($variarr);
?>

File getdatamain.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function reqListener () {
    console.log(this.responseText);
}
function teste() {
    var oReq = new XMLHttpRequest(); 
    oReq.onload = function() {
        var variarr = JSON.parse(this.responseText);
        alert(variarr[0]+" em "+variarr[1]+"-"+variarr[2]+"-"+variarr[3]+" às "+variarr[4]+":"+variarr[5]);
    };
    oReq.open("GET", "getdata.php", true);
    oReq.send();
}
</script>
</head>
<body>
Botanito
<input type="submit" name="botao" id="botao" value="Largar bosta" onclick="teste()" />
</body>
</html>

This works perfect! Clicking the button retrieves the information I want from MySQL table. My question is:- Is it possible to pass a variable to getdata.php? As you can see in this file I have

$myque = "SELECT * FROM leilaov WHERE numero12345 = 1";

Well, I want to pass a variable and use it in this "SELECT" (instead of "1")

Here's some of my trials that didn't work.

var indice = 1;
oReq.open("POST", "getdata.php", true);
oReq.send(indice);


var indice = {
    object: { child:1 }
}
oReq.open("POST", "getdata.php", true);
oReq.send(indice);

When I print the $POST the result is always the same

Array
(
)

Can anybody help me?

Zidrep
  • 57
  • 1
  • 6
  • 1
    You misunderstand how servers work - when you send your post the page processes it instantly - so you cant print_r the $_POST without returning it back to the current page. - you need to do something with the output on the current page - https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest is a good starting place - or try jquery $.AJAX – GrahamTheDev Feb 18 '15 at 15:56
  • 1
    Please also have a look at this http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php before moving on to plugging your POST variables in to your queries. – Dan Smith Feb 18 '15 at 15:57
  • Thanks guys but as I told before, I'm less than a newbie and I can't get an answer to my question in all the information you sent me to read. It's really not your problem, it's mine, but thanks anyway. – Zidrep Feb 18 '15 at 16:42

0 Answers0