我的网页显示的名称始终相同 我需要在我点击的模态中显示名称 这是我的代码:
如果单击图片,则显示内容(存储在mysql中)。
<?php
$query = "SELECT * FROM pet where pet_cat = 'D' ORDER BY petid ";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['petname'] = $row['petname'];
$_SESSION['petdesc'] = $row['petdesc'];
$_SESSION['petimg'] = $row['petimg'];
$_SESSION['petid'] = $row['petid'];
echo '<li style="
padding-right: 20px;
padding-left: 20px;">';
echo '<a style= "cursor: pointer;"onclick= "document.getElementById(\'dogmod'.$row['petid'].'\').style.display=\'block\'">';
echo '<img src="data:image/jpeg;base64,'.base64_encode($_SESSION['petimg'] ).'" />';
echo '<h4>';
echo $_SESSION['petname'] ;
echo '</h4>';
include 'desca.php';
echo '</a>';
echo '</li>';
}
?>
我的模态代码 (desca.php)
<div id="dogmod<?=$_SESSION['petid']?>" class="modal">
<center>
<form class="modal-content animate" style="margin: 0; padding-left:50px; padding-right:50px; padding-bottom:50px;">
<div class="imgcontainer">
<span onclick="document.getElementById('dogmod<?= $_SESSION['petid'] ?>').style.display='none'" class="close" title="Close">×</span>
<h1 align=center>Description</h1>
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($_SESSION['petimg'] ).'" style="margin-top:50px;float:left; margin-right:50px;"/>'; ?>
<h1 style="margin-top:50px;margin-bottom:50px">
<?php echo $_SESSION['petname'] ;?>
</h1>
<p style="margin-bottom:50px;">
<?php echo $_SESSION['petdesc'] ;?>
</p>
<input type="button" value="Back" onclick=location.href='doga.php' class="button_1" style=" width: auto;padding: 10px 18px; background-color: #f44336; border:0px; color:white;">
<input type="button" value="Adopt" onclick=location.href='form.php' class="button2"style= " width: auto;padding: 10px 18px; background-color: #4CAF50; border:0px; color:white; ">
</form>
</center>
<script>
// Get the modal
var modal = document.getElementById('dogmod');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</div>
这是我要显示名称的表单。
<div style="
height: 70px;
">
<label>Name:</label>
<input type="text" style=" width: 100%;
padding: 0px 10px;
width:323px;
text-align:center;
box-sizing: border-box;
border: none;
border-bottom: 1px solid;" placeholder="Name" value="<?=$_SESSION['fullname']?>" name="fname">
<label>Age:</label>
<input type="text"style=" width: 100%;
padding: 0px 20px;
width:100px;
text-align:center;
box-sizing: border-box;
border: none;
border-bottom: 1px solid;" placeholder="Age" name= "age">
<label>Email:</label>
<input type="text" style=" width: 100%;
padding: 0px 20px;
text-align:center;
width:298px;
box-sizing: border-box;
border: none;
border-bottom: 1px solid ;" value = "<?=$_SESSION['email']?>" placeholder="Email Address" name="email">
</div>
<div style="
height: 70px;
">
<label>Address:</label>
<input type="text" style=" width: 100%;
padding: 0px 20px;
width:795px;
text-align:center;
box-sizing: border-box;
border: none;
border-bottom: 1px solid;" placeholder="Address" name="adrs">
</div >
<div style="
height: 70px;
">
<label>Mobile Number:</label>
<input type="text" style=" width: 100%;
padding: 0px 20px;
text-align:center;
width:300px;
box-sizing: border-box;
border: none;
border-bottom: 1px solid ;" value = "<?=$_SESSION['contactnum']?>" placeholder="Contact Number" name="mnum"><br>
</div>
<div style="
height: 70px;
">
<label>Name/Description of animal:</label>
<input type="text" style=" width: 100%;
padding: 0px 20px;
text-align:center;
width:220px;
box-sizing: border-box;
border: none;
border-bottom: 1px solid ;" value = "<?=$_SESSION['petname']?>" placeholder="petname" name="petname">
</div>
()我使用此代码显示模式中名称相同的名称,但不幸的是,这将显示相同的错误名称。
答案 0 :(得分:0)
问题是在你的循环中你覆盖$_SESSION["petname"]
(以及其他一些值),所以它总是只存储最后一个petname。如果我明白你想要下一个:用户点击采用按钮后,他被重定向到带有表单的页面。在那种形式,你想看到正确的宠物名称。这是一个可能的解决方案:
修改采用 - 按钮,设置:
onclick=location.href='form.php?petname=<?=$_SESSION['petname']; ?>'
在使用petname的模态修改输入中,设置:
value = "<?=$_GET['petname']?>"
我希望这会奏效。但你应该考虑改进你的算法。您并不需要$_SESSION
在此处存储宠物信息。此外,也许您想将这些模态保留在同一页面上并用js填充。
答案 1 :(得分:0)
&#34;问题是在你的循环中你覆盖$ _SESSION [&#34; petname&#34;] (以及其他一些价值观)&#34;
我建议你应该使用AJAX,就像这样!!
<?php
$query = "SELECT * FROM pet where pet_cat = 'D' ORDER BY petid ";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$petname = $row['petname'];
$petdesc = $row['petdesc'];
$petimg = $row['petimg'];
$petid = $row['petid'];
//echo '<li style="
//padding-right: 20px;
//padding-left: 20px;">';
// echo "<a data-toggle="modal" data-target="#modalForm" data-id=" $petid;" id="formPet"></a> ";
echo '<img src="data:image/jpeg;base64,'.base64_encode($petimg).'" />';
// echo '<h4>';
// echo $_SESSION['petname'] ;
// echo '</h4>';
//include 'desca.php';
//echo '</a>';
//echo '</li>';
?>
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modalForm" data-id="<?php echo $petid; ?>" id="formPet">FormOpen</button>
<?php
}
?>
//按钮单击ajax脚本
<script>
$(document).ready(function() {
$(document).on('click', '#formPet', function(e) {
e.preventDefault();
var uid = $(this).data('id'); // it will get id of clicked row
$('#form-content').html(''); // leave it blank before ajax call
$('#formmodal-loader').show(); // load ajax loader
$.ajax({
url: 'form.php',
type: 'POST',
data: 'id=' + uid,
dataType: 'html'
})
.done(function(data) {
console.log(data);
$('#form-content').html('');
$('#form-content').html(data); // load response
$('#formmodal-loader').hide(); // hide ajax loader
})
.fail(function() {
$('#form-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
$('#formmodal-loader').hide();
});
});
});
</script>
//一旦你点击while循环中的至少一个按钮
,这个模态就会执行<div class="modal fade" id="modalForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h5 class="modal-title w-100 font-bold">PET MODAL</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body mx-3">
<div id="formmodal-loader" style="display: none; text-align: center;">
<img src="img/ajax-loader.gif">
</div>
<!-- content will be load here -->
<div id="form-content"></div>
</div>
</div>
</div>
</div>
//创建一个文件名form.php并从你的ajax帖子中获取所请求的id
<?php
require_once('dbconfig.php');
if (isset($_REQUEST['id']) ){
$pet_id = $_REQUEST['id'];
//create a form or view content here base on the id of pet you want to add/edit/delete/view
}