Exp 10
Exp 10
To Write a web services for finding what people think by asking 500 people’s opinion for
PROCEDURE:
PROGRAM:
Index.php
<!doctype html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div class="wrap">
<?php
// retrive post
include('config.php');
include ('function.php');
dbConnect();
$query = mysql_query(
'SELECT *
FROM post
$row = mysql_fetch_array($query);
?>
<div class="post">
</div>
<?php
$comment_query = mysql_query(
"SELECT *
FROM comment
LIMIT 15");
55
?>
<h2>Comments.....</h2>
<div class="comment-block">
<div class="comment-item">
<div class="comment-avatar">
</div>
<div class="comment-post">
</div>
</div>
<?php endwhile?>
</div>
<label>
<span>Name *</span>
</label>
<label>
<span>Email *</span>
</label>
<label>
here...." required></textarea>
</label>
</form>
</div>
</body>
</html>
Ajax_Comment.php
<?php
include('function.php');
dbConnect();
$name = mysql_real_escape_string($_POST['name']);
$mail = mysql_real_escape_string($_POST['mail']);
$comment = mysql_real_escape_string($_POST['comment']);
$postId = mysql_real_escape_string($_POST['postid']);
56
mysql_query("
?>
<div class="comment-item">
<div class="comment-avatar">
</div>
<div class="comment-post">
</div>
</div>
<?php
dbConnect(0);
endif?>
Config.php
<?php
# db configuration
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'dbname');
?>
Function.php
<?php
/**
* @param bool
*/
function dbConnect($close=true){
if (!$close) {
mysql_close($link);
return true;
if (!mysql_select_db(DB_NAME, $link))
return false;
/**
* gravatar Image
* @see http://en.gravatar.com/site/implement/images/
*/
57
return $url;
?>
Style.CSS
/* general styling */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
-o-font-smoothing: antialiased;
font-smoothing: antialiased;
text-rendering: optimizeLegibility;
body{
text-transform: inherit;
color: #333;
background: #e7edee;
width: 100%;
.wrap{
width: 720px;
background: white;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
overflow: hidden;
h1{
font-size: 2.8em;
text-align: center;
margin: 25px 0;
h3{
font-size: 1.2em;
margin: 5px 0;
h3 span{
58
font-weight: normal;
font-size: 1em;
}
.item{
clear: both;
margin:0;
padding: 10px;
overflow: hidden;
.item:hover{background: #f9f9f9}
.post{
padding: 10px 0;
.comment-block{
.comment-item{
overflow: hidden;
width: 500px;
clear: both;
padding: 10px;
border-radius: 5px;
margin: 5px;
.comment-avatar{
width: 60px;
float: left;
}
.comment-avatar img{
width: 60px;
height: 60px;
border-radius: 5px;
.comment-post{
width: 400px;
float: left;
#form{
clear: both;
margin: 10px;
width: 500px;
/* form styling */
input[type="text"],
input[type="email"],
input[type="tel"],
input[type="url"],
59
textarea {
width:100%;
background: #fff;
font-size: 13px;
line-height: 20px;
margin: 0;
margin:0 0 5px;
border-radius:5px;
textarea {
height:100px;
max-width:100%;
input[type="submit"] {
cursor:pointer;
width:100%;
border:none;
background:#991D57;
color:#FFF;
margin:0 0 5px;
padding:10px;
border-radius:5px;
input[type="submit"]:hover {
input[type="submit"]:active {
input:focus,
textarea:focus {
outline:0;
label{
display: block;
margin: 5px 0;
font-weight: 900;
cursor: pointer;
60
.alert{
display: none;
margin: 20px 0;
color: #468847;
background-color: #dff0d8;
border-color: #d6e9c6;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
Script.js
$(document).ready(function(){
form.on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
beforeSend: function(){
submit.val('Submitting...').attr('disabled', 'disabled');
},
success: function(data){
$('.comment-block').append(item);
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
});
});
});
OUTPUT:
RESULT:
Thus a web services for finding what people think by asking 500 people’s opinion for any