andrew_biggart Posted May 27, 2009 Share Posted May 27, 2009 Ok what i am trying to do is basically i have to pages... one to read the comments and one to insert a comment for a simple guestbook. But i want the two pages to run off the same page. Im using the following code. read.js <?php //Set no caching header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); ?> var guestbook; window.addEvent('domready', function(){ guestbook = new GuestBook(); }); var GuestBook = new Class({ initialize: function(){ var req = new Request.JSON({ url: 'control.php?action=getPage', onSuccess: this.success }).send(); }, success: function(jArray){ jArray.each(function(post){ var post = new PostItem(post.id, post.name, post.comment); post.display().inject($('commentList')); }, this); } }); var PostItem = new Class({ initialize: function(id, name, comment){ this.id = id; this.name = name; this.comment = comment; }, display: function(){ var cont = new Element('div',{ 'class':'postItem', 'id':'post'+this.id }); var title = new Element('h3',{ 'class':'postTitle', 'text':this.name + ' says...' }); var comment = new Element('p',{ 'class':'postComment', 'text':this.comment }); title.inject(cont); comment.inject(cont); return cont; } }); readposts.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled 2</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <link href="layout.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/mootools.js"></script> <script type="text/javascript" src="js/read.php"></script> </head> <body> <div id="wrapper"> <div id="masthead"> Andrews Guestbook </div> <div id="top_nav"> <a href="readposts.php">Read posts</a> | <a href="insertpost.php">Insert Post</a> | <a href="editpost.php">Edit post</a> | <a href="deletepost.php">Delete Post</a> </div> <div id="page_content"> <h1>Andrews Guestbook</h1> <div id="commentList"> </div> </div> <div id="footer"> © Andrew Biggart 2009 -2010 </div> </div> </body> </html> and then we have the insert files insert.js <?php //Set no caching header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); ?> window.addEvent('domready',domReady); function domReady(){ var guestbook = new GuestBook(); } GuestBook = new Class({ initialize: function(){ $('newComment').addEvent('submit', this.addComment); }, addComment: function(e){ e.preventDefault(); var req = new Request({ url:'control.php?action=insertPost', onSuccess:commentAddSuccess }).post(this); function commentAddSuccess(idNo){ new Element('span',{ 'text':'inserted item ' + idNo }).inject($('newComment')); } }, }); and finally insertpost.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled 2</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <link href="layout.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/mootools.js"></script> <script type="text/javascript" src="js/insert.php"></script> </head> <body> <div id="wrapper"> <div id="masthead"> Andrews Guestbook </div> <div id="top_nav"> <a href="readposts.php">Read posts</a> | <a href="insertpost.php">Insert Post</a> | <a href="editpost.php">Edit post</a> | <a href="deletepost.php">Delete Post</a> </div> <div id="page_content"> <h1>Insert Post into Guestbook</h1> <form id="newComment" action="#"> <div><label>Name: </label> <input type="text" name="name"/> </div> <div><label>Comment: </label> <textarea name="comment"></textarea> </div> <div><input type="submit" value="Add comment"/></div> </form> </div> <div id="footer"> © Andrew Biggart 2009 -2010 </div> </div> </body> </html> I am after some advice as to how i would go about making both of these pages running off the same page. Thanks Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 27, 2009 Author Share Posted May 27, 2009 OK so i have tried doing it this way but the insert post part of it doesn't work, it adds the name and comment but in the address bar and not the database. Can someone please point me in the right direction? <?php //Set no caching header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); ?> window.addEvent('domready',domReady); function domReady(){ var guestbook = new GuestBook(); } GuestBook = new Class({ initialize: function(){ $('newComment').addEvent('submit', this.addComment); }, addComment: function(e){ e.preventDefault(); var req = new Request({ url:'control.php?action=insertPost', onSuccess:commentAddSuccess }).post(this); function commentAddSuccess(idNo){ new Element('span',{ 'text':'inserted item ' + idNo }).inject($('newComment')); } }, }); var guestbook; window.addEvent('domready', function(){ guestbook = new GuestBook(); }); var GuestBook = new Class({ initialize: function(){ var req = new Request.JSON({ url: 'control.php?action=getPage', onSuccess: this.success }).send(); }, success: function(jArray){ jArray.each(function(post){ var post = new PostItem(post.id, post.name, post.comment); post.display().inject($('commentList')); }, this); } }); var PostItem = new Class({ initialize: function(id, name, comment){ this.id = id; this.name = name; this.comment = comment; }, display: function(){ var cont = new Element('div',{ 'class':'postItem', 'id':'post'+this.id }); var title = new Element('h3',{ 'class':'postTitle', 'text':this.name + ' says...' }); var comment = new Element('p',{ 'class':'postComment', 'text':this.comment }); title.inject(cont); comment.inject(cont); return cont; } }); Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 27, 2009 Author Share Posted May 27, 2009 and my control.php is the following! <?php require_once 'php/class.GuestBook.php'; $guestbook = new GuestBook('Biggart_GuestbookT'); function insertPost() { global $guestbook; return $guestbook->insert($_POST); } function editPost() { global $guestbook; return $guestbook->update($_POST); } function getSinglePost(){ global $guestbook; return $guestbook->getPost($_GET['id']); } function getPage() { global $guestbook; return $guestbook->getPostList(); } function removePost(){ global $guestbook; return $guestbook->removePost($_GET['id']); } echo $_GET['action']();[code] [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.