Auf Thema antworten

Guten Nabend zusammen.

Ich habe ein kleines Problem. Also ich habe ein Buch "Javascript und Ajax" in dem ist eine Erklärung womit man ein kleinen chat Programmieren kann. Ich habe diesen Code bereits fehlerfrei abgeschrieben. Nun habe ich folgendes Problem. Undzwar ich habe mir bei ein Free Hoster übergangs weise eine Mysql datenbank gemacht. Nur leider weis ich echt nicht wie ich die datenbank passend zum script einrichte damit alles Funktioniert. Dazu ist im buch leider keine passende erklärung. Mysql ist ein neues Thema für mich. Wenn später alles klappt werde ich auf mein Raspberry pi eine Msql Datebank anlegen und dann wollte ich den chat von zu hause aus hosten. Für eine static Ip ist gesorgt. Jedoch verstehe ich nur nicht wie ich die Mysql datenbank einrichte damit es klappt. Habe jede erdenklich formation ausprobiert.  Ich hoffe einer von euch kann mir Helfen.


Meine skripte sind im anhang als Text datei und einmal so .



[PHP]


$my_link = mysql_pconnect('','root','');

mysql_select_db('test',$my_link);



html

head

titleAjax Chattitle

script language=JavaScript

!--

var xml = null;

var ts = =date('Y-m-d His');

var running = false;

var dl = 0;

function getMsgs()

{

dl++;

if(running && dl  30){return;}

if(running && dl = 30 && xml != null){xml.abort();}

dl = 0;

running = true;

if(xml == null)

{

  if(window.XMLHttpRequest){ xml = new XMLHttpRequest(); }

  else if(window.ActiveXObject)

  {

   try{ xml = new ActiveXObject('Msxml2.XMLHTTP'); }

   catch(e1)

   {

    try{ xml = new ActiveXObject('Microsoft.XMLHTTP'); }

    catch(e2){}

   }

  }

}


if(xml != null)

{

  xml.open('GET','js5_20b.phpts='+escape(ts),true);

  xml.onreadystatechange = fillMsgs;

  xml.send(null);

}

}


var f = null;

function fillMsgs()

{

if(f == null){ f = document.getElementById('first');}

if(xml.readyState == 4){running = false;}

if(xml.readyState == 4 && xml.status == 200)

{

  d = document.getElementById('msgs');

  x = xml.responseXML.documentElement;

  for(i=0;ix.childNodes.length;i++)

  {

   itm = x.childNodes.item(i);

   if(itm.nodeType == 1)

   {

    name = itm.getAttribute('un');

    ts = itm.getAttribute('ts');

    color = itm.getAttribute('color');

    txt = '';

    for(j=0;jitm.childNodes.length;j++)

    {

     txt += itm.childNodes[j].nodeValue;

    }


    e = document.createElement('p');

    e.innerHTML = '<'+name+' um '+ts+'> '+txt;

    e.style.color = color;

    e.style.margin = '2px';

    d.insertBefore(e,f);

    f = e;

   }

  }

}

}

window.setInterval('getMsgs()',1000);



function sendMsg()

{

var sender = null;

if(window.XMLHttpRequest){ sender = new XMLHttpRequest(); }

else if(window.ActiveXObject)

{

  try{ sender = new ActiveXObject('Msxml2.XMLHTTP'); }

  catch(e1)

  {

   try{ sender = new ActiveXObject('Microsoft.XMLHTTP'); }

   catch(e2){}

  }

}


if(sender != null)

{

  t = document.form1.say.value;

  document.form1.say.value = '';

  document.form1.say.focus();

  sender.open('POST','js5_20b.php',true);

  sender.setRequestHeader('Content-Type',

                          'applicationx-www-form-urlencoded');


  sender.send('un==(isset($_POST['un'])

                      urlencode($_POST['un'])

                      'unbekannt')'+

              '&color==(isset($_POST['co'])

                      urlencode($_POST['co'])

                      'black')'+

              '&msg='+escape(t));

}

return false;

}


--

script

head

body bgcolor=#AEC8F7


if(isset($_POST['un']))

{

div id=msgs style=width100%; height500px; overflowscroll; border1px solid #BFBFBF; background-colorwhite;

p id=firstHallo =$_POST['un'], willkommen im Chat!p

div

form onsubmit=return sendMsg(); name=form1 style=margin0px; padding0px; method=post action=#

table align=center

  tr

   tdSag antd

   tdtextarea style=width500px; height50px; name=say cols=50 rows=3textareatd

   tdinput type=Submit style=width100px; height50px; value=Lostd

  tr

table

form


}

else

{

div align=center style=width100%; border1px solid #BFBFBF; background-colorwhite;

div style=width300px;

form method=post action=js5_20a.php

  table

   tr

    tdbDein Usernamebtd

    tdinput type=Text name=un value= td

   tr

   tr

    tdbDeine Farbebtd

    td

     select name=co

      option value=#000000Schwarzoption

      option value=#0000FFBlauoption

      option value=#800000Maroonoption

      option value=#008000Grünoption

      option value=#800080Lilaoption

     select

    td

   tr

  table

  input type=Submit value=Beitreten ...

form

div

div

}


body

html[/PHP]


[PHP]<?


if(isset($_POST['msg']))

{

$m = htmlentities($_POST['msg']);

$c = $_POST['color'];

$u = $_POST['un'];

mysql_query('INSERT INTO ajax_msg (un, msg, log, color) VALUES ("'.$u.'","'.$m.'",NOW(),"'.$c.'")');

echo 'Ok';

exit;

}


$ts = date('Y-m-d H:i:s');

if(isset($_GET['ts'])){$ts = $_GET['ts'];}


header('Content-Type: text/xml');

echo '<?xml version="1.0"?>

<Msglist>

';

$res = mysql_query('SELECT * FROM ajax_msg WHERE log > "'.$ts.'" ORDER BY log ASC');

$num = mysql_num_rows($res);

for($i=0; $i<$num; $i++)

{

echo '<msg un="'.mysql_result($res,$i,'un').'" ts="'.mysql_result($res,$i,'log').'" color="'.mysql_result($res,$i,'color').'" >'.str_replace('&','&amp;',mysql_result($res,$i,'msg')).'</msg>';

}

echo '</Msglist>';

?>[/PHP]


Ich bedanke mich schonmal für jede hilfe


Lg, Jan


Zurück
Oben