hello,
I have a code to send my form with dompdf to a pdf file. Everything works except the php result. I can't display the php result of $total on my pdf.. Hope someone can help me!! You can find my code if you follow the link





<?php
if (!empty($_POST)) {

// Used for later to determine result
$success = $error = false;

// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;

// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));

// Check for blank fields
if (empty($post->name) OR empty($post->email))
$error = true;

else {

// Get this directory, to include other files from
$dir = dirname(__FILE__);

// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf.php');
$pdf_html = ob_get_contents();
ob_end_clean();

// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');

$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();

// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');

$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer

$message = Swift_Message::newInstance()
->setSubject('How To Create and Send An HTML Email w/ a PDF Attachment') // Message subject
->setTo(array($post->email => $post->name)) // Array of people to send to
->setFrom(array('[email protected]' => 'Nettuts+')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'nettuts.pdf', 'application/pdf')); // Attach the generated PDF from earlier

// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;

}

}

$sympathisant=250;
$brons=1000;
$zilver=1500;
$goud=2000;
$perssymp=125;
$persbrons=125;
$perszilver=125;
$persgoud=125;
$table_price=20;
$chair_price=5;
$drinks_price=2;
$snacks_price=1;

$total=0;

if(isset($_POST['sympathisant']) && $_POST['sympathisant']=='1')
{
$total=$total+$sympathisant;
}

if(isset($_POST['brons']) && $_POST['brons']=='1')
{
$total=$total+$brons;
}

if(isset($_POST['zilver']) && $_POST['zilver']=='1')
{
$total=$total+$zilver;
}

if(isset($_POST['goud']) && $_POST['goud']=='1')
{
$total=$total+$goud;
}

if(isset($_POST['drinks']))
{
$num=(int)$_POST['drinks']; // make sure only integers are accepted
if($num>=1) // ignore negative values

{
$total=$total+($perssymp*$num);
}
}

if(isset($_POST['perssymp']))
{
$num=(int)$_POST['perssymp']; // make sure only integers are accepted
if($num>1) // ignore negative values
{
$total=$total+($perssymp*$num-125);
}
}
if(isset($_POST['persbrons']))
{
$num=(int)$_POST['persbrons']; // make sure only integers are accepted
if($num>4) // ignore negative values
{
$total=$total+($persbrons*$num-500);
}
}
if(isset($_POST['perszilver']))
{
$num=(int)$_POST['perszilver']; // make sure only integers are accepted
if($num>6) // ignore negative values
{
$total=$total+($perszilver*$num-750);
}
}

if(isset($_POST['persgoud']))
{
$num=(int)$_POST['persgoud']; // make sure only integers are accepted
if($num>8) // ignore negative values
{
$total=$total+($persgoud*$num-1000);
}
}


echo "<p>Total price is \$$total</p>\n";

?>

{table}
<form id="pricecalculation" method="post" action="">
<table>
<tr>
<td><label for="name">Your Name:</label></td>
<td><input type="text" name="name" id="name" class="input" /></td>
<td><label for="email">Your Email:</label>
<td><input type="text" name="email" id="email" class="input" /></td>
<tr>
<td>Sympathisant</td>
<td><input type="checkbox" name="sympathisant" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
<tr>
<td>Hoeveel personen? </td>
<td><input name="perssymp" type="text" id="perssymp" size="3"></td>
</tr>
</br>
<tr>
<td>Brons</td>
<td><input type="checkbox" name="brons" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen? </td>
<td><input name="persbrons" type="text" id="persbrons" size="3"></td>
</tr>
<br>
<tr>
<td>Zilver</td>
<td><input type="checkbox" name="zilver" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen?</td>
<td><input name="perszilver" type="text" id="perszilver" size="3"></td>
</tr>
<tr>
</br>
<tr>
<td>Goud</td>
<td><input type="checkbox" name="goud" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen? </td>
<td><input name="persgoud" type="text" id="persgoud" size="3"></td>
</tr>
</table>
<div id='total' name='total'>
<script type="text/php">
echo "<p>Total price is \$$total</p>\n";
</script>
</div>
<input name="submit" type="submit" value="Calculate price"/>
</form>
[/table]
Just to tell you for convenience: This is a Dutch forum. ;-)

Would you like to place the code between our code tags? This makes your message more readable!
Why.. don't you just make a real object and work that one out.
Its a better way of handling i think.
Rickert Bombaklats op 13/10/2015 13:23:37

Why.. don't you just make a real object and work that one out.
Its a better way of handling i think.


Bedankt voor de reactie maar kan je iets meer uitleg geven?
Je wilt een pdf maken gaf je al aan.
Als je nu een object maakt of ene class als service die tussen je applicatie staat en je pdf-lib. dan kan je die vullen e.d en daarna met een process() method het object laten omzetten naar een pdf.

Meerdere objecten die allemaal 1 pdf zijn.

Hopelijk snap je wat ik bedoel.



<?php

class pdfService {

}

$offerte = new PdfService();
$offerte->contentData();
$offerte->process();

$offerte->saveTo('path/to/save?');
?>


Het is maar wat bedenksel maar wellicht kan je er iets mee. Zelf zou ik proberen van alles een object te maken en zo weinig mogelijk arrays te gebruiken.
OOP is immers Object gericht.

Reageren