gpt4 book ai didi

javascript - 使用 SlideToggle 按钮显示/隐藏信息

转载 作者:行者123 更新时间:2023-11-29 17:19:31 26 4
gpt4 key购买 nike

目的 将name(input-text)和info(textarea-包含多行)插入数据库,提交表单后,在同一页面,2列用于显示相同的内容列、名称和信息中的数据,但位于信息列下。我为名称前面的每一行制作了按钮,用作 slideToggle 来显示/隐藏其中包含从“info”列检索的数据

问题 - 当我单击第一行的按钮时,它不是仅显示与第一个条目相关的信息,而是滑动并仅在单击时显示与所有条目相关的所有信息.

*其他 - 表单中又添加了一个输入,但隐藏用于 id(自动增量)

screenshot with the toggle problem

----index.php-----

<?php include('php_code.php'); ?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM records WHERE id=$id");

if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$acc = $n['acc_no'];
$info = $n['info'];
}
}
?>
<html>
<head>
<title>JSK</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').hide();
$('p').hide();
$('#sp').hide();
$("#inf").click(function(){
$("p").slideToggle();
$('#sp').slideToggle();
});
$("#fo").click(function(){
$("form").slideToggle();
});
});
</script>
</head>
<body>
<div class="container">
<div class="left">
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>

<?php $results = mysqli_query($db, "SELECT * FROM records"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Account No</th>
<th>Info</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['acc_no']; ?></td>
<td><button id="inf" onclick="myFunction()">show</button></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
</td>
</tr>
<tr id="sp"> <td colspan="4"><p> <?php echo $row['info']; ?> </p></td>
</tr>
<?php } ?>
</table>
<div id="fotable" align="center">
<button id="fo">Add New/Edit</button>
</div>

<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">

<div class="input-group">
<label>Name</label>
<input type="text" autocomplete="off" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Account No</label>
<input type="text" name="acc" value="<?php echo $acc; ?>">
</div>
<div class="input-group">
<label for="info">Info</label>

<textarea class="form-control" rows="8" name="info" id="info"><?php echo $row['info']; ?></textarea>

</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Add Account</button>
<?php endif ?>
</div>
</form>
</div><!-- left closed -->
<div class="right">

hello



</div> <!-- right closed -->
</div> <!-- container closed -->
</body>
</html>

---php_code.php-----

<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'jskrecords');


// initialize variables
$name = "";
$acc = "";
$info = "";
$id = 0;
$update = false;

if (isset($_POST['save'])) {
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];

mysqli_query($db, "INSERT INTO records (name, acc_no, info) VALUES ('$name', '$acc', '$info')");
$_SESSION['message'] = "Account saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];

mysqli_query($db, "UPDATE records SET name='$name',acc_no='$acc',info='$info' WHERE id=$id");
$_SESSION['message'] = "Account updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM records WHERE id=$id");
$_SESSION['message'] = "ACC deleted!";
header('location: index.php');
}

?>

最佳答案

概念:

如果您使用 php 脚本从同一个 mysql 表创建多个表单,您需要为每个表单指定一个唯一的 ID。例如

<form method="post" action="php_code.php" id="form<?= $id ?>">

然后将 data-target="#form"添加到类为 'inf' 的按钮。它将存储表单的 id。

<button class="inf" data-target="#form<?= $id ?>">show</button>

单击按钮时,我们知道要从数据目标打开哪个表单。

<script>
$('.container').on('click','button.inf',function(e){
e.preventDefault();
var formid=$(this).attr('data-target'); //get value of data-target attribute
//...proceed to play toggle with this form 'formid'

关于javascript - 使用 SlideToggle 按钮显示/隐藏信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com