gpt4 book ai didi

javascript - 在 .php 中制作 Textarea 和提交按钮以隐藏数据库表中的每一行?

转载 作者:行者123 更新时间:2023-11-29 20:25:57 25 4
gpt4 key购买 nike

已经研究这个很久了..想添加一个js效果(隐藏/取消隐藏)到一个简单的提交表单...

幸运的是它正在工作,但不幸的是它仅适用于数据库表中的单行。

我制作了 script:1(发布在下面),该网站的一位好心人已更正该脚本,即 script:2(发布在下面)。

不幸的是,两者都不起作用。希望你们帮我找到其中丢失的部分..

脚本#1

<!DOCTYPE html>
<html lang="en">
<head>
<?php
$servername = "localhost";
$password = "*****";
$dbname = "the_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<style>
.name {
width: 700px;
margin-top: 1%;
margin-left: 20%;
padding-left:2%;
padding-right:2%;
padding-top:10%;
margin-right: 35%;
word-wrap: break-word;
}
textarea#addtext{
width:600px;
height:100px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js">
</script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$().ready = function() {
$('#addtext').hide();
$('#addsubmit').hide();
$("#add").click(function() {
$('#add').hide();
$('#addtext').fadeIn('slow').focus();
$('#addsubmit').fadeIn('slow');
});

$('#addtext').blur(function(){
$('#addtext').hide();
$('#addsubmit').hide();
$('#add').fadeIn('slow');
});
}();
});//]]>

</script>

</head>
<body>

<div class="name">

<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { ?>

<div id="q">
<B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?> </B></br>
<B><small><font color= #ba4a00> Description:</font> <?php echo $row["description"]; ?> </small></B></br>
<p id="tag1"><small> <?php echo $row["subject"]; ?> </small></p><p id="tag2"><small> <?php echo $row["sub_subject"]; ?> </small></p>
<button class="addanswer" id="add"><B>Add Answer</B></button>
<form>
<textarea class="addtext" name="addtext" required id="addtext" placeholder="Please type your question here.."></textarea>
<button class="addanswer" id="addsubmit"><B>Submit</B></button>
</form>
<small><p><?php echo $row["date"]; ?></p></small>
</div>
<?php }
} else {
echo "0 results";
}
$conn->close();
?>
</div>

</body>
</html>

脚本#2

<!DOCTYPE html>
<html lang="en">
<head>
<?php
$servername = "localhost";
$username = "root";
$password = "******";
$dbname = "the_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<style>
.name {
width: 700px;
margin-top: 1%;
margin-left: 20%;
padding-left:2%;
padding-right:2%;
padding-top:10%;
margin-right: 35%;
word-wrap: break-word;
}
textarea#addtext{
width:600px;
height:100px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js">
</script>
<script type='text/javascript'>
function addanswer(index){
$('#add_' + index).hide();
$('#addtext_' + index).fadeIn('slow').focus();
$('#addsubmit_' + index).fadeIn('slow');
}
</script>

</head>
<body>

<div class="name">

<?php
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++; // stuff inside foreach goes here
?>

<div id="q">
<B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?> </B></br>
<B><small><font color= #ba4a00> Description:</font> <?php echo $row["description"]; ?> </small></B></br>
<p id="tag1"><small> <?php echo $row["subject"]; ?> </small></p><p id="tag2"><small> <?php echo $row["sub_subject"]; ?> </small></p>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<textarea style="display:none;" type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your question here.."></textarea>';
echo '<button style="display:none;" onClick="addsubmit('.$index.');" type="addsubmit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
<small><p><?php echo $row["date"]; ?></p></small>
</div>
<?php }
} else {
echo "0 results";
}
$conn->close();
?>
</div>

</body>
</html>

任何帮助将不胜感激..

最佳答案

好吧,这是你重写的代码,我还没有测试它,但如果你的 PHP 代码没有语法错误并且你的数据库已启动并运行,它应该可以工作:

config.php (我添加了 config.php 作为更好的实践)

<?php
$servername = "localhost";
$username = "root";
$password = "******";
$dbname = "the_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

list.php(这将是您的模板文件和其中的一些 php 逻辑*不是最佳实践)

<?php require_once('config.php') ?>
<!DOCTYPE html>
<html lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<style>
.name {
width: 700px;
margin-top: 1%;
margin-left: 20%;
padding-left: 2%;
padding-right: 2%;
padding-top: 10%;
margin-right: 35%;
word-wrap: break-word;
}

textarea#addtext {
width: 600px;
height: 100px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js">
</script>
</head>

<body>

<div class="name">

<?php
$sql = "SELECT * FROM input";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++; // stuff inside foreach goes here
?>

<div id="q">
<!-- NEVER user an ID on elements that are recurring, thats completely wrong, use a class or 'data-' attr -->
<B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?> </B>
</br>
<B><small><font color= #ba4a00> Description:</font> <?php echo $row["description"]; ?> </small></B>
</br>
<p id="tag1"><small> <?php echo $row["subject"]; ?> </small></p>
<p id="tag2"><small> <?php echo $row["sub_subject"]; ?> </small></p>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your question here.."></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
<small><p><?php echo $row["date"]; ?></p></small>
</div>
<?php }
} else {
echo "0 results";
}
$conn->close();
?>
</div>
<script type='text/javascript'>
$(document).ready(function() {
$('.add').click(function(e) {
e.preventDefault();
$(this).parent().find('form').fadeIn('slow').focus();
$(this).hide();
});
});
</script>
</body>

</html>

第二次编辑

如果您需要在文本区域失焦时隐藏表单: https://api.jquery.com/focusout/

$('.addtext').focusout(function() {
$(this).parent().fadeOut('slow');
$(this).parent().parent().find('.add').show();
});

** 第三次编辑 **自动聚焦于文本区域

    <script type='text/javascript'>
$(document).ready(function() {
$('.add').click(function(e) {
e.preventDefault();
$(this).parent().find('form').fadeIn('slow');
$(this).parent().find('form textarea.addtext').focus();
$(this).hide();
});
});
</script>

关于javascript - 在 .php 中制作 Textarea 和提交按钮以隐藏数据库表中的每一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308520/

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