Jump to content
praveen198830

trying to learn invoice creater

Recommended Posts

Posted (edited)

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');


?>

 

 

Edited by Barand
code tags

Share this post


Link to post
Share on other sites

Put your code in code boxes , please (<> button in the toolbar). I have done it for you this time.

Share this post


Link to post
Share on other sites

Try putting your button outside the form

<form>
   ...
</form> 

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

 

Share this post


Link to post
Share on other sites
10 hours ago, Barand said:

Try putting your button outside the form


<form>
   ...
</form> 

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

 

thank you.. tried that and didnt work :( 

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.