...

View Full Version : ShoutBox , Need Someone to finish



ajloun
09-03-2009, 04:11 AM
I have this Very Simple ShoutBox , and looking for someone Finish it by adding Statment to be able delete Message's ..

Mysql Tabel

CREATE TABLE `shoutbox` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`message` longtext NOT NULL,
`time` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;


Shoutbox.php

<?
//the host, name, and password for your mysql
mysql_connect("localhost","user","password");

//select the database
mysql_select_db("spoono_sections");

if($submit)
{
//use the PHP date function for the time
$time=date("h:ia d/j/y");

// inserting it into the shoutbox table which we made in the mysql statements before
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
"VALUES ('NULL','$name', '$message','$time')");
}
?>
<?
//returning the last 5 messages
$result = mysql_query("select * from shoutbox order by id desc limit 5");

//the while loop
while($r=mysql_fetch_array($result))
{
//getting each variable from the table
$time=$r["time"];
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
?>
<? echo $time ?><br>
<? echo $name ?><br>
<? echo $message ?><br>
<? } ?>
<form action="<? echo $php_self ?>" method="post">
<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
<input type="submit" name="submit" value="submit">
</form>

Zangeel
09-03-2009, 04:46 AM
Use $_GET in a link like


echo '<a href="shoutbox.php?delete=' . $id . '">[X]</a>';

Then MySql



<?php
if (isset($_GET['delete']))
{
mysql_query('DELETE FROM `shoutbox` WHERE `id` = \' ' . (int) $_GET['delete'] . ' \' ') or die(mysql_error());
}

ajloun
09-03-2009, 06:15 AM
Thank you Man ,

Just One thing , Why it takes tow Clicks to X to delete the Message?

Zangeel
09-03-2009, 10:58 AM
It takes two clicks to delete it?

funnymoney
09-03-2009, 12:21 PM
add this to code



<?php
if (isset($_GET['delete']))
{
mysql_query('DELETE FROM `shoutbox` WHERE `id` = \' ' . (int) $_GET['delete'] . ' \' ') or die(mysql_error());
header("Location: ");
}

Zangeel
09-03-2009, 05:03 PM
I think if he adds my code above the code to output shoutbox messages that wouldn't happen.

Ultimately I would use JS, and AJAX on this though. Ajax to delete it, JS to remove it cosmetically without needing to refresh, like adding an ID to each message then onclick, send an xmlhttp request then making the id of the message display = 'none' woul make it much smoother.

funnymoney
09-03-2009, 08:08 PM
think something like this?

shoutbox.php

<?php
session_start();

if (!empty($_SESSION['name']) && isset($_POST['shout'])) {
$handle = fopen("shouts.txt", "a+");
fwrite($handle, time()." <strong>Name</strong>: ".$_SESSION['name']. " <strong>shout:</strong> ".htmlspecialchars($_POST['shout'])."\n");
fclose($handle);
header("Location: shoutbox.php");
}

if (empty($_SESSION['name']) && !isset($_POST['name'])) {
$html =
<<<EOF
<form action="" method="post">
<p>Your name <input type="text" name="name" value="" /></p>
<input type="submit" value="Enter name" />
</form>
EOF;
}
elseif (empty($_SESSION['name']) && isset($_POST['name'])) {
$_SESSION['name'] = htmlspecialchars($_POST['name']);
header("Location: shoutbox.php");
}
else {
$html =
<<<EOF
<form action="" method="post">
<p>{$_SESSION['name']} <input type="hidden" name="name" value="{$_SESSION['name']}" /> <input type="text" name="shout" value="Shout here..." /></p>
<input type="submit" value="SHOUT!" />
</form>
<p><a href="?logout=1">Logout</a></p>

EOF;
}

if (isset($_GET['logout'])) {
unset($_SESSION);
session_destroy();
header("Location: shoutbox.php");
}

if (isset($_GET['ajax'])) {

$handle = fopen("shouts.txt", "r");
$contents = stream_get_contents($handle);


$shouts = file("shouts.txt");
$size = count($shouts);

$all = "";
$i = 0;

while ($size >= 0) {
$all .= "<p>".$shouts[$size]."</p>";
$size--;
$i++;
if ($i > 10) {
exit;
}
}
print $all;

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="texthtml;charset=utf-8" />
<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{

document.getElementById("shouts").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST","shoutbox.php?ajax=1",true);
xmlhttp.send(null);
}

var c=0
var t

function timedCount()
{
ajaxFunction();
t=setTimeout("timedCount()",4000);
}

</script>
</script>
<style type="text/css">

</style>
</head>
<body onload="timedCount()">

<div id="shouts">

</div>
<?php

if (!isset($_GET['ajax'])) {
print $html;
}

?>
</body>
</html>

feel free to improve it with jquery ;)

ajloun
09-04-2009, 06:12 AM
@funnymoney than u Man for the solution you posted above ..

funnymoney
09-04-2009, 03:05 PM
no problem, although it's not a solution, more like an idea. you can mess around with code to get more neat solution.

i noticed that this part of code needs a bit more work but i belive you can handle it yourself


if (isset($_GET['ajax'])) {

$handle = fopen("shouts.txt", "r");
$contents = stream_get_contents($handle);


$shouts = file("shouts.txt");
$size = count($shouts);

$all = "";
$i = 0;

while ($size >= 0) {
$all .= "<p>".$shouts[$size]."</p>";
$size--;
$i++;
if ($i > 10) {
exit;
}
}
print $all;

}

ajloun
09-05-2009, 01:38 AM
i noticed that this part of code needs a bit more work but i belive you can handle it yourself


:thumbsup: :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum