I'm trying to write from my database into my page, using one effect. The objective is, when it writes my comment on the page, show it with an effect.
<?php
if(count($_POST) > 0){
echo '<script language="javascript">';
echo '$( "#toggle" ).toggle( "drop" );';
echo '</script>';
$db = new mysqli("localhost", "root", "usbw", "trab_projeto");
$qr = $db->query("INSERT INTO comments(comment) VALUES ('{$_POST['mensagem']}')");
echo "<fieldset id='toogle'>";
echo "<p>";
$row = $db->query("SELECT * FROM comments ORDER BY comment_id DESC LIMIT 1")->fetch_assoc();
echo $row["comment"];
echo "</p>";
echo "</fieldset>";
}
?>
The part of the script doesn't work. These code is executed when I click on a submit form.
<div class="cadastro">
<form action="" id="form-msg" method="post" enctype="multipart/form-data">
<fieldset>
<p>
<span><b>Escreva aqui o seu comentário:</b></span><br>
<textarea name="mensagem" style="margin: 0px; width: 511px; height: 119px;"></textarea>
</p>
<input type="submit" value="Enviar">
</fieldset>
</form>
</div>
To return a JSON response, you must first place this in your
header()
. At the top of your script append:Moving on, to prevent your SQL injection I have changed the driver to
PDO
which you can find documentation on.For future notice, this 'comment' it not attached to any 'thread' scope. If you have multiple posts, you should give each post a unique ID and cross-reference the post ID with the comment so you can load the comments for that specific post.
You can then use jQuery to send requests like so:
Note the following code above needs a
click()
attached to your submit button. Consider removing this line of code:And using:
So you can target it through the form like so:
You could use an effect when showing the comment to the user-end like so: