我在数据库中添加数据,同时当单击添加按钮时,它通过 ajax 显示到同一页面。现在我想以升序和降序显示数据。添加单击时显示数据,但是当我单击升序按钮时,数据未显示。降序按钮也是一样。
添加.php文件
<section class="my-5">
<div class="py-5">
<h2 class="text-center">Information</h2>
</div>
<div class="w-50 m-auto">
<form method="POST" onsubmit="event.preventDefault();onSubmit();">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" id="title" autocomplete="off" class="form-control" required="">
</div>
<div class="form-group">
<label>Description</label>
<input type="text" name="description" id="description" autocomplete="off" class="form-control" required="">
</div>
<br>
<button type="submit" class="btn btn-success" id="fetch" name="dataadd" onclick="addRecord();">Add</button>
</div>
</form>
</div>
</section>
<section align="center">
<form>
<button type="submit" class="btn btn-success" name="ascd" onclick="ascdRecord();">Ascending</button>
<button type="submit" class="btn btn-success" name="desc" onclick="descRecord();">Descending</button>
</form>
</section>
<br>
<br>
<div align="center">
<h2 id="records">Show All Data</h2>
</div>
</div>
JavaScript 函数
<script type="text/javascript">
function addRecord()
{
var title = $('#title').val();
var description = $('#description').val();
$.ajax({
url: "data.php",
type: "POST",
data: {
title: title,
description: description
},
success: function(data, status)
{
readRecords();
}
});
}
function readRecords()
{
var readrecord = "readrecord";
$.ajax({
url: "data.php",
type: "POST",
data: {readrecord: readrecord},
success: function(data, status)
{
$('#records').html(data);
}
});
}
function DeleteUser(deleteid)
{
var conf = confirm("you want to delete ?");
if (conf==true) {
$.ajax({
url: "data.php",
type: "POST",
data: {deleteid: deleteid},
success: function(data, status)
{
readRecords();
}
});
}
}
function ascdRecord()
{
$("#ascd").click(function(){
$.ajax({
url: "data.php",
type: "POST",
dataType: "html",
success: function(data)
{
$('#records').html(data);
}
});
});
}
</script>
数据.php文件
if(isset($_POST['ascd']))
{
echo "<div class='result'>";
$data = '<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
';
$fetch_query = "select * from app.add order by title";
$fetch_run = mysqli_query($con, $fetch_query);
if(mysqli_num_rows($fetch_run) > 0)
{
while($row = mysqli_fetch_array($fetch_run))
{
$data .= '<tr style="border: 1px solid black; padding: 8px; text-align: center">
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['title'].'</td>
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['description'].'</td>
<td>
<button onclick="DeleteUser('.$row['id'].')">Delete</button>
</td>
</tr>';
}
}
$data .= '</table>';
echo $data;
echo "</div>";
}
if(isset($_POST['readrecord']))
{
echo "<div class='result'>";
$data = '<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
';
$fetch_query = "select * from app.add";
$fetch_run = mysqli_query($con, $fetch_query);
if(mysqli_num_rows($fetch_run) > 0)
{
while($row = mysqli_fetch_array($fetch_run))
{
$data .= '<tr style="border: 1px solid black; padding: 8px; text-align: center">
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['title'].'</td>
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['description'].'</td>
<td>
<button onclick="DeleteUser('.$row['id'].')">Delete</button>
</td>
</tr>';
}
}
$data .= '</table>';
echo $data;
echo "</div>";
}
if(isset($_POST['title']) && isset($_POST['description']))
{
$insert_query = "insert into app.add (title, description) values('$title','$description')";
$query_run = mysqli_query($con, $insert_query);
}
您可以修改每个函数以发送一个新参数 - 我选择了task
但它并不重要。正如您将在 javascript 中看到的那样,每个请求类型的值都会更改。PHP 代码可以根据这个单一的 POST 变量查找然后派生逻辑。
仅供参考:盲目地提取 PHP 中的变量使用extract
打开了各种攻击的大门 - 只有在清理 POST 数组后才谨慎使用它 - 可能filter_input_array
用作可接受名称/类型的开始和白名单。
function addRecord(){
var title = $('#title').val();
var description = $('#description').val();
$.ajax({
url: "data.php",
type: "POST",
data: {
task:'add',
title:title,
description:description
},
success: function(data, status)
{
readRecords();
}
});
}
function readRecords(){
$.ajax({
url: "data.php",
type: "POST",
data: {
task:'read'
},
success: function(data, status)
{
$('#records').html(data);
}
});
}
function DeleteUser(deleteid){
if( !confirm("you want to delete?") )return false;
$.ajax({
url: "data.php",
type: "POST",
data: {
task:'delete',
deleteid:deleteid
},
success: function(data, status)
{
readRecords();
}
});
}
function ascdRecord(e){
$.ajax({
url: "data.php",
type: "POST",
data:{
task:'ascd'
},
dataType: "html",
success: function(data)
{
$('#records').html(data);
}
});
}
/* presume similar for dscdRecord */
function descRecord(e){
$.ajax({
url: "data.php",
type: "POST",
data:{
task:'dscd'
},
dataType: "html",
success: function(data)
{
$('#records').html(data);
}
});
}
数据.php
if( isset( $_POST['task'] ) ){
switch( $_POST['task'] ){
case 'add':
if(isset(
$_POST['title'],
$_POST['description']
)){
# this is prone to SQL injection - you should use a `Prepared statement`
/*
$insert_query = "insert into app.add () values('$title','$description')";
$query_run = mysqli_query($con, $insert_query);
*/
$sql='insert into `app`.`add` ( `title`, `description` ) values (?,?)';
$stmt=$con->prepare($sql);
$stmt->bind_param('ss',$_POST['title'],$_POST['description']);
$stmt->execute();
$stmt->close();
}
break;
case 'delete':
break;
case 'read':
$data = '
<div class="result">
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>';
$fetch_query = "select * from app.add";
$fetch_run = mysqli_query($con, $fetch_query);
if(mysqli_num_rows($fetch_run) > 0)
{
while($row = mysqli_fetch_array($fetch_run))
{
$data .= '
<tr style="border: 1px solid black; padding: 8px; text-align: center">
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['title'].'</td>
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['description'].'</td>
<td>
<button onclick="DeleteUser('.$row['id'].')">Delete</button>
</td>
</tr>';
}
}
$data .= '
</table>
</div>';
header('Content-Type: text/html');
exit( $data );
break;
case 'ascd':
$data = '
<div class="result">
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>';
$fetch_query = "select * from app.add order by title";
$fetch_run = mysqli_query($con, $fetch_query);
if(mysqli_num_rows($fetch_run) > 0)
{
while($row = mysqli_fetch_array($fetch_run))
{
$data .= '
<tr style="border: 1px solid black; padding: 8px; text-align: center">
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['title'].'</td>
<td style="border: 1px solid black; padding: 8px; text-align: center">'.$row['description'].'</td>
<td>
<button onclick="DeleteUser('.$row['id'].')">Delete</button>
</td>
</tr>';
}
}
$data .= '
</table>
</div>';
header('Content-Type: text/html');
exit($data);
break;
case 'dscd':
break;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句