在.php中创建Textarea和submit按钮以隐藏数据库表中的每一行

Making Textarea and submit button in .php to hide for every row in database table?

本文关键字:一行 数据库 隐藏 Textarea 创建 php submit 按钮      更新时间:2023-09-26

我做了很长时间了。想要添加一个js效果(隐藏/取消隐藏)到一个简单的提交表单…

幸运的是,它正在工作,但不幸的是,它只工作于DB表中的单行。

我制作了脚本:1(张贴在下面),这是由一个好心人从这个网站更正的脚本: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="answerswer_'.$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代码没有语法错误,你的DB启动并运行,它应该可以工作:

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="answerswer_'.$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>