Jump to content

Loois

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by Loois

  1. comment.sql

    CREATE TABLE `comment` (
      `user_id` int(8) NOT NULL,
      `photo_id` int(8) NOT NULL, 
      `comment` text NOT NULL,
      `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`user_id``photo_id`)
    );

    ajax_comment.php

    <?php
    // code will run if request through ajax
    if (isset( $_SERVER['HTTP_X_REQUESTED_WITH'] )):
      include('../config.php');
      // connecting to db
      dbConnect();
      
      if (!empty($_SESSION['user_id']) AND !empty($_POST['photo_id']) AND !empty($_POST['comment'])) {
        // preventing sql injection
        $user_id = $_SESSION['user'];
        $photo_id = $_POST['photo_id'];
        $comment = $_POST['comment'];
    
        // insert new comment into comment table
        $query = "INSERT INTO comment (user_id, photo_id, comment) VALUES('$user_id', '$photo_id', '$comment')");  
      }
    ?>
    <!-- sending response with new comment and html markup-->
    <div class="comment-item">
      <div class="comment-avatar">
            <a href="<?php echo $baseurl . "/" . $photo_username ?>"><img src="./core/getimg.php?profiloimg=<?php echo $photo_userid ?>" class="home-foto-profilofoto" /></a>  </div>
      <div class="comment-post">
        <h3><?php echo $photo_username . "/" . $user_id ?> <span>ha commentato:</span></h3>
        <p><?php echo $comment?></p>
      </div>
    </div>
    
    <?php
      // close connection
      dbConnect(0);
    endif?> 

    jQuery.js

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      var form = $('form');
      var submit = $('#submit');
    
      form.on('submit', function(e) {
        // prevent default action
        e.preventDefault();
        // send ajax request
        $.ajax({
          url: 'ajax_comment.php',
          type: 'POST',
          cache: false,
          data: form.serialize(), //form serizlize data
          beforeSend: function(){
            // change submit button value text and disabled it
            submit.val('Submitting...').attr('disabled', 'disabled');
          },
          success: function(data){
            // Append with fadeIn see http://stackoverflow.com/a/978731
            var item = $(data).hide().fadeIn(800);
            $('.comment-block').append(item);
    
            // reset form and button
            form.trigger('reset');
            submit.val('Submit Comment').removeAttr('disabled');
          },
          error: function(e){
            alert(e);
          }
        });
      });
    });
    </script> 

    home.php (Where there are my users posts)

    		
    		<!--  Commenti -->
    		
    	<form id="form" method="post">
        <!-- need to supply post id with hidden fild -->
        <input type="hidden" name="comment" value="1">
            <label>
          <a href="<?php echo $baseurl . "/" . $photo_username ?>"><img 	width="25"
    	height="25"src="./core/getimg.php?profiloimg=<?php echo $photo_userid ?>" class="home-foto-profilofoto" /></a>
    		</label>
    		<label>
            <a href="<?php echo $baseurl . "/" . $photo_username; ?>"><?php echo $photo_username; ?></a>
    
    		</label>
    	<label>
          <span>Commenta</span>
          <textarea name="comment" id="comment" cols="0" rows="0" placeholder="Scrivi un commento.." required></textarea>
        </label>
        <input type="submit" id="submit1" value="Submit Comment">
      </form>
      <?php 
    include ("ajax_comment.php");
    include ("jquery.js");
      ?>
          <!-- Fine commenti -->

     

  2. I actually had problems with comments.php i can not establish the 
    function, i form i already keep it alike but i can not create the 
    function behind. Can not you help me?
×
×
  • Create New...