Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Can I haz Jerb?
Monday, May 7th, 2012 at 9:55:24pm UTC 

  1. <html>
  2. <head>
  3. <script src="/prototype.js" type="text/javascript"></script>
  4. <script src="/underscore.js" type="text/javascript"></script>
  5. <!--<link rel="stylesheet" href="/assets/css/temp.css" media="screen">-->
  6. <?php
  7.     function addItem($object){
  8.     }
  9.     function getItems(){
  10.         return array();
  11.     }
  12.    
  13.     $from = $_POST['from'];
  14.     $comment = $_POST['comment'];
  15.     if(trim($from) !== '' && trim($comment) !== ''){
  16.         $object = new stdClass;
  17.         $object->from = $from;
  18.         $object->comment = $comment;
  19.         addItems($object);
  20.     }else{
  21. ?>
  22. <script>
  23. var comment_list = [
  24.     {
  25.         'from':'Test1',
  26.         'body':'test1comment'
  27.     },
  28.     {
  29.         'from':'Test2',
  30.         'body':'test2comment'
  31.     }
  32. <?php
  33.     $comments = getItems();
  34.     $items = '';
  35.     foreach($comments as $comment){
  36.         if($items !== ''){
  37.             $items .= ",";
  38.         }
  39.         $items .= "{
  40.             'from':".htmlspecialchars($comment->from, ENT_QUOTES).",
  41.             'body':".htmlspecialchars($comment->body, ENT_QUOTES)."
  42.         }\n";
  43.     }
  44.     echo $items;
  45. ?>
  46. ];
  47.  
  48. function submitComment(){
  49.     var cf = $('commentForm');
  50.     var params = {
  51.         "from": cf.from.value,
  52.         "body": cf.body.value
  53.     };
  54.     Ajax.Request(
  55.         "guestbook.php",
  56.         {
  57.             method:    'post',
  58.             parameters: params,
  59.             onSuccess:  function() {
  60.                 var item = createCommentBlock(params);
  61.                 $('commentsList').appendChild(item);
  62.                 cf.from.value = '';
  63.                 cf.comment.value = '';
  64.             },
  65.             onFailure:  function() {
  66.                 console.log('There was an error when submitting your comment.');
  67.             }
  68.         }
  69.     );
  70. }
  71. function createCommentBlock(comment){
  72.     var commentDiv = new Element('div',{'class':'comment'});
  73.  
  74.     var commentFrom = new Element('div',{'class':'comment from'});
  75.     commentFrom.innerHTML = comment.from;
  76.     commentDiv.appendChild(commentFrom);
  77.  
  78.     var commentBody = new Element('div',{'class':'comment body'});
  79.     commentBody.innerHTML = comment.body;
  80.     commentDiv.appendChild(commentBody);
  81.  
  82.     return commentDiv;
  83. }
  84. function createCommentForm(){
  85.     var formBlock = new Element('div',{'class':'commentForm'});
  86.    
  87.     formBlock.innerHTML = '<h1>Submit your comment here</h1>' +
  88.     '<form id="commentForm" method="POST" action="guestbook.php">' +
  89.     '   <label>From: </label>' +
  90.     '   <input type="text" name="from"/><br/>' +
  91.     '   <label>Your comment:</label>' +
  92.     '   <textarea name="body"></textarea><br/>' +
  93.     '   <input type="button" value="Send my comment" onclick="submitComment();"/>' +
  94.     '</form>';
  95.  
  96.     return formBlock;
  97. }
  98. function init(){
  99.     var body = $$('body')[0];
  100.     var commentsList = new Element('div',{'id':'commentsList'});
  101.     body.appendChild(commentsList);
  102.     _.each(comment_list, function(comment){
  103.         var item = createCommentBlock(comment);
  104.         commentsList.appendChild(item);
  105.     });
  106.     var commentForm = createCommentForm();
  107.     body.appendChild(commentForm);
  108. }
  109. </script>
  110. <?php
  111.     }
  112. ?>
  113. </head>
  114. <body onload='init();'></body>
  115. </html>

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right