Jump to content

Recommended Posts

here's the screen shot of the error

 

fnwu3s.jpg

 

I don't really know what's going on

 

here's the  chat.php code

 

<?php

require_once("chat.class.php");

$mode = $_POST['mode'];
$id = 0;

$chat = new Chat();

if($mode == 'SendAndRetrieveNew')
{
  $name = $_POST['name'];
  $message = $_POST['message'];
  $color = $_POST['color'];
  $id = $_POST['id'];
  if($name != '' && $message != '' && $color != '')
  {
    $chat->postMessage($name,$message,$color);
  }
}
elseif($mode == 'DeleteAndRetrieveNew')
{
  $chat->deleteMessages();
}
elseif($mode == 'RetrieveNew')
{
  $id = $_POST['id'];
}

if(ob_get_length()) ob_clean();
header('Expires: Mon, 26 July 1997 05:00:00 GMT');
header('Last-Modified: '. gmdate('D, d M Y H:i:s'). 'GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: application/json');
echo json_encode($chat->retrieveNewMessages($id));
?>

 

and here's the chat.class.php

 

<?php

require_once('config.php');
require_once('error_handler.php');

class Chat
{
  private $mMysqli;

  function __construct()
  {
    $this->mMysqli = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_DATABASE);
  }

  public function __destruct()
  {
    $this->mMysqli->close();
  }

  public function deleteMessages()
  {
    $query = 'TRUNCATE TABLE chat';
    $result = $this->mMysqli->query($query);
  }

  public function postMessage($name,$message,$color)
  {
    $name = $this->mMysqli->real_escape_string($name);
    $message = $this->mMysqli->real_escape_string($message);
    $color = $this->mMysqli->real_escape_string($color);
    $query = 'INSERT INTO chat(posted_on, user_name,message,color)';
    $query .= 'VALUES(NOW(),"$name","$message","$color")';
    $result = $this->mMysqli->query($query);
  }

  public function retrieveNewMessages($id=0)
  {
    $id = $this->$mMysqli->real_escape_string($id);
    if($id>0)
    {
      $query =
      'SELECT chat_id, user_name, message, color, DATE_FORMAT(posted_on, "%Y-%m-%d %H:%i:%s") AS posted on '.
      'FROM chat where chat_id > '. $id .
      'ORDER BY chat_id ASC';
    }
    else
    {
      $query =
      'SELECT chat_id, user_name, color, posted_on FROM'.
      '{SELECT chat_id, user_name, message, color, '.
      'DATE_FORMAT(posted_on,"%Y-%m-%d %H:%i:%s) AS posted_on'.
      'FROM chat '.
      'ORDER BY chat_id DESC'.
      'LIMIT 50) AS Last50'.
      'ORDER BY chat_id ASC';
    }

    $result = $this->mMysqli->query($query);
    $response = array();
    $response['clear'] = $this->isDatabaseCleared($id);
    $response['messages'] = array();

    if($result->num_rows)
    {
      while($row = $result->mysqli_fetch_array(MYSQLI_ASSOC))
      {
        $message = array();
        $message['id'] = $row['chat_id'];
        $message['color'] = $row['color'];
        $message['name'] = $row['user_name'];
        $message['time'] = $row['posted_on'];
        $message['message'] = $row['message'];
        array_push($response['messages'],$message);
      }
      $result->close();
    }
    return $response;
  }

  private function isDatabaseCleared($id)
  {
    if($id > 0)
    {
      $check_clear = 'SELECT count(*) old FROM chat where chat_id <= $id';
      $result = $this->mMysqli->query($check_clear);
      $row = $result->fetch_array(MYSQLI_ASSOC);
      if($row['old'] == 0)
        return 'true';
      return 'false';
    }
    return 'true';
  }
}

?>

 

and here's the html page

 

<html>
<head>
    <title>testing chat.php</title>
</head>
<body>
    <form action="chat.php" method="post">
        Mode:
        <input type="text" name="mode" size="50" value="SendAndRetrieveNew" />
        <br />
        Name:
        <input type="text" name="name" size="50" />
        <br />
        Message:
        <input type="text" name="message" size="50" />
        <br />
        Color:
        <input type="text" name="color" size="50" value="#000000"/>
        <br />
        ID:
        <input type="text" name="id" size="50" />
        <br />
        <input type="submit" />
    </form>
</body>
</html>

 

and here's my error_handler.php

 

<?php


set_error_handler('error_handler',E_ALL);

function error_handler($errNo,$errStr,$errFile,$errLine)
{
    if(ob_get_length()) ob_clean();
    $error_message = 'ERRNO: '. $errNo. chr(10).
                    'TEXT: '. $errStr. chr(10).
                    'LOCATION: '. $errFile. chr(10).
                    ', line '. $errLine;
    echo $error_message;
    exit;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/199505-undefined-index-help/
Share on other sites

Is the filename actually chat.php without a typo maybe chat.phps or something.

Does it prompt the download in I.E?

 

What changes have you made in chat.php for it to lead to a possible download prompt.

 

yeah, i tried in I.E(8),, and it does the same thing too, i didn't alter anything in the chat.php file, only the chat.class.php

but no, am 101% sure all my files got .php extension except for the test.html ofcourse.

any idea now why chat.php is being downloaded when form is submitted ?

none sir, my webhosting account doesn't allow ajax chat..actually this code isn't fully complete yet, it's supposed to be an ajax stuff :)

Seriously?! Ajax is PHP + Javascript.. all paid hosts must allow that.. quite a few free hosts do too like 000webhosting.

How are you testing the code?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.