Jump to content

Problem building AJAX/PHP guestbook

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
I'm currently starting to build some applications in PHP to learn the language and I've started with a guestbook.
Everything works except for the part that's supposed to add a message to the guestbook.

The problem isn't the PHP code for adding a message but the AJAX...
Instead of adding a post and doing it in the background it keeps going to the addmessage.php page... and because it's supposed to be AJAX there shouldn't be a page reload...

If anyone can help me solve this problem and give me some tips on what to add to the guestbook like security or other stuff I forgot...

My code is:

addmessage.php
<?php

require_once("config.php");

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$query = "INSERT INTO posts (name, email, message) VALUES ('$name','$email', '$message')";

$result = mysqli_query($conn, $query) or diet("Error executing insert query.");

if($result){
	echo "Message added";
}

?>

showmessages.php
<?php

require_once("config.php");

$query = "SELECT * FROM posts ORDER BY id DESC";

$result = mysqli_query($conn, $query) or die("Error executing read query.");

while($row = mysqli_fetch_object($result)){
	if(mysqli_num_rows($result)>0){
		$name = $row->name;
		echo $name . "<br />";
	}else{
		echo "No messages in the guestbook yet...";
	}
}

?>

Index.php
<html>
<head>
<title>Guestbook V1.1</title>
<script type="text/javascript">

// Used in showMessages NOT IN getXmlHttpObject()!!!
var xmlHttp;

function showMessages(){
	xmlHttp = getXmlHttpObject();
	if(xmlHttp == null){
		alert("Your browser does not support Javascript");
		return;
	}	
		
	var url = "showmessages.php";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function postMessage(){
	xmlHttp = getXmlHttpObject();
	if(xmlHttp == null){
		alert("Your browser does not support Javascript");
		return;
	}
	var name = document.postform.name.value;
	var email = document.postform.email.value;
	var message = document.postform.message.value;
	var url = "addmessage.php?name="+name+"&email="+email+"&message="+message;

	xmlHttp.onreadystatechange=statechanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function stateChanged(){
	if(xmlHttp.readyState == 4){
		document.getElementById("messages").innerHTML=xmlHttp.responseText
	}
	if(xmlHttp.readyState == 3){
		document.getElementById("messages").innerHTML="Loading3...";
	}
	if(xmlHttp.readyState == 2){
		document.getElementById("messages").innerHTML="Loading2...";
	}
	if(xmlHttp.readyState == 1){
		document.getElementById("messages").innerHTML="Loading1...";
	}
	if(xmlHttp.readyState == 0){
		document.getElementById("messages").innerHTML="Loading0...";
	}
}

function getXmlHttpObject(){
	var xmlHttp = null;
	try{
		//Firefox, opera, safari,...
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		try{
			//IE
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			//IE other
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

</script>
</head>
<body>

<h3>Guestbook V1.1</h3>
<div id="messages">
<script type="text/javascript">
showMessages();
</script>
</div>

<h3>Post a message</h3>
<div id="postform">
</div><form name="postform" id="postform" method="post" action="addmessage.php">
Name:*<br />
<input type="text" name="name" id="name" /><br />
Email:*<br />
<input type="text" name="email" id="email" /><br />
Message:*<br />
<textarea cols="60" rows="6" name="message" id="message"></textarea><br />
<input type="reset" value="Reset" />
<input type="submit" value="Post message" onclick="postMessage()" />
</form>
</div>

</body>
</html>


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
your AJAX sends the data with a GET and your PHP reads the POST:ed data. thats probably why it doesn't work.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Changed the 2 sends to POST and now it doesn't do anything anymore... :p
It just flickers my page and empties the form... no posts added to the DB...

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
how do you add your data by POST in AJAX now then?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Erhm... :p that's a good question... can you please explain this to me? :)

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
if you just change your php to accept get instad of post, and change back your ajax call to get, I think it will work.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Okay my script is like this now:

addmessage.php
<?php


require_once("config.php");


$name = $_GET['name'];

$email = $_GET['email'];

$message = $_GET['message'];


$query = "INSERT INTO posts (name, email, message) VALUES ('$name','$email', '$message')";


$result = mysqli_query($conn, $query) or diet("Error executing insert query.");


if($result){

	echo "Message added";

}


?>

showmessages.php
<?php


require_once("config.php");


$query = "SELECT * FROM posts ORDER BY id DESC";


$result = mysqli_query($conn, $query) or die("Error executing read query.");


while($row = mysqli_fetch_object($result)){

	if(mysqli_num_rows($result)>0){

		$name = $row->name;

		echo $name . "<br />";

	}else{

		echo "No messages in the guestbook yet...";

	}

}


?>

index.php
<html>

<head>

<title>Guestbook V1.1</title>

<script type="text/javascript">


// Used in showMessages NOT IN getXmlHttpObject()!!!

var xmlHttp;


function showMessages(){

	xmlHttp = getXmlHttpObject();

	if(xmlHttp == null){

		alert("Your browser does not support Javascript");

		return;

	}	

		

	var url = "showmessages.php";

	xmlHttp.onreadystatechange=stateChanged;

	xmlHttp.open("GET", url, true);

	xmlHttp.send(null);

}


function postMessage(){

	xmlHttp = getXmlHttpObject();

	if(xmlHttp == null){

		alert("Your browser does not support Javascript");

		return;

	}

	var name = document.postform.name.value;

	var email = document.postform.email.value;

	var message = document.postform.message.value;

	var url = "addmessage.php?name="+name+"&email="+email+"&message="+message;

	xmlHttp.onreadystatechange=statechanged;

	xmlHttp.open("GET", url, true);

	xmlHttp.send(null);

}


function stateChanged(){

	if(xmlHttp.readyState == 4){

		document.getElementById("messages").innerHTML=xmlHttp.responseText

	}

	if(xmlHttp.readyState == 3){

		document.getElementById("messages").innerHTML="Loading3...";

	}

	if(xmlHttp.readyState == 2){

		document.getElementById("messages").innerHTML="Loading2...";

	}

	if(xmlHttp.readyState == 1){

		document.getElementById("messages").innerHTML="Loading1...";

	}

	if(xmlHttp.readyState == 0){

		document.getElementById("messages").innerHTML="Loading0...";

	}

}


function getXmlHttpObject(){

	var xmlHttp = null;

	try{

		//Firefox, opera, safari,...

		xmlHttp = new XMLHttpRequest();

	}catch(e){

		try{

			//IE

			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

		}catch(e){

			//IE other

			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

		}

	}

	return xmlHttp;

}


</script>

</head>

<body>


<h3>Guestbook V1.1</h3>

<div id="messages">

<script type="text/javascript">

showMessages();

</script>

</div>


<h3>Post a message</h3>

<div id="postform">

</div><form name="postform" id="postform" method="post">

Name:*<br />

<input type="text" name="name" id="name" /><br />

Email:*<br />

<input type="text" name="email" id="email" /><br />

Message:*<br />

<textarea cols="60" rows="6" name="message" id="message"></textarea><br />

<input type="reset" value="Reset" />

<input type="submit" value="Post message" onclick="postMessage()" />

</form>

</div>


</body>

</html>


Still just flickers the page and doesn't add anything to the database.
Like the script isn't even called... :s

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
does it add when you run addmessage.php by hand with the right parameters?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#9
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Yes :)

#10
oldhendra

oldhendra

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
May I?
Just few comments for index.php, what if:

1) In function postMessage(), line
xmlHttp.onreadystatechange=statechanged,
be written as:
xmlHttp.onreadystatechange=stateChanged;

2) In function stateChanged(), line
if(xmlHttp.readyState == 4){
document.getElementById("messages").innerHTML=xmlHttp.responseText
be written as:
if(xmlHttp.readyState == 4 && xmlHttp.status==200 ){
document.getElementById("messages").innerHTML=xmlHttp.responseText;

3) Change
<input type="submit" value="Post message" onclick="postMessage()" />
into:
<input type="button" value="Post message" onclick="postMessage()" />

#11
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Lol it was the letter C in stateChanged that did the trick ;)
But when I change step 2 or 3 like you suggest it calls up the php page and inserts it right there in the page :)
So I left it "submit" in the button.
Thx!!!