Click to See Complete Forum and Search --> : Data only submits to script when Submit clicked in IE


jimmybeaches
08-15-2004, 04:32 PM
Hello all,

I have two questions:

1.
I created a very simple chat script for talking to a friend. When I am using Mozilla, I can enter data into the textbox and click Submit OR push enter, but when using Internet Explorer I HAVE to click the submit button. Sometimes the message is submitted when I push Enter, but not always. It always works when clicking the submit button... ???

Question 2.

When my friend enters a message, it is written to the chatfile (text file) like so:
$date|$ipaddress|$themessage

When I enter "sdf" as a message, this will trigger a delete function. the delete function opens up the chatfile, deletes the contents, and writes "1" to the file. What I am trying to do is when the file only contains "1" then a NO DATA message is displayed. I sort of have this working, but the NO DATA message always shows up ender the messages. Here is my bit of code that reads and displays the contents of the chatfile:


if (!file_exists($chatfile)) {

echo "NO DATA";

} else {

$fp = fopen($chatfile, "r+");
$file_contents = fread($fp, filesize($chatfile));
fclose($fp);

$line = explode("\n", $file_contents);

rsort($line);

$i = 0;

while($i <= sizeof($line)) {

$data_pair = explode("|", $line[$i]);

if ($data_pair[1]==$ip) {
$who = "you";
$message = "you";
} else {
$who = "guest";
$message = "guest";
}

if ($data_pair[1]<=0 || $data_pair[0]=="1") { //
echo "<center><b>NO DATA</b></center>";
break;
} else {

echo <<<END
<span class="details">$data_pair[0]</span>
<br /><br />
$who: <span class="$message">$data_pair[2]</span>
<br /><br />
<hr size="1" color="#666666" noshade>
END;

}

$i++;
}

} // end if file exists statement


Here is the script in action:
http://www.robj.ca/test/

and the source:
http://www.robj.ca/test/chat.txt

Can anyone help me out? :confused:

sciguyryan
08-15-2004, 05:14 PM
1) Some browsers dont like you clicking enter in textareas and submiting the information while others do.

2) Try checking for a "1" in the first split:

if ($line[0] == "1"){
echo "Whatever....";
}


Hope that helps,

- RyanJ

BuezaWebDev
08-16-2004, 12:21 AM
For question #1

use

if ($_SERVER['REQUEST_METHOD'] == 'POST') {


instead of

if ($_POST['Submit']) {


Regards.

jimmybeaches
08-16-2004, 07:33 AM
BuezaWebDev,

That did it! Thanks!

I have changed some code and am having another problem. When I wanted to delete the contents of the chatfile, I would write "1" to the file. If the file contained "1", a NO DATA message would appear, but when I post a message, I am using fputs to write to append to the file, therefore a "1" is showing up beside the date/time of the message. So I changed the "1" to " " and it works better, once I have posted one message, I get two blank messages showing up after my first message, almost like one tailing blank line is doing this...

Here is the code that opens and displays the messages:


if (!file_exists($chatfile)) {

echo "NO DATA";

} else {

$fp = fopen($chatfile, "r+");
$file_contents = fread($fp, filesize($chatfile));
fclose($fp);

$line = explode("\n", $file_contents);

rsort($line);

$i = 0;

while($i <= sizeof($line)) {

$data_pair = explode("|", $line[$i]);

// suggested by RyanJ
if ($line[0]==" ") { // <- I changed to " " because a "1" was showing up beside the date
echo "<div class=\"nodata\">NO DATA</span>";
break;
}

// finds who posted data
if ($data_pair[1]==$ip) {
$who = "you";
$message = "you";
} else {
$who = "guest";
$message = "guest";
}

//if ($data_pair[1]<=0 || $data_pair[0]=="1") { //
// echo "<div class=\"nodata\">NO DATA</span>";
// break;
//} else {

echo <<<END
<span class="details">$data_pair[0]</span>
<br /><br />
$who: <span class="$message">$data_pair[2]</span>
<br /><br />
<hr size="1" color="#666666" noshade>
END;

//}

$i++;
}

} // end if file exists statement


Any ideas?

Thanks again! :D

sciguyryan
08-17-2004, 04:48 AM
It does not seem to be a probelm with that section of the script, it maybe with the writeing part of the script though.


- RyanJ