将json var传递给php

时间:2015-02-18 15:50:34

标签: javascript php json

朋友们对我很耐心。我甚至不是新手。我正在开发一个访问MySQL数据库的网站,我使用这个代码:

文件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);
?>

文件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>

这很完美!单击该按钮可从MySQL表中检索我想要的信息。 我的问题是: - 是否可以将变量传递给getdata.php? 正如您在此文件中所见,我有

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

好吧,我想传递一个变量并在这个&#34; SELECT&#34;中使用它。 (而不是&#34; 1&#34;)

这里有一些我的试验没有用。

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);

当我打印$ POST时,结果始终是相同的

Array
(
)

有人能帮助我吗?

0 个答案:

没有答案