Jump to content

trying to learn invoice creater


praveen198830

Recommended Posts

for some reason submit button doesnt work...when  i click nothing happens. what am i doing wrong..can someone help please

index.html page


 

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset='utf-8' /> 
    <title>Invoice Form</title> 
    <link rel="stylesheet" href="style.css" /> 
</head> 
<body> 
<div id="wrap"><div> 
    <h1>Checkout</h1> 
    <form method="post" action="create_reciept.php"> 
 
   
<fieldset> 
    <legend>Personal Information</legend> 
    <div class="col"> 
        <p> 
            <label for="name">Name</label> 
            <input type="text" name="name" value="John Doe" /> 
        </p> 
        <p> 
            <label for="email">Email Address</label> 
            <input type="text" name="email" value="joh@doe.com" /> 
        </p> 
    </div> 
    <div class="col"> 
        <p> 
            <label for="address">Address</label> 
            <input type="text" name="address" value="123 Main Street" /> 
        </p> 
        <p> 
            <label for="city">City</label> 
            <input type="text" name="city" value="Toronto" /> 
        </p> 
        <p> 
            <label for="province">Province</label> 
            <input type="text" name="province" value="Ontario" /> 
        </p> 
        <p> 
            <label for="postal_code">Postal Code</label> 
            <input type="text" name="postal_code" value="A1B 2C3" /> 
        </p> 
        <p> 
            <label for="country">Country</label><input type="text" name="country" value="Canada" /> 
        </p> 
    </div> 
</fieldset>
<fieldset> 
    <legend>Purchases</legend> 
    <table> 
        <thead> 
            <tr><td>Product</td><td>Price</td></tr> 
        <thead> 
        <tbody> 
            <tr> 
                <td><input type='text' value='a neat product' name='product[]' /></td> 
                <td>$<input type='text' value='10.00' name='price[]' /></td> 
            </tr> 
            <tr> 
                <td><input type='text' value='something else cool' name='product[]' /></td> 
                <td>$<input type='text' value='9.99' name='price[]' /></td> 
            </tr> 
            <tr> 
                <td><input type='text' value='buy this too!' name='product[]' /></td> 
                <td>$<input type='text' value='17.85' name='price[]' /></td> 
            </tr> 
            <tr> 
                <td><input type='text' value='And finally this' name='product[]' /></td> 
                <td>$<input type='text' value='5.67' name='price[]' /></td> 
            </tr> 
        </tbody> 
    </table> 
</fieldset>


<button type="submit">Submit Order</button>


</form> 
</div></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> 
<script type="text/javascript"> 
$('button').click(function () { 
    $.post('create_reciept.php', $('form').serialize(), function () { 
        $('div#wrap div').fadeOut( function () { 
            $(this).empty().html("<h2>Thank you!</h2><p>Thank you for your order. Please <a href='reciept.pdf'>download your reciept</a>. </p>").fadeIn(); 
        }); 
    }); 
    return false; 
}); 
</script> 
</body>
</html>

-------------------

create_reciept.php

<?php


require('test/fpdf/fpdf.php');

class PDF_reciept extends FPDF { 
    function __construct ($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin = 40) { 
        $this->FPDF($orientation, $unit, $format); 
        $this->SetTopMargin($margin); 
        $this->SetLeftMargin($margin); 
        $this->SetRightMargin($margin); 
        $this->SetAutoPageBreak(true, $margin); 
    } 

}
function Header() { 
    $this->SetFont('Arial', 'B', 20); 
    $this->SetFillColor(36, 96, 84); 
    $this->SetTextColor(225); 
    $this->Cell(0, 30, "Nettuts+ Online Store", 0, 1, 'C', true); 
}

function Footer() { 
    $this->SetFont('Arial', '', 12); 
    $this->SetTextColor(0); 
    $this->SetXY(0,-60); 
    $this->Cell(0, 20, "Thank you for shopping at Nettuts+!", 'T', 0, 'C'); 
}


$pdf = new PDF_receipt(); 
$pdf->AddPage(); 
$pdf->SetFont('Arial', '', 12);

$pdf->SetY(100);


$pdf->Cell(100, 13, "Ordered By"); 

$pdf->SetFont('Arial', ''); 


$pdf->Cell(100, 13, $_POST['name']);


$pdf->SetFont('Arial', 'B', 12); 
$pdf->Cell(50, 13, 'Date'); 
$pdf->SetFont('Arial', ''); 
$pdf->Cell(100, 13, date('F j, Y'), 0, 1);


$pdf->SetX(140); 
$pdf->SetFont('Arial', 'I'); 
$pdf->Cell(200, 15, $_POST['address'], 0, 2); 
$pdf->Cell(200, 15, $_POST['city'] . ', ' . $_POST['province'], 0, 2); 
$pdf->Cell(200, 15, $_POST['postal_code'] . ' ' . $_POST['country']); 
$pdf->Ln(100);


$pdf->PriceTable($_POST['product'], $_POST['price']);

 function PriceTable($products, $prices) { 
    $this->SetFont('Arial', 'B', 12); 
    $this->SetTextColor(0); 
    $this->SetFillColor(36, 140, 129); 
    $this->SetLineWidth(1); 
    $this->Cell(427, 25, "Item Description", 'LTR', 0, 'C', true); 
    $this->Cell(100, 25, "Price", 'LTR', 1, 'C', true); 
 
    $this->SetFont('Arial', ''); 
    $this->SetFillColor(238); 
    $this->SetLineWidth(0.2); 
    $fill = false; 
 
    for ($i = 0; $i < count($products); $i++) { 
        $this->Cell(427, 20, $products[$i], 1, 0, 'L', $fill); 
        $this->Cell(100, 20, '$' . $prices[$i], 1, 1, 'R', $fill); 
        $fill = !$fill; 
    } 
    $this->SetX(367); 
    $this->Cell(100, 20, "Total", 1); 
    $this->Cell(100, 20, '$' . array_sum($prices), 1, 1, 'R'); 
}

$pdf->Ln(50);

$message = "Thank you for ordering at the Nettuts+ online store. Our policy is to ship your materials within two business days of purchase. On all orders over $20.00, we offer free 2-3 day shipping. If you haven't received your items in 3 busines days, let us know and we'll reimburse you 5%.\n\nWe hope you enjoy the items you have purchased. If you have any questions, you can email us at the following email address:"; 
$pdf->MultiCell(0, 15, $message);

$pdf->SetFont('Arial', 'U', 12); 
$pdf->SetTextColor(1, 162, 232); 
 
$pdf->Write(13, "store@nettuts.com", "mailto:example@example.com");

    
$pdf->Output('reciept.pdf', 'F');


?>

 

 

Link to comment
Share on other sites

You have a form with action=create_receipt.php which you are submitting with a submit button.

That same button has a click event which sends an ajax request to create_receipt.php

You need to make up your mind exactly how you intend to process the form data.

Link to comment
Share on other sites


<form>
...
</form>

<button>Submit Order</button>

I got it to work by moving it outside the form and letting send the ajax request.

Your class was a bit messed up. Three class methods were defined as stand-alone functions outside the class definition and your spelling of "receipts" with the class was haphazard - sometimes "ie" and sometimes "ei". I also had to change your call to the parent constructor.

Revised php:

<?php

require('test/fpdf/fpdf.php');

class PDF_receipt extends FPDF { 
    function __construct ($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin = 40) { 
        parent::__construct($orientation, $unit, $format); 
        $this->SetTopMargin($margin); 
        $this->SetLeftMargin($margin); 
        $this->SetRightMargin($margin); 
        $this->SetAutoPageBreak(true, $margin); 
    } 

    function Header() { 
        $this->SetFont('Arial', 'B', 20); 
        $this->SetFillColor(36, 96, 84); 
        $this->SetTextColor(225); 
        $this->Cell(0, 30, "Nettuts+ Online Store", 0, 1, 'C', true); 
    }

    function Footer() { 
        $this->SetFont('Arial', '', 12); 
        $this->SetTextColor(0); 
        $this->SetXY(0,-60); 
        $this->Cell(0, 20, "Thank you for shopping at Nettuts+!", 'T', 0, 'C'); 
    }
 function PriceTable($products, $prices) { 
    $this->SetFont('Arial', 'B', 12); 
    $this->SetTextColor(0); 
    $this->SetFillColor(36, 140, 129); 
    $this->SetLineWidth(1); 
    $this->Cell(427, 25, "Item Description", 'LTR', 0, 'C', true); 
    $this->Cell(100, 25, "Price", 'LTR', 1, 'C', true); 
 
    $this->SetFont('Arial', ''); 
    $this->SetFillColor(238); 
    $this->SetLineWidth(0.2); 
    $fill = false; 
 
    for ($i = 0; $i < count($products); $i++) { 
        $this->Cell(427, 20, $products[$i], 1, 0, 'L', $fill); 
        $this->Cell(100, 20, '$' . $prices[$i], 1, 1, 'R', $fill); 
        $fill = !$fill; 
    } 
    $this->SetX(367); 
    $this->Cell(100, 20, "Total", 1); 
    $this->Cell(100, 20, '$' . array_sum($prices), 1, 1, 'R'); 
}

}


$pdf = new PDF_receipt(); 
$pdf->AddPage(); 
$pdf->SetFont('Arial', '', 12);

$pdf->SetY(100);


$pdf->Cell(100, 13, "Ordered By"); 

$pdf->SetFont('Arial', ''); 


$pdf->Cell(100, 13, $_POST['name']);


$pdf->SetFont('Arial', 'B', 12); 
$pdf->Cell(50, 13, 'Date'); 
$pdf->SetFont('Arial', ''); 
$pdf->Cell(100, 13, date('F j, Y'), 0, 1);


$pdf->SetX(140); 
$pdf->SetFont('Arial', 'I'); 
$pdf->Cell(200, 15, $_POST['address'], 0, 2); 
$pdf->Cell(200, 15, $_POST['city'] . ', ' . $_POST['province'], 0, 2); 
$pdf->Cell(200, 15, $_POST['postal_code'] . ' ' . $_POST['country']); 
$pdf->Ln(100);


$pdf->PriceTable($_POST['product'], $_POST['price']);

 $pdf->Ln(50);

$message = "Thank you for ordering at the Nettuts+ online store. Our policy is to ship your materials within two business days of purchase. On all orders over $20.00, we offer free 2-3 day shipping. If you haven't received your items in 3 busines days, let us know and we'll reimburse you 5%.\n\nWe hope you enjoy the items you have purchased. If you have any questions, you can email us at the following email address:"; 
$pdf->MultiCell(0, 15, $message);

$pdf->SetFont('Arial', 'U', 12); 
$pdf->SetTextColor(1, 162, 232); 
 
$pdf->Write(13, "store@nettuts.com", "mailto:example@example.com");

    
$pdf->Output('receipt.pdf', 'F');


?>                                                                                  

You could have a problem if two users request a receipt at the same time - whose receipt.pdf will they get?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.