我有一个ajax代码,它为mysql查询传递一个变量。我需要传递另一个变量。我已经得到了这个变量,但我不知道如何将它添加到现有的代码中。
这是代码
function Docent(){
var opleidingid = $('#opleidingddl').val();
var datum = $('#datumddl :selected').text();
$('#docentddl').html();
$('#docentddl').html("<option>Loading.....</option>");
$.ajax({
type:"POST",
url:"Docent.php"
data :
{
'opleidingid': opleidingid,
'datum' : datum
},
success: function(data){
$('#docentddl').html();
$('#docentddl').html("<option value='0'>Selecteer docent</option>");
$.each(data,function(i,item){
$('#docentddl').append('<option value="'+ data[i].Opleiding_ID +'">'+ data[i].Docent+'</option>');
$('#docentddl').selectpicker('refresh');
});
},
complete: function(){
}
});
}
PHP
<?php
include ('config.php');
$opleidingid = $_POST['opleidingid'];
$datum = $_POST['datum'];
$sql=mysql_query("SELECT * FROM Docent_relatie WHERE Opleiding_ID = 'opleidingid'");
if(mysql_num_rows($sql)){
$data = array();
while($row=mysql_fetch_array($sql)){
$data[] = array(
'Opleiding_ID' => $row['Opleiding_ID'],
'Docent' => $row['Docent'],
'OpleidingDatum' => $row['OpleidingDatum']
);
}
header('Content-type: application/json');
echo json_encode($data);
}
?>
好的。所以我又回到了我的电脑上。它应该看起来像这样。一个重要的评论。您不想使用mysql弃用的方法。
您确实希望使用PDO而不是mysql或mysqli。
遵循此规则,始终:
PDO使用可以随意重复使用的准备好的统计数据。
1:你用什么来准备报表?每个值。
2:绑定值(按查询中的出现顺序,1,2,3……)
3:您执行语句。
4:您使用foreach循环遍历结果数组,而不是一段时间。
需要注意的是。在PHP中,遍历结果的foreach循环是一种冗余。你可以直接回显$result并用javascript处理它(在这种情况下,在查询中,不是选择*,而是选择Opleiding_ID、Docent、OpleidingDatum,但我不想"修改"你的逻辑。
Javascript:
function Docent() {
var opleidingid = $('#opleidingddl').val();
var datum = $('#datumddl :selected').text();
$('#docentddl').html("<option>Loading.....</option>");
var datas = {'opleidingid': opleidingid, 'datum': datum};
$.ajax({
cache: false,
type: "POST",
url: "Docent.php",
data: datas,
success: function (data) {
var result = $.parseJSON(data);
if (result.ctrl === true) {
$('#docentddl').html("<option value='0'>Selecteer docent</option>");
$.each(result.response, function (i, item) {
$('#docentddl').append('<option value="' + item.Opleiding_ID + '">' + item.Docent + '</option>');
});
$('#docentddl').selectpicker('refresh');
}else{
alert(result.response);// error message from php
}
}
});
}
PHP:
define("SQLHOST", "127.0.0.1");
define("SQLDB", "databasename");
define("SQLUSER", "login");
define("SQLPASS", "password");
try {
$con = new PDO('mysql:host=' . SQLHOST . ';dbname=' . SQLDB . ';charset=UTF8', SQLUSER, SQLPASS);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt= $con->prepare("SELECT * FROM Docent_relatie WHERE Opleiding_ID = ? AND datum= ?");
} catch (PDOException $e) {
echo json_encode(['ctrl' => false, 'response' => 'Connection failed to the database: ' . $e->getMessage()]);
}
$opleidingid = (isset($_POST['opleidingid'])) ? $_POST['opleidingid'] : null; // control if ajax sent proper value
$datum = (isset($_POST['datum'])) ? $_POST['datum'] : null; // control if ajax sent proper value
if ($opleidingid !== null && $datum !== null) {
$stmt->bindParam(1, $opleidingid, PDO::PARAM_INT); // could be PARAM_STR depending if $opleidingid is a int or a string
$stmt->bindParam(2, datum, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
$data = array();
if (count($result) !== 0) {
foreach ($result as $key => $row) {
$data[] = array(
'Opleiding_ID' => $row['Opleiding_ID'],
'Docent' => $row['Docent'],
'OpleidingDatum' => $row['OpleidingDatum']
);
}
echo json_encode(['ctrl' => true, 'response' => $data]);
}else{
echo json_encode(['ctrl' => false, 'response' => 'No results found']);
}
} else {
echo json_encode(['ctrl' => false, 'response' => 'ajax did not send values']);
}