[英]Toggle display / hide and Button to new page to show certain field of record in php
I wish not to display all fields of records from query. 我希望不显示查询记录的所有字段。 I wish either a button in the query records to a new page to display the field. 我希望查询中的一个按钮记录到新页面以显示该字段。 And a button toggle display a field. 和按钮切换显示一个字段。
I can't figure out the action code for the button to new page. 我无法弄清楚新页面按钮的操作代码。 And I can only toggle display / hide only the first record. 而且我只能切换显示/隐藏第一条记录。
After reading some answers here, I think the problem should be the id of the record, which my code dont assign a unique id for each record. 在这里阅读了一些答案之后,我认为问题应该是记录的ID,我的代码没有为每个记录分配唯一的ID。 Seems I can't figure how to solve it. 似乎我不知道如何解决。 As for the button to new page, I stuck. 至于新页面的按钮,我卡住了。
Below is to button to new page to echo $row['contact']
下面是到新页面的按钮,以echo $row['contact']
<?php
$data = mysqli_query($conn, $query) or die('error');
if(mysqli_num_rows($data) > 0){
while($row = mysqli_fetch_assoc($data)){
$status = $row['status'];
$area = $row['area'];
$address = $row['address'];
?>
<tr>
<td><form method="post" name="dataid" value="'.$row->new_id.'"><input
type="submit" name="submit1"></form></td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['area'];?></td>
<td><?php echo $row['address'];?></td>
</tr>
Below is meant to toggle display / hide $row['contact']
下面的意思是切换显示/隐藏$row['contact']
<?php
$data = mysqli_query($conn, $query) or die('error');
if(mysqli_num_rows($data) > 0){
while($row = mysqli_fetch_assoc($data)){
$status = $row['status'];
$area = $row['area'];
$address = $row['address'];
?>
<tr>
<td>
<div id=1 onclick="document.getElementById('body_1').style.display=
(document.getElementById('body_1').style.display=='none'? 'block':'none')">Click</div>
<div id="body_1" style="display:none; border:2px solid green;"><?php echo $row['contact'];?></div>
</td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['area'];?></td>
<td><?php echo $row['address'];?></td>
</tr>
My button not working yet. 我的按钮尚无法使用。 And only toggle display / hide only the first record 并且仅切换显示/仅隐藏第一条记录
I try this code to button / link to certain field of my record : 我尝试使用此代码来按钮/链接到我的记录的某些字段:
a href="detail.php?new_id=">Click a href =“ detail.php?new_id =”>点击
In detail.php : 在detail.php中:
<?php
if(isset($_GET['new_id']) && $_GET['new_id'] !== ''){
$new_id = $_GET['new_id'];
echo $new_id;
echo $contact;
} else {
echo "failed";
}
?>
Result : I can display the $new_id, but unable to display $contact
There are two reasons why this code isn't working for multiple div
elements with the id
"body_1": 此代码不适用于id
“ body_1”的多个div
元素的原因有两个:
An id
in HTML is unique. HTML中的id
是唯一的。 Which means, you may only have one HTML element with that specific id
in your DOM
. 这意味着,您的DOM
可能只有一个具有特定id
HTML元素。
The JS selector getElementById()
will only return one element with the specified id
. JS选择器getElementById()
将仅返回一个具有指定id
元素。 That is because an id
is supposed to be unique. 那是因为id
应该是唯一的。
If your $row
array contains an id element (if you have a column in your database table with an unique identifier (id)) you could use this value to give your HTML elements
its unique ID. 如果$row
数组包含id元素(如果数据库表中的列具有唯一标识符(id)),则可以使用此值为HTML elements
赋予其唯一ID。
I am personally not a huge fan of using in-line JS code like this. 我个人并不喜欢使用这样的内联JS代码。 I prefer to keep it in a separate .js
file for readability and caching purposes, but the following code may help you out if your rows contain an id which can be accessed by $row['id']
: 我倾向于将其保存在单独的.js
文件中,以提高可读性和缓存目的,但是如果您的行包含可以由$row['id']
访问的ID,则以下代码可以为您提供帮助:
$data = mysqli_query($conn, $query) or die('error');
if (mysqli_num_rows($data) > 0) {
while ($row = mysqli_fetch_assoc($data)) {
$status = $row['status'];
$area = $row['area'];
$address = $row['address'];
?>
<tr>
<td>
<div onclick="document.getElementById('<?= 'body_' . $row['id'] ?>').style.display=
(document.getElementById('<?= 'body_' . $row['id'] ?>').style.display==='none'? 'block':'none')">Click
</div>
<div id="<?= 'body_' . $row['id'] ?>" style="display:none; border:2px solid green;"><?php echo $row['contact']; ?></div>
</td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['area']; ?></td>
<td><?php echo $row['address']; ?></td>
</tr>
<?= 'text' ?>
is a shorthand for <?php echo 'text' ?>
. <?= 'text' ?>
是<?php echo 'text' ?>
echo'text <?= 'text' ?>
的简写。 I have also used the the triple equals ===
instead of ==
to compare if the style property display
has 'none' as its value because of Test Equality 我还使用了三等号===
而不是==
来比较样式属性display
是否由于测试平等而将其值设置为“ none”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.