Friday, June 24, 2011

IMPORTANT NOTICE

Hello to everyone, To those who have been following my tutorials. I have create a fan page on facebook so you guys can give a feed backs on my tutorials and say what you like and what you dont like and what kind of tutorials you want more and the kind stuff you want to learn more.
Go to http://www.facebook.com/pages/Phpcoderboard/209608885746800?sk=wall and join.

Thursday, June 23, 2011

TUTORIAL COMMENT SYSTEM WITH ALLOW AND DENY PUBLISHING FUNCTIONALITY

first create table and call it "comment" with 4 fields id, name, comment and status mysql.php  <?php  @mysql_connect("localhost","root","") or die ("could not connect to mysql"); @mysql_select_db("name of your database") or die ("no database"); ?> ///////////page end here
index.php
<?php 
include_once "mysql.php";
//////render data that is already stored in the data base
 $sql_comments = mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($row = mysql_fetch_array($sql_comments)){
$name = $row["name"];
$comment = $row["comment"];
$commentlist .= 'name : '.$name.'<br />comment : '.$comment.'<hr>';
}
//////////////
?>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('#loading').hide();
});
function wall(){
$('#loading').show();
  var name = $('#name').val();///#name is the input id from the form and will be equal the variable "name"
  var comment = $('#comment').val();/////#comment is textarea id from the form and will be equal the variable "comment"
  var URL = "post.php"; /////post.php will be equal the variable "comment"
  
  $.post(URL,{wall:"post",name1:name,comment1:comment},function(data){//parameter wall will be equal "post", name1 will be equal to the var "name" and comment1 will be equal to the var "comment"
  if($('#name').val() != "" && $('#comment').val() != ""){//if there is a value for name and comment
  $("#notice").show();//show the div with id = notice
  $("#notice").fadeOut(4000);//fade out the div with id = notice within 4 seconds
  }
  $('#loading').hide();
  var name = $('#name').val("");
  var comment = $('#comment').val("");
  });
 }
</script>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div id='form'><form>
    name<br />
      <input type="text" id="name" />
      <br />
   comment<br />
      <textarea id="comment" cols="45" rows="5"></textarea>
      <br />
      <input type="submit" name="button" id="button" value="Submit" onclick="return false" onmousedown="javascript:wall();"/><img src="loading.gif" alt="" width="15" height="15" id="loading" />
      <br /></form>
    </div>
      <div id="result"><?php print"$commentlist";?></div></td>
  </tr>
</table>
post.php 
<?php
include_once "mysql.php";
  if ($_POST['wall'] == "post"){
  $name = $_POST['name1'];//name1 is parameter from the jquery function  
  $comment = $_POST['comment1'];//comment1 is parameter from the jquery function  
   if ($comment=="" || $name==""){exit();}// if no name or comment exit the script
   else{
   //insert the name and comment into database as pending 
  $sql = mysql_query("INSERT INTO comment (name,comment) 
     VALUES('$name','$comment','0')")
     or die (mysql_error());
  }
}
else if ($_POST['wall'] == "allow"){
   $comid = $_POST['cid'];
//set comment from pendded to published
   mysql_query("UPDATE comment SET status='1' WHERE id='$comid'") or die(mysql_error());
   echo"comment has been published.";
   }
   else if ($_POST['wall'] == "deny"){
   $comid = $_POST['cid'];
//delete comment if dennied
   mysql_query("DELETE FROM comment WHERE id='$comid'") or die(mysql_error());
   echo"comment has been deleted.";
   }
?>
admin.php
<?php 
include_once "mysql.php";
//////render data that is already stored in the data base
 $sql_comments = mysql_query("SELECT * FROM comment WHERE status='0' ORDER BY id DESC");
while($row = mysql_fetch_array($sql_comments)){
    $id = $row["id"];
$name = $row["name"];
$comment = $row["comment"];
$commentlist .= '<div id="'.$id.'"><table width="700" border="0" cellspacing="0" cellpadding="0">//we are going to the comment id as div id
  <tr>
    <td width="536" align="left" valign="top">name : '.$name.'<br />comment : '.$comment.'</td>
    <td width="150" align="left" valign="top">
<a id="comment'.$id.'" href="#" onclick="return false" onmousedown="javascript:allow('.$id.');">allow</a>
<a id="comment'.$id.'" href="#" onclick="return false" onmousedown="javascript:deny('.$id.');">deny</a></td>
  </tr>
</table></div><hr>';
}
//////////////
?>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">
  function allow(id){//if admin want to publish
  
  var URL = 'post.php';
  $.post(URL,{wall:"allow",cid:id},function(data){
  $('#'+id).html(data);
  });
  }
  function deny(id){//if admin want to delete
  
  var URL='post.php';
  $.post(URL,{wall:"deny",cid:id},function(data){
  $('#'+id).html(data);
  });
  }
</script>
<style type="text/css">
<!--
.style {
font-size: 16px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
color: #333333;
}
-->
</style>
<span class="style">Pending comments</span>
<hr>
<?php print"$commentlist"; ?>

Monday, June 20, 2011

TUTORIAL:CREATE A LOGIN FORM VALIDATED WITH PHP AND JQUERY

CREATE  TABLE CALLED members WITH 5 FIELDS id firstname lastname email password
INDEX.PHP

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">

function login(){
  var email = $('#email').val();//$('#email').val() is the value of the input field with id = email so we are goning to say var email will equal the value of that input
  var pass = $('#pass').val();//$('#pass').val() is the value of the input field with id = pass so we are goning to say var pass will equal the value of that input
  var URL = "parse.php";

  $.post(URL,{action:"login",logname:email,logpass:pass},function(data){
  $("#result").html(data).show();
  });
 }

</script>
<style type="text/css">
<!--
.font {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
}
.result{
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
 font-weight: bold;
 color: #CC0000;
}
.txtstyle{
 height: 25px;
 width: 200px;
 border-top-width: 2px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-top-style: solid;
 border-right-style: solid;
 border-bottom-style: solid;
 border-left-style: solid;
 border-top-color: #DDDDDD;
 border-right-color: #DDDDDD;
 border-bottom-color: #DDDDDD;
 border-left-color: #DDDDDD;
}
.loginbutton {
 clear: left;
}
-->
</style>

<table width="458" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="261" height="117" align="left" valign="bottom">log in </td>
    <td width="291" align="left" valign="bottom">sgin up</td>
  </tr>
  <tr>
    <td height="155" align="left" valign="top"><span class="font">Email</span><br />
      <input class="txtstyle" type="text" id="email" />//CREATE INPUT WITH TYPE=TEXT AND ID=EMAIL
      <br />
      <span class="font">Password</span><br />
      <input class="txtstyle" type="password" id="pass" />//CREATE INPUT WITH TYPE=PASSWORD AND ID=PASS
      <br />
      <input type="button" class="loginbutton" id="login" onclick="return false" onmousedown="javascript:login();" value="login"/>//CREATE INPUT WITH TYPE=BUTTON AND ONECE THE BUTTON IS CLICKED JAVASCRIPT:LOGIN() WILL RUN
      <br />
      <div id="result" class="result"></div></td>
    <td width="291" align="left" valign="top">SIGN UP FORM WILL BE HERE</td>
  </tr>
</table>
PARSE.PHP
<?php
include_once "mysql.php";
if($_POST['action']=="login"){
$email1 = $_POST['logname'];//$email1 = $('#email').val();
$pass = $_POST['logpass'];//$pass =  $('#pass').val();
if($email1 ==""){echo'Please enter your email'; }//if $email1 has no value show Please enter your email
else{//if $email1 has value
$sql = mysql_query("SELECT * FROM members WHERE email='$email1'");
$email_check = mysql_num_rows($sql); //check if $email1 value exist in the table
if($email_check == 1){//if exist proceed
  
while($row = mysql_fetch_array($sql)){
$password = $row["password"];//get the password
if($pass == ""){echo 'Please enter your password';}//if $pass has no value show Please enter your password
else if(md5($pass) != $password){echo 'Your password is incorrect';}//if encrypted $pass value not equal to $password show Your password is incorrect
else{//if encrypted $pass value equal to $password the proceed
 //create sessions for the table values
$_SESSION['id']; 
$_SESSION['firstname']; 
$_SESSION['lastname'];  
$_SESSION['email']; 
echo'<script>window.location = "profile.php"</script>';
exit(); 
     }
    }
   }
   else if($email_check != 1){//if doesnt exist show Email is incorrect
   echo 'Email is incorrect';
   }
  }
}
else{echo'<script>window.location = "index.php"</script>';}
?>
PROFILE.PHP
<?php
session_start(); // Must start session first 
include_once "mysql.php";
if (isset($_SESSION['id'])) {  
$id = $_SESSION['id']; 
echo'you are logged in';
 }
else {echo 'you are not logged in';}
?>