Jump to content

ajax intregration help


inTel

Recommended Posts

hello I am new to ajax. I have coded this php shoutbox and manage the following javascript code, but I cannot add both to make an ajax shoutbox, Could anyone please help me?

shout.php

<html>
<head>
<title>ajax-chat</title>
</head>
<body>
<?php
echo "<h3>shoutbox</h3>";
mysql_connect("localhost","root","");
echo mysql_error();
mysql_select_db(test);
echo mysql_error();
$msg=$_POST["msg"];
if(!empty($msg))
{
mysql_query("INSERT INTO ajax_chat SET msg='".$msg."',postime='".time()."'");
echo mysql_error();
}
$q=mysql_query("SELECT msg,postime FROM ajax_chat ORDER BY id DESC LIMIT 10");
echo mysql_error();
while($a = mysql_fetch_row($q))
{
echo date("h:i:s a",$a[1])."- $a[0]<br/>";
}
?>
<form action=shout.php" method="POST">
<input type="text" name="msg">
<input type="submit" value="Shout">
</form>
</body>
</html>

sql

CREATE TABLE IF NOT EXISTS `ajax_chat` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `postime` bigint(10) unsigned NOT NULL DEFAULT '0',
  `msg` char(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

javascript

<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('userInput').value;
document.getElementById('boldStuff2').innerHTML += userInput+"<br/>";
}
</script>
<p>Shout Message<br/> <b id='boldStuff2'></b> </p> 
<input type='text' id='userInput'/>
<input type='button' onclick='changeText2()' value='Shout'/>

Link to comment
Share on other sites

AJAX deals with 2 separate files. You have them all in one.

 

Create a PHP file with just the insert and select and echo out the necessary information. You have to decide how to print them out. Then in your HTML file, have an AJAX call to that PHP file. Get the information and display them. Google AJAX.

Link to comment
Share on other sites

I googled several times and compelled to start a topic here. I mentioned a javascript function which shows how I want to show the texts.  It will show last 10 texts form the sql table. Please help me.

Link to comment
Share on other sites

Ok I made this buggy code,shout.php and sql metioned earlier. Now please help me to make it work.

 

<html>
<head>
<title> ajax-shout</title>
</head>
<script type="text/javascript">
var xmlHttp;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="shout.php";
/// bugs
url=url+"?msg="+str;
xmlHttp.onreadystatechange=ChangeText2;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);

function changeText2(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
///bugs

var userInput = document.getElementById('userInput').value;
document.getElementById('boldStuff2').innerHTML += userInput+"<br/>";
document.getElementById("boldStuff2").innerHTML=xmlHttp.responseText;
///bugs
}
}

<p>Shout Message<br/> <b id='boldStuff2'></b> </p> 
<input type='text' id='userInput'/>
<input type='button' onclick='changeText2()' value='Shout'/>

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}
</script>

<p>Shout Message<br/> 
<div id='boldStuff2'></div> </p> 
<form>
<input type='text' id='userInput'/>
<input type='button' onclick='changeText2()' value='Shout'/>
</form>
</body>
</html>

Link to comment
Share on other sites

It's not a case of fixing the above code, it is missing parts, a bit like having a car without the engine.....

 

What you should do is put the INSERT INTO query into a new script, seperate from the one that displays the shouts.

 

I don't think you understand what is involved? Even if the above script worked with no bugs, it would not behave how you would expect an AJAX Shoutbox to. It wouldn't refresh and update the shouts... when you post a shout it would not show on the screen unless you refreshed the page, which defeats the purpose of using AJAX.

 

That's what is missing, it needs to use AJAX to retrieve the shouts on a timer. At least that is what I would an AJAX shout box to do.

Link to comment
Share on other sites

Read rule #7 - http://www.phpfreaks.com/page/forum-rules

 

Besides, if we write it for you, you won't learn. I'm aware you're new to AJAX and it's not something particularly easy to understand your first time through, but you know JavaScript right? Well AJAX is JavaScript. It's just a bit funky because you're not used to it.

 

I told you str is undefined, so fix that problem. We can debug your code, but asking us to write it for you won't help.

Link to comment
Share on other sites

ken2k7, I understood that what type of guy you are but I was waiting for your last post. I tried as far as I know. Are you such a blind that you dont see my topic name 'ajax intregration help'. Your most of the posts are full of suggestions. Is this a SUGGESTION FORM or Ajax Help forum? you write what code for me huh? metioned that str is undefined what I metion  earlier? You are damn blind.

look at your post. And how technical liar.

http://www.phpfreaks.com/forums/index.php/topic,250514.msg1174983.html#msg1174983

 

If you need help, don't hesitate to ask, but I don't think this is a tutoring class.
.

 

Besides, if we write it for you, you won't learn

 

Shame on you.

 

You better write a book and sell it because you hardly help people for free with your socalled programming knowledge.

 

Link to comment
Share on other sites

Telling you that the variable str is undefined is considered help. It's a simple fix really. I mean I'm testing to see if you know basic JavaScript. I don't mind writing script for people as long as they know some basics and need help with other parts. But if you don't even put up the effort, I shouldn't either. Read my other posts if you think I don't help by writing codes.

 

Also, that post you linked to wasn't directed towards you so it's not really a valid to quote it here. I'm not paid to provide 'help' or whatever you call this and I also have no obligation in this forum or toward you. But fine, I'll step down from your topics.

Link to comment
Share on other sites

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.