user6779766 user6779766 - 2 months ago 5
MySQL Question

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

Been working on this for long time.. want to add a js effect (hide/unhide) to a simple submit form...

Fortunately it is working but unfortunately it is only working for single row in the DB table.

I made script:1 (posted below) which was corrected by a well wisher from this site which is script:2 (posted below).

unfortunately both are not working. hope you guys help me find the missing peice in it..

SCRIPT#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>


SCRIPT#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>


Any Help would be greatly Appreciated..

Answer

Ok so here is your code rewritten, I have not tested it but it should work if your PHP code has no syntax error and your DB is up and running:

config.php (I added config.php as its just better practice)

<?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 (this will be your template file and some php logic in there *not the best practice)

<?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>

Second Edit

If you need to hide the form when the textarea is out of focus: https://api.jquery.com/focusout/

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