我正在尝试将数据库中的数据动态加载到表中,但这给我一个错误:
未定义索引:/xxx/xxx/xxx/search.php中第26行的条目
此外,通过登录浏览器控制台,我的选择无法识别onChange事件。
我以这个家庭教师为基础来实现自己的愿望:https://www.phpzag.com/ajax-drop-down-selection-data-load-with-php-mysql/
<?php
require('config/config.php');
if(isset($_SESSION['flash']))
{
foreach($_SESSION['flash'] as $type => $message)
{
echo $message;
}
unset($_SESSION['flash']);
}
$output = '';
if(isset($_POST["query"]))
{
$query = $cnx->prepare("SELECT * FROM candidacies WHERE lastName LIKE :lastName OR firstName LIKE :firstName OR age LIKE :age OR scheduleRange LIKE :scheduleRange OR phoneNumber LIKE :phoneNumber OR email LIKE :email OR candidacyType LIKE :candidacyType");
$element = "%".$_POST['query']."%";
$query->bindValue(':lastName', $element, PDO::PARAM_STR);
$query->bindValue(':firstName', $element, PDO::PARAM_STR);
$query->bindValue(':age', $element, PDO::PARAM_INT);
$query->bindValue(':scheduleRange', $element, PDO::PARAM_STR);
$query->bindValue(':phoneNumber', $element, PDO::PARAM_STR);
$query->bindValue(':email', $element, PDO::PARAM_STR);
$query->bindValue(':candidacyType', $element, PDO::PARAM_STR);
}
else if(isset($_POST['entries']))
{
$entries = intval($_POST['entries']);
$query = $cnx->prepare("SELECT * FROM candidacies LIMIT :entries");
$query->bindValue(':entries', $entries, PDO::PARAM_INT);
}
else
{
$limit_per_page = 10;
$page = (!empty($_GET['page']) ? $_GET['page'] : 1);
$start = ($page - 1) * $limit_per_page;
$query = $cnx->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM candidacies LIMIT :limit_per_page OFFSET :start");
$query->bindValue(':limit_per_page', $limit_per_page, PDO::PARAM_INT);
$query->bindValue(':start', $start, PDO::PARAM_INT);
//$query = $cnx->prepare('SELECT * FROM candidacies');
}
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
$count = $query->rowCount();
$resultFoundRows = $cnx->prepare('SELECT found_rows()');
$resultFoundRows->execute();
$listElements = $resultFoundRows->fetchColumn();
if($count > 0)
{
$output .= '
<select name="entriesData" class="entriesData">
<option value="2">2</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
';
$output .= '
<table class="table table-striped" id="candidaciesTable">
<thead class="thead-light">
<tr>
<th scope="col">firstName</th>
<th scope="col">lastName</th>
<th scope="col">Âge</th>
<th scope="col">Schedule Range</th>
<th scope="col">Phone Number</th>
<th scope="col">Email</th>
<th scope="col">Candidacy Type</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
';
foreach($result as $key => $value)
{
$output .= "
<tr>
<td>".htmlspecialchars($value["firstName"])."</td>
<td>".htmlspecialchars($value["lastName"])."</td>
<td>".htmlspecialchars($value["age"])."</td>
<td>".htmlspecialchars($value["scheduleRange"])."</td>
<td>".htmlspecialchars($value["phoneNumber"])."</td>
<td>".htmlspecialchars($value["email"])."</td>
<td>".htmlspecialchars($search["candidacyType"])."</td>
<td>
<div class='btn-group'>
<button class='btn btn-light btnViewCandidacy' id=".$value['id'].">
<i class='far fa-eye'></i>
</button>
<button class='btn btn-info' id='btnEditCandidacy' onclick='editCandidacy(".$value['id'].")'>
<i class='far fa-edit'></i>
</button>
<button class='btn btn-danger' id='btnDeleteCandidacy' onclick='deleteCandidacy(".$value['id'].")'>
<i class='far fa-trash-alt'></i>
</button>
</div>
</td>
</tr>
";
}
$output .= "
</tbody>
</table>";
}
else
{
echo "No results found !";
}
?>
<script>
$(document).ready(function()
{
$('.entriesData').change(function()
{
var entriesData = $(this).find(":selected").val();
var entries = 'entries='+ entriesData;
$.ajax({
url: "search.php",
method:"POST",
data:entries,
success:function(data)
{
$('#result').html(data);
}
})
});
});
</script>
<div id="result"></div>