Month: March 2014

Jquery advance validations


// after seeing error when user start typing in the input box the message hides and border comes to original color 
$("#contest_title").keyup(function(){
   $("#contest_title_err").hide(0);
   $("#contest_title").css('border-color','#B2B2B4');
 });
 
 //call this method onclick of submit button
 function valid()
 {
 //check if contest title is not empty or you can use regular expression comparision also
		if($('contest_title').val == ''){
		$('contest_title_err').html('Please enter contest title');
		$('contest_title').css('border-color','#c43d3d');
		}
		else{
		$('contest_title_err').html('');
		$('contest_title').css('border-color','#B2B2B4');
		noerror[0] = 1;
		}
		
if(noerror[0] == 1 && so on for many fields)
			{
			return true;
			}
			else
			{
			return false;
			}
}



// the html for the fields is		
<input type="text" class="form-control" placeholder="Title" id="contest_title"  name="contest_title">
<label id="contest_title_err" class="message error"></label>

Using stripe for payment

1)To start… set up an account at https://stripe.com/gb, this is comletely free.
2)Once you’ve done this.. download the Stripe library from here https://stripe.com/docs/libraries
3)Go to your account and then account settings,then api keys…copy the one saying “API Keys” and use as key in code below.
4)

require_once('lib/Stripe.php');
Stripe::setApiKey("sk_test_your_key_here");
isset($_POST)) {
$payment = Stripe_Charge::create(array(
          'amount'        => $_POST['amount'],
          'currency'      => $_POST['currency'],
          'card'          => array(
          'number'    => $_POST['card'],
          'exp_month' => $_POST['expMonth'],
          'exp_year'  => $_POST['expYear'],
          'cvc'       => $_POST['cvc']
            ),
          'description'   => $_POST['description']
         )
);
print_r($payment);
}
 

5)Use the test card credentials
for example
currency = USD
card = 4242424242424242
expiry month = 06
expiry year = 2018
cvc = 564
The original tutorial is located at http://michaelbudd.org/tutorials/view/7/stripe-payment-gateway-tutorial-php
Thanks to Michael Budd

Simple shopping cart application using php,mysql,ajax,jquery

1)
Create table named `bb_prodducts` and fields
number_of_bucks,name,price_per_package,price_per_credit,number_of_bucks,id as auto increment
2)
create a file named packages.php and paste the code below

session_start();
 $title = "BabyBoomApps - Content Page";
$page_description = "BabyBoomApps is the place to help fund and gift a baby's future through contests, drawings and online fun!";
include("includes/header.php");
include('includes/checksession.php');
include('includes/connect.php');
$packs = mysql_query("SELECT * FROM `bb_packages`");


<!--breadcrumbs start-->
<div class="breadcrumbs">
  <div class="container">
    <div class="row">
      <div class="col-lg-4 col-sm-4">
        <h1 id="main_conhdr">MEMBERS</h1>
      </div>
      <div class="col-lg-8 col-sm-8">
        <ol class="breadcrumb pull-right">
          <li><a href="#">Home</a></li>
          <li class="active">BoomBucks
          <li>
        </ol>
      </div>
    </div>
  </div>
</div>
<!--breadcrumbs end-->
<!--container start-->
<div class="container">
  <div class="row">

    <div class="col-lg-9 about">
	<section class="credits-header">
          <div id="buy-credits-default">
            <h3>Buy BoomBucks</h3>
          
            <div class="clear"></div>
         
        </section>
     <div class="boombucks_section">
           
            <section class="tile-container smaller-boombucks_section">
			<?php while($pk = mysql_fetch_array($packs)) { ?>
             <section class="boombucks" >
                <header></header>
                <h3><?php echo $pk['number_of_bucks']; ?><BR><?php echo $pk['name']; ?> </h3>
                <span class="boombuck">$<?php echo ($pk['price_per_package']); ?>&nbsp;USD</span> <span class="boombucks-unit-price">($<?php echo $pk['price_per_credit']; ?>/credit)</span>
          
               
                 <span id="s<?php echo $pk['id']; ?>"> <input type="submit" name="submit" value="Buy Now" onclick="addtocart('<?php echo $pk['id']; ?>','<?php echo $pk['number_of_bucks']; ?>','<?php echo $pk['name']; ?>','<?php echo $pk['price_per_credit']; ?>','<?php echo $pk['price_per_credit']; ?>');" class="btn btn-info"  /></span>
           
              </section>
             <?php } ?>
            
            </section>
            <div class="clear"></div>
          
            
           
          </div>
		  <span class="row button-next btn btn-info" style="float:right;width:20%;" onclick="window.location.href='checkout.php'" id="proced_checkout">Proceed to Checkout</span>
    </div>
	
    <div class="col-lg-">
      <?php include("includes/sidebar-account.php"); ?>
    </div>
	
  </div>
  <div  class="row">
  </div>
</div>
<!--container end-->
<?php include("includes/footer.php"); ?>

<script>
function addtocart(id,numbucks,name,total,ppc)
{
$("#s"+id).html("Added!");
$.ajax({
     url:"add-to-cart.php",
	 type:"post",
	 data:{'crtidpck':id,'numbucks':numbucks,'name':name,'total':total,'ppc':ppc},
	 success:function(da){
	      alert('added');
		  
    }
});
}
</script>

3)The packages added to cart will be saved to session,use the file below add-to-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
/* unset($_SESSION['cart']);  */
$_SESSION['cart']['id'][] = $_POST['crtidpck'];
$_SESSION['cart']['numbucks'][] = $_POST['numbucks'];
$_SESSION['cart']['name'][] = $_POST['name'];
$_SESSION['cart']['total'][] = $_POST['total'];
$_SESSION['cart']['ppc'][] = $_POST['ppc'];
print_r($_SESSION['cart']);
?>


4)And the checkout file managing removal of items from cart and displaying them on the page on file checkout.php as follows


<?php
session_start();
 $title = "BabyBoomApps - Content Page";
$page_description = "BabyBoomApps is the place to help fund and gift a baby's future through contests, drawings and online fun!";
include("includes/header.php");
include('includes/checksession.php');
include('includes/connect.php');

?>

<!--breadcrumbs start-->
<div class="breadcrumbs">
  <div class="container">
    <div class="row">
      <div class="col-lg-4 col-sm-4">
        <h1 id="main_conhdr">MEMBERS</h1>
      </div>
      <div class="col-lg-8 col-sm-8">
        <ol class="breadcrumb pull-right">
          <li><a href="#">Home</a></li>
          <li class="active">BoomBucks
          <li>
        </ol>
      </div>
    </div>
  </div>
</div>
<!--breadcrumbs end-->
<!--container start-->
<div class="container">
  <div class="row">

    <div class="col-lg-9 about">
	<section class="credits-header">
          <div id="buy-credits-default">
            <h3>BoomBucks Cart</h3>
          
            <div class="clear"></div>
         
        </section>
     <div class="boombucks_section">
           
            <section class="tile-container smaller-boombucks_section">
			
			
			<!-------this section runs when cart is updated----------->
			<?php if($_POST['upcart']) { 
					if($_GET['index'])
					{
					$removeindex = $_GET['index'];
					unset($_POST[$removeindex]);
					$_POST = array_values($_POST);
					}
			?>
			<table style="width:100%">
			<thead style="border-bottom:1px dashed #ccc">
			<tr>
			<th>Package</th><th>Package Price</th><th>Quantity</th><th>Sub Total</th><th>&nbsp;</th>
			</tr>
			</thead>
			<tbody>
			
			<?php 
			//echo "<pre>";print_r($_SESSION['cart']['id']); print_r($_POST);
			$countt = count($_SESSION['cart']['id']);
			$total = 0;
			for($l=0;$l<$countt;$l++)
			{
			?>
			<tr>
			<td>
			<?php echo $_SESSION['cart']['numbucks'][$l];?>&nbsp;BoomBucks
			</td>
			
			<td>
			$<span id="ppc<?php echo $_SESSION['cart']['id'][$l];?>"><?php echo $_SESSION['cart']['ppc'][$l];?></span>
			</td>
			<td>
			<form action="" method="post">
			<input type="text" value="<?php echo $_POST[$l];?>" name="<?php echo $l ;?>" id="<?php echo $_SESSION['cart']['id'][$l];?>" style=" border: 1px solid #CCCCCC;
    height: 30px;
    width: 50px;"/>
			</td>
			<td>
			$<span id="s<?php echo $l;?>">
			
			<?php echo ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l])*($_POST[$l]);?>
			
			
			</span>
			</td>
			<td>
			<span class="removespan" id="re<?php echo $l;?>" onclick="removeitem('<?php echo $l;?>')">Remove</span>
			</td>
			</tr>
			<?php 
			$total+=  ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l])*($_POST[$l]);
			} ?>
			<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>Grand Total</td><td>$<span id="grand_total"><?php echo $total; ?></span></td><td>&nbsp;</td></tr>
			</tbody>
			</table>
					
            <?php }else { ?>
			<!------------------>
		
			
			<table style="width:100%">
			<thead style="border-bottom:1px dashed #ccc">
			<tr>
			<th>Package</th><th>Package Price</th><th>Quantity</th><th>Sub Total</th><th>&nbsp;</th>
			</tr>
			</thead>
			<tbody>
			
			<?php 
			
			$countt = count($_SESSION['cart']['id']);
			$total = 0;
			for($l=0;$l<$countt;$l++)
			{
			?>
			<tr>
			<td>
			<?php echo $_SESSION['cart']['numbucks'][$l];?>&nbsp;BoomBucks
			</td>
			
			<td>
			$<span id="ppc<?php echo $_SESSION['cart']['id'][$l];?>"><?php echo $_SESSION['cart']['ppc'][$l];?></span>
			</td>
			<td>
			<form action="" method="post">
			<input type="text" value="1" name="<?php echo $l;?>" id="<?php echo $_SESSION['cart']['id'][$l];?>" style=" border: 1px solid #CCCCCC;
    height: 30px;
    width: 50px;"/>
			</td>
			<td id="sub<?php $l;?>">
			$<span id="s<?php echo $l;?>"><?php echo ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l]);?></span>
			</td>
			<td>
			<span class="removespan" id="re<?php echo $_SESSION['cart']['id'][$l];?>" onclick="removeitem('<?php echo $l;?>')">Remove</span>
			</td>
			</tr>
			<?php 
			$total+=  ($_SESSION['cart']['numbucks'][$l])*($_SESSION['cart']['ppc'][$l]);
			} ?>
			<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>Grand Total</td><td>$<span id="grand_total"><?php echo $total; ?></span></td><td>&nbsp;</td></tr>
			</tbody>
			</table>
			<?php } ?>
            </section>
            <div class="clear"></div>
          
            
           
          </div>
		  <br/><br/>
		  <input type="submit" name="upcart" class="row button-next btn btn-info" value="Update Cart" style="float:left;width:20%;"></form><span class="row button-next btn btn-info" style="float:left;width:20%;margin-left:5px;" onclick="removeall();">Remove All</span><span class="row button-next btn btn-info" style="float:right;width:20%;" onclick="window.location.href='checkout.php'" id="proced_checkout">Proceed to Checkout</span>
    </div>
	
    <div class="col-lg-">
      <?php include("includes/sidebar-account.php"); ?>
    </div>
	
  </div>
  <div  class="row">
  </div>
</div>
<!--container end-->
<?php include("includes/footer.php"); ?>


<script>
function addtocart(id)
{
$.ajax({
     url:"add-to-cart.php",
	 type:"post",
	 data:{'crtidpck':id},
	 success:function(da){
	      alert('added');
		  $("#s"+id).html("Added!");
    }
});
}
function removeitem(id)
{

var subtotal = $("#s"+id).text();

var grand_total = $("#grand_total").text();
var new_grand_total = parseInt(grand_total) - parseInt(subtotal);
$("#grand_total").text(new_grand_total);
$("#re"+id).parent("td").parent("tr").hide(0);
 $.ajax({
     url:"remove-from-cart.php",
	 type:"post",
	 data:{'crtidpck':id},
	 success:function(da){
	      //alert('Removed!');
		  window.location.href="checkout.php?index="+id;
		 // $("#s"+id).html("Removed!");
    }
}); 
}
function removeall()
{
 $.ajax({
     url:"remove-all-from-cart.php",
	 type:"post",
	 success:function(da){
	     $("tbody tr:last").prevAll("tr").hide(0);
		 $("#grand_total").text("00.00");
    }
}); 
}
</script>

5)The other ajax request files used in checkout.php are
a)remove-from-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
$index = $_POST['crtidpck'];

unset($_SESSION['cart']['id'][$index]);
$_SESSION['cart']['id'] = array_values($_SESSION['cart']['id']);

unset($_SESSION['cart']['numbucks'][$index]);
$_SESSION['cart']['numbucks'] = array_values($_SESSION['cart']['numbucks']);

unset($_SESSION['cart']['name'][$index]);
$_SESSION['cart']['name'] = array_values($_SESSION['cart']['name']);

unset($_SESSION['cart']['total'][$index]);
$_SESSION['cart']['total'] = array_values($_SESSION['cart']['total']);

unset($_SESSION['cart']['ppc'][$index]);
$_SESSION['cart']['ppc'] = array_values($_SESSION['cart']['ppc']);


?> 

b)remove-all-from-cart.php

<?php
if(!session_id()){
	session_start();
}
include('includes/checksession.php');
unset($_SESSION['cart']);
?>

Get a cars’ general information using registration number curl

You need to download simple_html_dom parser from here http://simplehtmldom.sourceforge.net/
or just google for it


loadHTML($scrapped_website);
 
$items = $doc->getElementsByTagName('td');

    
       $make = explode('Make',$items->item(1)->nodeValue);
	   print_r($make[1]);echo "<br>";
	   
       $other = explode('Colour', $items->item(2)->nodeValue);
	   print_r($other[1]);echo "<br>";
	   $model = explode('Model',$other[0]);
	   print_r($model[1]);echo "<br>";
		
?>

Json encode ,decode single row fetched from database


function abc(id)
{

	$("#id_of_registry").val(id);
	
	$.ajax({
         url:"get-ship-info.php",
	 type:"post",
	 dataType :"json",
	 data:{'id':id},
	 success:function(da){
	 
	 if(da == "1")
	 {
	 $("#addpro").show(0);
	 }
	 else{
	   $("#fname").val(da[0].fname);
           $("#lname").val(da[0].lname);
           $("#email").val(da[0].email);
           $("#phone1").val(da[0].phone1);
           $("#phone2").val(da[0].phone2);
           $("#phone3").val(da[0].phone3);
           $("#address").val(da[0].address);
           $("#city").val(da[0].city);
           $("#state").val(da[0].state);
           $("#zip").val(da[0].zip);
           $("#addpro").show(0);
  }
    }
});
}

in php file


$phone = $_POST['phone1']."-".$_POST['phone2']."-".$_POST['phone3'];
$out = mysql_query("select * from `bb_shipping_addresses` where `reg_id` = '".$_POST['id']."'");
$add = mysql_fetch_array($out);
$ph = explode('-',$add['phone']);
$num = mysql_num_rows($out);
$info = array();
if($num =="1"){
		$info[] = array( 'id' => $add['reg_id'],'fname'=>$add['fname'],'lname'=>$add['lname'],'email'=>$add['email'],'phone1'=>$ph[0],'phone2'=>$ph[1],'phone3'=>$ph[2],'address'=>$add['address'],'city'=>$add['city'],'state'=>$add['state'],'zip'=>$add['zip'] );
		echo json_encode($info);
}
else
{
		echo "1";
}