php issue form submit auto when page reload - php

I have a form which i want to post problem is this when ever page refresh form submit auto I just want form only form submit when I click on the button.otherwise form not post thanks in advance
<?php
session_start();
//mail("ifaisal24#gmail.com","hhhhhh",print_r($_REQUEST,TRUE));
define('HOST','xxxxx');
define('USER','xxxxx');
define('PASS','Ixxxxx_');
define('DB','xxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
require_once 'include/DB_Functions.php';
if (!isset($_SESSION['email'])){
?>
<script>
window.location = 'login2.php';
</script>
<?php
}
$db = new DB_Functions();
$response = array("error" => FALSE);
if (isset($_POST['sender_email'])&& isset($_POST['reciver_email'])&& isset($_POST['data'])) {
// receiving the post params
$sender_email = $_SESSION['email'];
$_POST['sender_email']=$_SESSION['email'];
$reciver_email = $_POST['reciver_email'];
$data =$sender_email.' : '. $_POST['data'];
///echo $sender_email;
//die();
// create a new chat
$user_data = $db->storeData($data,$sender_email,$reciver_email);
//die;
if ($user_data) {
// chat stored successfully
$response["error"] = FALSE;
// $response["uid"] = $user_data["unique_id"];
$response["user_data"]["sender_email"] = $user_data["sender_email"];
$response["user_data"]["reciver_email"] = $user_data["reciver_email"];
$response["user_data"]["data"] = $user_data["data"];
$response["user_data"]["created_at"] = $user_data["created_at"];
echo json_encode($response);
}
}
// if (isset($_REQUEST['sender_email'])&& isset($_REQUEST['reciver_email'])&& isset($_REQUEST['fatch_server_data'])) {
// $sender_email = $_REQUEST['sender_email'];
// $reciver_email = $_REQUEST['reciver_email'];
$sql = "select * from user_data WHERE (sender_email='$sender_email' AND reciver_email='$reciver_email') OR
(reciver_email ='$sender_email' AND sender_email='$reciver_email')" ;
//$sql = "select * from user_data";
$res = mysqli_query($con,$sql)or mysql_error();
$resultt = array();
while($row = mysqli_fetch_array($res)){
array_push($resultt,
array(
'data'=>$row[3],
'created_at'=>$row[4]
// 'email'=>$row[3]
));
}
// }
json_encode(array("resultt"=>$resultt));
mysqli_close($con);
?>
<?php
define('HOST','mxxxxx');
define('USER','axxxxxxx');
define('PASS','Ixxxxxxx_');
define('DB','axxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from users";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
// 'id'=>$row[0],
'email'=>$row[3],
));
}
json_encode(array("result"=>$result));
mysqli_close($con);
?>
<html>
<head>
<style>
#userschat {
font-family:tahoma;
font-size:12px;
color:black;
border: 5px teal solid;
height: 325px;
width: 300px;
overflow-y:scroll;
}
#usersOnLine {
font-family:tahoma;
font-size:12px;
color:black;
border: 10px teal solid;
height: 625px;
width: 300px;
overflow-y:scroll;
}
#allcon{
margin-bottom: 15px;
font-family:tahoma;
font-size:12px;
color:black;
border: 10px teal solid;
height: 40px;
width: 300px;
}
#contact{
float:right;
}
#all{
float:left;
}
#data {
width: 250px;
height: 30px;
border: 5px teal solid;
float:left;
margin-top:1px;
}
#send {
border: 5px teal solid;
width: 57px;
height: 30px;
float:right;
margin:1px;
}
.container{
width:970px;
height:auto;
margin:0 auto;
}
#messageForm{
}
.clickAble{
display: inline-block;;
cursor:pointer;
}
</style>
<meta http-equiv="refresh" content="600">
</head>
<body bgcolor="beige">>
<div class="container">
<form method="post" action="login2.php">
<input type="submit" id="l_btn" name="l_btn" value="Logout" />
</form>
<?php
if ($_POST) {
// here is where your code would be included
session_destroy();
header("Location:chat.php" . $_SERVER['REQUEST_URI']);
exit();
}
?>
<div id = "welcome"><h2>Welcome to Chat</h2>
<?php echo $_SESSION['email']; ?>
</div>
<div id = "all">
<h1> Chat Box</h1>
<div id="receiver">
<h3>receiver</h3>
<!-- NOTICE THAT THIS IS NEW NOW... -->
<!-- IT'S IMPORTANT THAT YOU HAVE THIS PARAGRAPH EXACTLY AS IT IS HERE -->
<p class='bubbled-data'></p>
</div>
<div id="sender"><h3>sender</h3>
<?php echo $_SESSION['email']; ?>
</div>
<div id="userschat">
<h4 style = " margin-top: 4px; margin-left: 7px">
<?php
foreach($resultt as $key => $val)
{
echo $val['data'];
echo "<br>";
echo $val['created_at'];
echo "<br>";
echo "<br>";
}
?>
</h4>
</div>
<form id="messageForm" action = "<?php $_PHP_SELF ?>" method = "POST">
<input type="hidden" name="sender_email" id="sender_email" value="">
<input type="hidden" name="reciver_email" id="reciver_email" value="">
<input type="text" name="data" id="data" placeholder="data" onFocus="fun1(this)" onBlur="fun2(this)" required="" />
<input id="send" type="submit" value="Send">
</form>
</div>
<div id = "contact">
<div id = "allcon">
<h1 style = " text-align: center; margin-top: 4px;"> All Contacts </h1>
</div>
<div id="usersOnLine" >
<h2 style = " margin-top: 4px; margin-left: 7px">
<?php
foreach($result as $key => $val){
echo "<span class='clickAble'>" . $val['email'] . "</span>";
echo "<br>";
echo "<br>";
}
?>
</h2>
</div>
</div>
</div>
<!-- JAVASCRIPT - JQUERY -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
var objDiv = document.getElementById("userschat");
objDiv.scrollTop = objDiv.scrollHeight;
(function ($) {
$(document).ready(function (e) {
var receiver = $("#receiver");
var clickAbles = $(".clickAble");
receiver.find(".bubbled-data").text(localStorage.getItem("nameValue"));
$("#reciver_email").val(localStorage.getItem("nameValue"));
// BIND THE clickAble SPAN TO AN ON-CLICK EVENT...
// SO THAT WITH EACH CLICK, YOU CAN SIMPLE APPEND THE CONTENT OF THAT SPAN TO THE RECEIVER DIV
clickAbles.on("click", function (evt) {
var nameValue = $(this).text();
receiver.find(".bubbled-data").text(nameValue);
$("#reciver_email").val(nameValue);
localStorage.setItem("nameValue", nameValue);
});
});
})(jQuery);
</script>
</body>
</html>
here is the full code.i edit it

A Post/Redirect/Get design would be a viable solution to your problem. An example of code that would accomplish this is:
if ($_POST) {
// here is where your code would be included
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
The header function then redirects the user to the same page, which means that the $_POST information is lost, hence refreshing the page would not re-send the form.

Related

Why does I can't get the title and the note inside each modal?

The while loop works, where I can display each title as a button, and every button has a modal when clicked, but inside each modal, I can't display the title and the note? why?
I once included the up to down inside the while loop execution but I get only the first title and note for every modal.
If anyone has a question on my code, just leave the comment, Please help out with my code :)
<html>
<head>
<script src="jquery-1.10.2.min.js"></script>
<style>
body
{
margin:0;
}
.submitted{
margin:0px;
}
.modal
{
width:100%;
height:100%;
position:fixed;
top:0;
display:none;
}
.modal_close
{
width:100%;
height:100%;
background:rgba(0,0,0,.8);
position:fixed;
top:0;
}
.close
{
cursor:pointer;
}
.padding{
padding:20px 60px 60px 60px;
}
.note{
text-align:center;
}
#note{
font-family: Javanese text;
}
.call_modal{
font-family: myFirstFont;
}
.modal_main
{
width:50%;
height:400px;
background:#fff;
z-index:4;
position:fixed;
top:16%;
border-radius:4px;
left:24%;
display:none;
-webkit-animation-duration: .5s;
-webkit-animation-delay: .0s;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-o-animation-fill-mode: both;
-webkit-backface-visibility: visible!important;
-webkit-animation-name: fadeInRight;
}
#-webkit-keyframes fadeInRight{0%{opacity:0;-webkit-transform:translateX(20px)}100%{opacity:1;-webkit-transform:translateX(0)}}
::-webkit-input-placeholder{
font-size: 13.4px;
}
.error{
text-align:center;
padding: 170px; 50px; 50px 50px;
}
button
{
padding:20px;
border-radius:5px;
background:#808080;
border:none;
font-size:18px;
color:#fff;
margin:8%;
}
</style>
<script>
$(document).ready(function(){
$(".call_modal").click(function(){
$(".modal").fadeIn();
$(".modal_main").show();
});
});
$(document).ready(function(){
$(".close").click(function(){
$(".modal").fadeOut();
$(".modal_main").fadeOut();
});
});
$(document).ready(function(){
$(".submitted").click(function(){
$(".modal").fadeOut();
$(".modal_main").fadeOut();
});
});
</script>
</head>
<body>
<?php $con=mysqli_connect("localhost","root","","task");?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<?php
echo ' ';
echo '<button class="call_modal" style="cursor:pointer;">'. $row['title'] . '</button>';
?>
<?php
}?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note"> <?php
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo '<br><br>';
echo '<div class="padding">'.$row['title'].'';
echo '<br><br><br><br>';
echo ''.$row['note'].'</div>';
?>
<img src="i783wQYjrKQ.png" class="close" style="line-height: 12px;
margin-top: 1px;
margin-right: 2px;
position:absolute;
top:0;
right:0;">
</div>
</div>
<?php
}?>
</body>
</html>
It looks like you need some sort of unique identifier for every modal. This is more of a javascript problem, and less of a php problem. I'm adding a data-id to every note, since that seems to be the only thing that needs to be unique. Show the modal, and show the one note relevant.
Try doing something more like this.
echo '<button class="call_modal" data-id="{$row['id']}" style="cursor:pointer;">'. $row['title'] . '</button>';
And this
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="note" data-id="<?= $row['id'] ?>">
And this
$(document).ready(function(){
$(".call_modal").click(function(){
$(".modal").fadeIn();
$(".modal .note[data-id='"+$(this).data('id')+"']").show();
});
});

DOCTYPE breaks nth-child background colors

My CSS is validated, but still breaks when adding <!DOCTYPE html>. What am I doing wrong? I have search the forums and the common response seems to be [add height: 100% to the body, html tags. Did that, but no luck.
Without DOCTYPE: http://www.babeweiser.com/rockhistory/
With DOCTYPE: http://www.babeweiser.com/rockhistory/test.php
CSS
html, body
{
height: 100%;
width: 100%;
background: #333333;
}
div.Container
{
margin: auto;
width: 90%;
background: #5e6d3d;
padding: 10px;
}
p
{
font-family: sans-serif;
}
.Table
{
display: Table;
}
.Title
{
display: table-caption;
text-align: center;
font-weight: bold;
font-size: larger;
background: #c6d4a8;
}
.Heading
{
display: table-row;
font-weight: bold;
text-align: center;
}
.Row
{
display: table-row;
height: 100%; width: 100%;
}
div.row:nth-child(odd)
{
background: #daedb2;
}
div.row:nth-child(even)
{
background: #c6d4a8;
}
.Cell
{
display: table-cell;
padding: 15px;
}
select:required:invalid
{
color: #999;
}
option {
color: #000;
}
test.php:
<!DOCTYPE html>
<html>
<head>
<title>Today in Rock History</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<link rel="stylesheet" type="text/css" href="rockhistory2.css">
<script>
$(function() {
$.ajax({
type: "POST",
url: "pull_history2.php",
data: "" + status,
success: function(data){
document.getElementById("demo").innerHTML = data;
}
});
});
</script>
<script>
$(document).ready(function()
{
$(document).on('submit', '#reg-form', function()
{
var data = $(this).serialize();
$.ajax({
type : 'POST',
url : 'pull_history2.php',
data : data,
success : function(data)
{
document.getElementById("demo").innerHTML = data;
}
});
return false;
});
});
</script>
</head>
<body>
<div class="Container">
<form id="reg-form" name="reg-form" method="post">
<select name="month" required id="month" size="1">
<option value="" disabled selected>Month</option>
<?php
$mo = 1;
while($mo <= 12) {
echo '<option value= "' . $mo . '">' . date("F", mktime(0, 0, 0, $mo+1, 0, 0)) . '</option>';
echo "\n";
$mo++;
}
?>
</select>
<select name="day" required id="day" size="1">
<option value="" disabled selected>Day</option>
<?php
$da = 1;
while($da <= 31) {
echo '<option value= "' . $da . '">' . date("j", mktime(0, 0, 0, 0, $da, 0)) . '</option>';
echo "\n";
$da++;
}
?>
</select>
<button type="submit" >Go</button>
</form>
<p id="demo"></p>
</div>
</body>
</html>
pull_history2.php:
<?php
if($_POST) {
$month= $_POST['month'];
$day= $_POST['day'];
} else {
$month = date('n');
$day = date('j');
}
$tdate=date("F j", mktime(0, 0, 0, $month, $day, 0));
?>
<div class="Table">
<div class="Title">
<p><? echo "This Day in Rock History for $tdate" ?></p>
</div>
<div class-"heading">
</div>
<?php
$db = mysql_connect("localhost","xxx", "xxx");
mysql_select_db("babewe5_wlup",$db);
$result = mysql_query("SELECT * FROM RockHistory081512 WHERE month=$month AND day=$day ORDER BY year",$db);
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
while ($row = mysql_fetch_array ($result)) {
?>
<div class="Row">
<div class="Cell">
<p><? echo $row["year"] ?></p>
</div>
<div class="Cell">
<p><? echo $row["history"] ?></p>
</div>
</div>
<?
}
mysql_free_result ($result);
?>
<div class="Row">
<div class="Cell">
</div>
<div class="Cell">
<p><small>Copyright © <? echo date("Y"); ?> Tim Spencer</p>
</div>
</div>
You are using .Row and .row in your CSS. One is initial caps and the other is not. Change div.row:nth-child to div.Row:nth-child.
Applying the doctype enforces a set of rules on your document. Without the doctype the two are treated the same.
It is the case sensitivity. either modify classname to "row" (lowercase letters). or change the relevant styles in stylesheet.
div.Row:nth-child(even) -> (R as uppercase)..
div.Row:nth-child(odd)
You better check the browser compatibilities of n-th selectors.. It is supported in IE 9 and higher versions.
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

PHP & jQuery ajax Chat

I have created a chat application using PHP, mysql, and ajax for posting data to the server. I have tested it by spamming the chat repeatedly, and everything works for about 7 or 8 seconds until my chat will not post further. On refresh, I get a "NO DATA RECEIVED" error from google chrome. Basically I think I am overloading my site. Is there a way that I could optimize this chat? Or is it not a server-side issue at all?
This is my jQuery that is posting to my server:
$(document).ready(getMessage());
//Send message
$('#send').click(function() {
var message = $('#message').val();
var username = $('#un').val();
var id = $('#id').val();
var fullname = $('#name').val();
$.ajax({
url: 'chat/send_message.php',
type: 'POST',
data: {
message: message,
username: username,
user_id: id,
fullname: fullname
},
success: function() {
$('#message').val('');
getMessage();
}
});
return false;
});
function getMessage() {
$.post('chat/get_message.php', {getMessage: true}, function(message) {
$('#chatbox').html(message);
$('#chatbox').scrollTop($('#chatbox')[0].scrollHeight);
});
}
setInterval(getMessage, 5000);
This is the PHP that handles the sending of the message. It just stores it in a database:
<?php
//Message sending
if(isset($_POST['message'])) {
try {
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
} catch (PDOException $e) {
echo("Could not connect to the server.");
}
$message = $_POST['message'];
$timesent = date('Y-m-d H:i:s');
$id = $_POST['user_id'];
$fullname = $_POST['fullname'];
$username = $_POST['username'];
$query = $pdo->prepare("INSERT INTO `message`(`message`,`user_id`,`timesent`,`fullname`,`username`) VALUES (:message,:user_id,:timesent,:fullname,:username)");
$query->execute(array(
':message' => $message,
':user_id' => $id,
':timesent' => $timesent,
':fullname' => $fullname,
':username' => $username,
));
} else {
echo("Could not send message");
}
This is my HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head>
<?php
include 'resources/head.php';
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
?>
<title>GoonSquad | Chat</title>
<style type="text/css">
form, p, span {
margin:0;
padding:0; }
input { font:12px arial; }
a {
color:#0000FF;
text-decoration:none; }
a:hover { text-decoration:underline; }
#wrapper {
margin:0 auto;
padding-bottom:0;
background: rgba(0, 0, 0, 0.13);
width: 1000px;;
border:1px solid #ACD8F0;
text-align: center;
}
#chatbox {
text-align:left;
margin:0 auto;
margin-bottom:25px;
padding:10px;
background:#fff;
height:400px;
width:940px;
border:1px solid #ACD8F0;
overflow:auto; }
#message {
width:860px;
border:1px solid #ACD8F0;
margin-right: 5px;
}
#submit { width: 60px; }
.error { color: #ff0000; }
#menu { padding:12.5px 25px 12.5px 25px; }
.welcome {
margin-left:auto;
margin-right: auto;
margin-top: 20px;
font-size: 150%;
}
.message {
width: 100%;
height: auto;
background-color: #cccccc;
margin-bottom: 6px;
border-radius: 3px;
padding: 6px;
}
.msgln { margin:0 0 2px 0; }
</style>
</head>
<body>
<?php include 'resources/header.php' ?>
<div id="row">
<div id="large-9 columns" class="override" role="content">
<div id="wrapper" class="left-align">
<div id="menu">
<p class="welcome">GoonSquad Chat</p>
<div style="clear:both"></div>
</div>
<div id="chatbox">
</div>
<form id="messenger" action="" enctype="multipart/form-data">
<input type="text" id="message" class="left" autocomplete="off" size="63" maxlength="2000">
<input type="submit" id="send" class="button tiny" value="Send" >
<input type="hidden" id="id" value="<?php echo $user->data()->id; ?>">
<input type="hidden" id="name" value="<?php echo $user->data()->fullname?>">
<input type="hidden" id="un" value="<?php echo $user->data()->username?>"
</form>
</div>
</div>
</div>
<script type="text/javascript" src="scripts/chat.js"></script>
</body>
</html>

Delete comment from database php

I am trying to delete comments from my comment system but the code I have is not working. I want to be able to delete each comment from my table including the parent comments. At the moment my code does not get the parameter of the comment from the delete button link.
function.php:
<?php
function getComments($row) {
echo "<li class='comment'>";
echo "<div class='aut'>" . $row['author'] . "</div>";
echo "<div class='comment-body'>" . $row['comment'] . "</div>";
echo "<div class='timestamp'>" . $row['created_at'] . "</div>";
echo "<a href='#comment_form' class='reply' id='" . $row['id'] . "'>Edit Song</a>";
echo "<span> </span>";
echo "<a href='delete.php?id='" . $row['id'] . "'>Delete Song</a>";
$q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
$r = mysql_query($q);
if(mysql_num_rows($r)>0)
{
echo "<ul>";
while($row = mysql_fetch_assoc($r)) {
getComments($row);
}
echo "</ul>";
}
echo "</li>";
}
?>
delete.php:
<?php
include 'includes/config.php';
$deleteid = $_GET['id'];
mysql_query("DELETE FROM threaded_comments WHERE id='$deleteid'");
echo "Song has been deleted!";
header ('Location: index.php');
?>
I have added the index.php and post_comment page page. Also the delete function still doesn't work when I made the changes.
index.php:
<?php
include("config.php");
include("functions.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multi-Lyric Collaboration System</title>
<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
$("a.reply").click(function() {
var id = $(this).attr("id");
$("#parent_id").attr("value", id);
$("#name").focus();
});
});
</script>
<style type='text/css'>
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,
pre, form, fieldset, table, th, td { margin: 0; padding: 0; }
body {
font-size: 14px;
line-height:1.3em;
background: url('music.jpg');
height: 100%;
background-size:100% ;
background-position: static;
background-color: purple;
min-height: 100%;
background-repeat: no-repeat;
}
label{
color: white;
font-size: 2em;
}
a, a:visited {
outline:none;
color:#7d5f1e;
}
.clear {
clear:both;
}
#wrapper {
width:480px;
margin:0px auto;
padding:15px 0px;
}
.comment {
padding:5px;
border:2px solid black;
border-width: 1px;
margin-top:15px;
list-style:none;
background-color: white;
opacity: 0.96;
-moz-border-radius:20px;
-webkit-border-radius:20px;
overflow-y: scroll;
}
.aut {
font-weight:bold;
}
.timestamp {
font-size:85%;
float:right;
}
#comment_form {
margin-top:15px;
}
#comment_form input {
font-size:1.2em;
margin:0 0 10px;
padding:3px;
display:block;
width:100%;
}
#comment_body {
display:block;
width:100%;
height:150px;
}
#submit_button {
text-align:center;
clear:both;
}
header{
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id='wrapper'>
<header><font size="20">MultiLyric Collaborator</font></header>
<br>
<ul>
<?php
$q = "SELECT * FROM threaded_comments WHERE parent_id = 0";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
getComments($row);
endwhile;
?>
</ul>
<form id="comment_form" action="post_comment.php" method='post'>
<label for="name">Name:</label>
<input type="text" name="name" id='name'/>
<label for="comment_body">Enter Song:</label>
<textarea name="comment_body" id='comment_body'></textarea>
<input type='hidden' name='parent_id' id='parent_id' value='0'/>
<div id='submit_button'>
<input type="submit" value="Add comment"/>
</div>
</form>
</div>
</body>
</html>
post_comment.php:
<?php
include("config.php");
$author = mysql_real_escape_string($_POST['name']);
$comment_body = mysql_real_escape_string($_POST['comment_body']);
$parent_id = mysql_real_escape_string($_POST['parent_id']);
$q = "INSERT INTO threaded_comments (author, comment, parent_id) VALUES ('$author', '$comment_body', $parent_id)";
$r = mysql_query($q) or die(mysql_error());
if(mysql_affected_rows()==1) {
header("location:index.php");
}
else {
echo "Comment cannot be posted. Please try again.";
}
?>
From the "SELECT" statement in getComments(), it looks like what you need to do is:
mysql_query("DELETE FROM threaded_comments WHERE parent_id ='$deleteid'");
It is 'parent_id' not 'id'.

Creating a Auto Updating <div></div> When Information Was Pasted on Mysql

It's my first day here, I thought you guys could help me. So, I'm working on this kind of chatting service for a friend. I want it to auto update anything inside the div tags that has an id of messages. I tried adding some javascript and stuff from other posts, but they just made it worse. So, if anyone could fix it and comment, or give me suggestions. I want it as soon as someone posted something, it updates the posts in the div tags with the id of messages.
<?php
session_start();
if(!isset($_SESSION['username']))
{
header("Location: /index.php");
}
?>
<html>
<head>
<title>
House Email System
</title>
<style>
body
{
background: #383838;
font-family: Arial;
color: #444444;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -150px;
}
form
{
background: #ffffff;
width: 270px;
border-radius: 15px;
box-shadow: 3px 3px 6px 1px #303030;
}
input
{
margin-left: 15px;
}
p
{
margin-left: 15px;
}
error
{
padding:2px 4px;
margin:0px;
border:solid 1px #FBD3C6;
background:#FDE4E1;
color:#CB4721;
font-size:14px;
font-weight:bold;
}
div
{
background: #f0f0f0;
}
hr
{
color: #d0d0d0;
background-color: #d0d0d0;
height: 3px;
border: 0px;
}
username
{
margin-left: 5px;
}
message
{
margin-left: 10px;
}
</style>
</head>
<body>
<?php
$connect = mysql_connect("localhost", "root", "*******");
mysql_select_db("*******");
$username = $_SESSION['username'];
$message = $_POST['message'];
$submit = $_POST['submit'];
if($submit)
{
if($message)
{
mysql_query("INSERT INTO messages VALUES('', '$username', '$message')");
}
else
{
?>
<center>
<error>Please enter a message to send.</error> <br />
</center>
<br />
<?php
}
}
?>
<form action="active.php" method="POST">
<br />
<p>Message</p>
<input type="text" name="message" /> <br />
<br />
<input type="submit" name="submit" value="Submit" /> <br />
<br />
<?php
$query = mysql_query("SELECT * FROM messages ORDER BY id DESC");
$num_rows = mysql_num_rows($query);
if($num_rows > 0)
{
while($row = mysql_fetch_assoc($query))
{
$username = $row['username'];
$message = $row['message'];
?>
<div id="messages">
<br /> <username><b><?php echo $username; ?></b></username>
<hr />
<message><?php echo $message; ?></message> <br />
<br /> </div> <br />
<?php
}
}
else
{
?>
<center>
<error>There are no messages to display.</error> <br />
</center>
<br />
<?php
}
?>
</form>
</body>
</html>
I hope you guys can help me, thank you.
First create a page message.php as:
<?php
session_start();
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#submit').click(function()
{
//alert("hello");
// ss=document.getElementById("message").value;
$.ajax({
type : 'POST',
url : 'active.php',
data: {
messages :$('#message').val()
},
success : function(data){
$('#messages').html(data);
}
});
});
});
</script>
<title>
House Email System
</title>
<style>
body
{
background: #383838;
font-family: Arial;
color: #444444;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -150px;
}
form
{
background: #ffffff;
width: 270px;
border-radius: 15px;
box-shadow: 3px 3px 6px 1px #303030;
}
input
{
margin-left: 15px;
}
p
{
margin-left: 15px;
}
error
{
padding:2px 4px;
margin:0px;
border:solid 1px #FBD3C6;
background:#FDE4E1;
color:#CB4721;
font-size:14px;
font-weight:bold;
}
div
{
background: #f0f0f0;
}
hr
{
color: #d0d0d0;
background-color: #d0d0d0;
height: 3px;
border: 0px;
}
username
{
margin-left: 5px;
}
message
{
margin-left: 10px;
}
</style>
</head>
<body>
<center>
<error>Please enter a message to send.</error> <br />
</center>
<br />
<form action="active.php" method="POST">
<br />
<p>Message</p>
<input type="text" name="message" id="message" /> <br />
<br />
<input type="button" id="submit" name="submit" value="Submit" /> <br />
</form>
<br />
<div id="messages">
</div>
</body>
</html>
<script language="JavaScript"><!--
// v3 compatible:
function doSomething() {
// do something here...
$.ajax({
type : 'POST',
url : 'active.php',
data: {
messages :$('#message').val()
},
success : function(data){
$('#messages').html(data);
}
});
setTimeout('doSomething()',3000);
}
setInterval('doSomething()',3000);
//--></script>
Then Create a page active.php as
<?php
session_start();
$connect = mysql_connect("localhost", "root", "");
mysql_select_db("test");
$username=$_SESSION['username'];
$message=$_POST['messages'];
if($message!="")
{
mysql_query("INSERT INTO messages VALUES('', '$username', '$message')");
}
$query = mysql_query("SELECT * FROM messages ORDER BY id DESC limit 1,0");
$num_rows = mysql_num_rows($query);
if($num_rows > 0)
{
while($row = mysql_fetch_assoc($query))
{
$username = $row['username'];
$message = $row['message'];
?>
<div>
<br /> <username><b><?php echo $username; ?></b></username>
<hr />
<message><?php echo $message; ?></message> <br />
<br /> </div>
<br />
<?php
}
}
else
{
echo"No message to show";
}
?>
This is very general chat.Now you need to change it according to your requirements.
I want it as soon as someone posted something, it updates the posts in the div tags with the id of messages.
You will have to use javacript for that part. Everything else can be done with PHP and MySQL.
I watched great youtube tutorial the other day on how to do this. Its is a "quick and dirty" solution but should give you something to build on. http://www.youtube.com/watch?v=FyXeOX-uYMc

Categories