Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Closed Thread
[AJAX/PHP guestbook] how to ?
Old 12-03-2008, 01:23 PM [AJAX/PHP guestbook] how to ?
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
This problem seems to return lots of times, but I can't finds a clear answer.

So what I want to do is:
- a form with multi input fields
- a button to send the information to a mysql database
- with a php script that manages the connection with the database, inserts the information and fetches it back.
- with an ajax script that calls that php script and prints the result back.
- no reload of the whole page, just in the section div

With what specific script can you do that?
There are lots of different libraries, do they do the same? Or do you only need a small part of that?

Maybe there needs to be a specific tutorial for that, where can I find it if it exists?


Ok, I have some code already:

The main one: guestbook.php
Code:
<html>
<head>
<script src="ajax.js"></script>
</head>
<body>

<div id="addgb">

<form>
<ul>
<li><input name="datum" id="datum" value="<?php echo date("d-m-Y"); ?>" type="hidden"></li>
<li><input name="tijd" id="tijd" value="<?php echo date("H:i:s"); ?>" type="hidden"></li>
<li><input name="ip" id="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" type="hidden"></li>
<li>Naam: <input name="naam" id="naam" maxlength="50"></li>
<li>E-mail: <input name="email" id="email" maxlength="50"></li>
<li>Website: <input name="website" id="website" maxlength="50"></li>
<li>Motto: <input name="motto" id="motto" maxlength="50"></li>
<li>Bericht: <textarea name="bericht" id="bericht" maxlength="600"></textarea></li>
<li><button type="submit" name="submit" value="submit" onclick="addGuests(this.value)">Verstuur</button></li>
</ul>
</form>

</div>

<div id="showgb">
<!-- the guestbook comments should come right here -->
</div>

</body>
</html>
The ajax.js file goes like this:

Code:
var xmlHttp

function addGuests(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="gbadd.php"
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}


function showGuests(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="gbshow.php"
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}


function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("showgb").innerHTML=xmlHttp.responseText 
 } 
}


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;
}
Further, the php codes of gbadd.php and gbshow.php:

gbadd.php:
Code:
<?php 
if(isset($_POST['submit'])) {

if((strlen($_POST['naam']) == 0) || (strlen($_POST['bericht']) == 0) || (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $_POST['email']))) 
  { 
           echo "it is not working fill in again";
  } 
else {

$datum = $_POST['datum'];
$tijd = $_POST['tijd'];
$ip = $_POST['ip'];
$bericht = $_POST['bericht'];
$bericht = strip_tags($bericht);
$bericht = htmlentities($bericht);
$email = $_POST['email'];
$email = strip_tags($email);
$email = htmlentities($email);
$website = $_POST['website'];
$website = strip_tags($website);
$website = htmlentities($website);
$motto = $_POST['motto'];
$motto = strip_tags($motto);
$motto = htmlentities($motto);
$naam = $_POST['naam'];
$naam = strip_tags($naam);
$naam = htmlentities($naam);
$bericht = str_replace("\n", "<br>", $bericht);

$sql = "INSERT INTO `trouwboek` ";
$sql .= "(ip, naam, email, website, motto, bericht, datum, tijd) ";
$sql .= "VALUES ";
$sql .= "('$ip', '$naam', '$email', '$website', '$motto', '$bericht', '$datum', '$tijd') ";
$sql .= "; ";

require_once('connectionfile.php');
$verbinding = @mysql_connect(MYSQL_SERVER, MYSQL_GEBRUIKERSNAAM, MYSQL_WACHTWOORD) or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error()); 

$toegevoegd = mysql_query($sql) or die(mysql_error());
mysql_close($verbinding);

 }
}

else {
  echo "Please, fill in the form above!<p><br>";
}
?>
gbshow.php:
Code:
<?php

require_once('connectionfile.php');
$verbinding = @mysql_connect(MYSQL_SERVER, MYSQL_GEBRUIKERSNAAM, MYSQL_WACHTWOORD) or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error()); 

$sql = "SELECT * FROM trouwboek ORDER BY id DESC LIMIT 0, 30"; 
$tonen = mysql_query($sql) or die (mysql_error());
 
while ($rij = mysql_fetch_assoc($tonen)) { 

$datum = $rij["datum"];
$tijd = $rij["tijd"];
$naam = $rij["naam"];
$email = $rij["email"];
$website = $rij["website"];
$motto = $rij["motto"];
$bericht = $rij["bericht"];


echo "<div id=\"kaderboven\"><ul><li><b>". $naam ."</b> plaatste dit bericht op ". $datum ." om ". $tijd .".</li></ul></div>";
echo "<div id=\"kadermidden\"><ul><li>". $bericht ."</li><li><i>- ". $motto ." -</i></li></ul></div>";
echo "<div id=\"kaderonder\"><ul><li><a href=\"". $website ."\" target=\"_blank\">". $website ."</a></li></ul></div>"; 


   }

mysql_free_result($tonen);
mysql_close($verbinding);

?>
But the thing is that it's not working. Nothing runs or loads or shows up.
What am I doing wrong?


The php guestbook code works perfect in a normal flow, when the whole page reload.
But I don't want the whole page to reload, because there's a music player in another separate div, and it may not reload.
That's why I use AJAX for only the typical section to reload.
__________________

Please login or register to view this content. Registration is FREE
Bulevardi is offline
View Public Profile
 
 
Register now for full access!
Old 12-03-2008, 05:17 PM Re: [AJAX/PHP guestbook] how to ?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
1. onclick="addGuests(this.value)" only sends the value for the submit button.
2. In the addGuests() function you don't do anything with the str that comes in.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
View Public Profile
 
Old 12-04-2008, 04:22 AM Re: [AJAX/PHP guestbook] how to ?
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
Than I have to do it with something like this?

<form action="javascript:ajaxpage('gbscript.php', 'content');" method="post">
... form fields...

I don't get it actually... you know any solution how I can fix these two problems?
__________________

Please login or register to view this content. Registration is FREE
Bulevardi is offline
View Public Profile
 
Old 12-04-2008, 09:52 AM Re: [AJAX/PHP guestbook] how to ?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I think you should put all the fields in a form and make the onclick a function which loops all the elements in the form and adds their names and variables to the URL as GET variables.
I'm not sure how to this, though, because I've always had problems with child elements. :P
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
View Public Profile
 
Closed Thread     « Reply to [AJAX/PHP guestbook] how to ?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14001 seconds with 12 queries