I have a proble: I have a function in the html head, and then in the body I have a form type submit and run the function onsubmit. It seeems I cant reach the function or go to function to insert details to database. I cant complete this idea. Please help me this.
<?php
function sida() {
$host = "localhost";
$username = "root";
$password = "";
$databasename = "vinhcv_truonghoc";
$connect = mysql_connect($host, $username, $password);
$db = mysql_select_db($databasename);
if (isset($_POST['comment']) && isset($_POST['name'])) {
$comment = $_POST['comment'];
$name = $_POST['name'];
$q = "insert into comments values('', '$name', '$comment', CURRENT_TIMESTAMP)";
echo $q;
$insert = mysql_query($q);
if (!$insert) { echo mysql_error(); }
$id = mysql_insert_id($insert);
$select = mysql_query("select name, comment, post_time from comments where name = '$name' and comment='$comment' and id='$id'");
if ($row = mysql_fetch_array($select)) {
$name = $row['name'];
$comment = $row['comment'];
$time = $row['post_time'];
?>
<div class="comment_div">
<p class="name">Posted By:<?php echo $name;?></p>
<p class="comment"><?php echo $comment;?></p>
<p class="time"><?php echo $time;?></p>
</div>
<?php
}
exit;
}
}
?>
and the form in the body:
<form method="POST" onsubmit="sida()">
<textarea id="comment" placeholder="Write Your Comment Here....."></tetarea>
<br>
<input type="text" id="username" placeholder="Your Name">
<br>
<input type="submit" value="Post Comment">
</form>
<div id="all_comments">
<?php
$host = "localhost";
$username = "root";
$password = "";
$databasename = "vinhcv_truonghoc";
$connect = mysql_connect($host,$username,$password);
$db = mysql_select_db($databasename);
$comm = mysql_query("select name,comment,post_time from comments order by post_time desc");
while($row = mysql_fetch_array($comm))
{
$name = $row['name'];
$comment = $row['comment'];
$time = $row['post_time'];
?>
<div class="comment_div">
<p class="name">Posted By:<?php echo $name;?></p>
<p class="comment"><?php echo $comment;?></p>
<p class="time"><?php echo $time;?></p>
</div>
<?php
}
?>
</div>
In the body it can connect to database to get information, that mean not thing wrong when connect with database, so why it cant insert to database?