Emailing form output

For my course project I have to create a web page where users can insert there income and expense information into the form and then get an output that tells them what their total income is, their total expenses, how much they will have left after paying expenses, and then it tells the user what percentage of their income and expenses each expense actually is.

I have been really proud of myself, because I wrote a code that does exactly what it’s supposed to and it seems to work just fine. This week, however, I have to take that output page and give the user a way to print the information and/or email the information to themselves. I want to create buttons from images (images/print.png) and (images/email.png) that do that. But I can’t find any examples that help me get their using this project.

Is there a simple string of script that will send my html out to the printer and email? Or do I need to create a new script all together and link the buttons to those scripts? This is my output page as it stands right now:
[php]<?php
// create short variable names; each variable name is reflective of the field it’s assigned to. I have tried to keep my variables under 5-6 letters, so some longer words have been reduced to an abbreviated form (for example maintenance becomes maint, which is the first five letters of the word. Also you maynoticed that utility becomes uutil, which is only four letters, the reason is because I never end an abbreviation with a vowel- it’s a weird compulsion, but it’s one I understand.
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$month = $_POST[‘month’];
$year = $_POST[‘year’];
$pay1 = $_POST[‘pay1’];
$pay2 = $_POST[‘pay2’];
$pay3 = $_POST[‘pay3’];
$pay4 = $_POST[‘pay4’];
$house = $_POST[‘house’];
$util = $_POST[‘util’];
$trans = $_POST[‘trans’];
$ins = $_POST[‘ins’];
$maint = $_POST[‘maint’];
$food = $_POST[‘food’];
$loan = $_POST[‘loan’];
$invest = $_POST[‘invest’];
$cc1 = $_POST[‘cc1’];
$cc2 = $_POST[‘cc2’];
$cell = $_POST[‘cell’];
$media = $_POST[‘media’];
$other1 = $_POST[‘other1’];
$other2= $_POST[‘other2’];
//sets fields taking money amounts to floats, so that they can take real money values
settype ($year, ‘int’);
settype ($pay1, ‘float’);
settype ($pay2, ‘float’);
settype ($pay3, ‘float’);
settype ($pay4, ‘float’);
settype ($house, ‘float’);
settype ($util, ‘float’);
settype ($trans, ‘float’);
settype ($ins, ‘float’);
settype ($maint, ‘float’);
settype ($food, ‘float’);
settype ($loan, ‘float’);
settype ($invest, ‘float’);
settype ($cc1, ‘float’);
settype ($cc2, ‘float’);
settype ($cell, ‘float’);
settype ($media, ‘float’);
settype ($other1, ‘float’);
settype ($other2, ‘float’);
?>

Budget Tracker
Budget Tracker
Budget Summary
<?php echo "

$name
"; echo "$email
"; //script for defining the month variable and displaying the outcome if ($month == "1") { echo "January "; } elseif ($month == "2") { echo "February "; } elseif ($month == "3") { echo "March "; } elseif ($month == "4") { echo "April "; } elseif ($month == "5") { echo "May "; } elseif ($month == "6") { echo "June "; } elseif ($month == "7") { echo "July "; } elseif ($month == "8") { echo "August "; } elseif ($month == "9") { echo "September "; } elseif ($month == "10") { echo "October "; } elseif ($month == "11") { echo "November "; } elseif ($month == "12") { echo "December "; } else { echo "Month Not Chosen"; } echo "$year


"; //echos the entry for the year.

//adds income fields
$income = 0;
$income = $pay1 + $pay2 + $pay3 + $pay4;
printf ("

Total Income is $%.2f
", $income);

//adds expense fields
$expenses = 0;
$expenses = $house + $util + $trans + $ins + $maint + $food + $loan + $invest + $cc1 + $cc2 + $cell + $media + $other1 + $other2;
printf ("Total Expenses are $%.2f
", $expenses);
//subtracts expense from income
$outlook = 0;
$outlook = $income - $expenses;
printf ("Total pocket for the month is $%.2f
", $outlook);

//creates projects on expense and income totals for each expense, rounds to the nearest tenth place
echo '

Expense Projections:

';
$rent1= 0;
$rent2= 0;
$rent1= round($house / $income * 100,2);
$rent2= round($house / $expenses * 100,2);
echo “
    ”; //begins an unordered list
    echo “
  • Rent/Mortgage account for $rent1% of the total income and $rent2% of the total expenses

  • ”;
    $util1= 0;
    $util2= 0;
    $util1= round($util / $income * 100,2);
    $util2= round($util / $expenses * 100,2);
    echo “
  • Utilities account for $util1% of the total income and $util2% of the total expenses

  • ”;
    $trans1= 0;
    $trans2= 0;
    $trans1= round($trans / $income * 100,2);
    $trans2= round($trans / $expenses * 100,2);
    echo “
  • Transportation costs account for $trans1% of the total income and $trans2% of the total expenses

  • ”;
    $ins1= 0;
    $ins2= 0;
    $rent1= round($ins / $income * 100,2);
    $rent2= round($ins / $expenses * 100,2);
    echo “
  • Insurance costs account for $ins1% of the total income and $ins2% of the total expenses

  • ”;
    $maint1= 0;
    $maint2= 0;
    $maint1= round($maint / $income * 100,2);
    $maint2= round($maint / $expenses * 100,2);
    echo “
  • Saving for maintenance costs for a house and/or car account for $maint1% of the total income and $maint2% of the total expenses

  • ”;
    $food1= 0;
    $food2= 0;
    $food1= round($food / $income * 100,2);
    $food2= round($food / $expenses * 100,2);
    echo “
  • Grocery spending accounts for $food1% of the total income and $food2% of the total expenses

  • ”;
    $loan1= 0;
    $loan2= 0;
    $loan1= round($loan / $income * 100,2);
    $loan2= round($loan / $expenses * 100,2);
    echo “
  • Monthly loan payments account for $loan1% of the total income and $loan2% of the total expenses

  • ”;
    $invest1= 0;
    $invest2= 0;
    $invest1= round($invest / $income * 100,2);
    $invest2= round($invest / $expenses * 100,2);
    echo “
  • This month $invest1% of the total income and $invest2% of the total expenses was spent on investments

  • ”;
    //To demonstrate I’m putting thought into my budget tracker, and create a more accurate projection, I combined credit cards 1 and 2 into one variable, ccx, and calculated the percentage of the total credit card debt as well as each individual card.
    $cc11= 0;
    $cc12= 0;
    $cc11= round($cc1 / $income * 100,2);
    $cc12= round($cc1 / $expenses * 100,2);
    echo “
  • The first credit card accounts for $cc11% of the total income and $cc12% of the total expenses

  • ”;
    $cc21= 0;
    $cc22= 0;
    $cc21= round($cc2 / $income * 100,2);
    $cc22= round($cc2 / $expenses * 100,2);
    echo “
  • The second credit card accounts for $cc21% of the total income and $cc22% of the total expenses

  • ”;
    $ccx= 0;
    $ccx= $cc1 + $cc2;
    $ccx1= round($ccx / $income * 100,2);
    $ccx2= round($ccx / $expenses * 100,2);
    echo “
  • Credit card debt accounts for $ccx1% of the total income and $ccx2% of the total expenses

  • ”;
    //and then I go back figuring up the fields like I did in the early part of the projections
    $cell1= 0;
    $cell2= 0;
    $cell1= round($cell / $income * 100,2);
    $cell2= round($cell / $expenses * 100,2);
    echo “
  • Mobile phone service accounts for $cell1% of the total income and $cell2% of the total expenses

  • ”;
    $media1= 0;
    $media2= 0;
    $media1= round($media / $income * 100,2);
    $media2= round($media / $expenses * 100,2);
    echo “
  • Entertainment costs (like cable and internet bills, for example) account for $media1% of the total income and $media2% of the total expenses

  • ”;
    $other11= 0;
    $other12= 0;
    $other11= round($other1 / $income * 100,2);
    $other12= round($other1 / $expenses * 100,2);
    echo “
  • The first miscellaneous expense accounts for $other11% of the total income and $other12% of the total expenses

  • ”;
    $other21= 0;
    $other22= 0;
    $other21= round($other2 / $income * 100,2);
    $other22= round($other2 / $expenses * 100,2);
    echo “
  • The second miscellaneous expense accounts for $other21% of the total income and $other22% of the total expenses

  • ”;
    //I decided to create a total miscellaneous spending calculation
    $otherx= 0;
    $otherx= $other1 + $other2;
    $otherx1= round($otherx / $income * 100,2);
    $otherx2= round($otherx / $expenses * 100,2);
    echo “
  • Your total miscellaneous spending expense accounts for $otherx1% of the total income and $otherx2% of the total expenses

  • ”;
    echo “
      ”; //kills the unordered list
      ?>
[/php]

This is my output page, along with some basic notation to show you what I was thinking when I wrote something. I know a lot of people aren’t into closing php tags, but they are required for the course work, so that’s out of my hands. I’m not trying to get fancy, just something simple, basic, and functional.

Thanks i advance for anyone who can give me some pointers on this…

well first instead of explicitly declaring each variable name, since you are using the field names anyway you could use:

[php]
foreach ($_POST as $key => $value) {
${$key} = $value;
}
[/php]
that will create a variable for each of the keys you have in the $_POST array, which is derived from the FIELD NAMES sent to the script from your form. you would still need to set the type but you could loop that as well with a little bit of creative logic :slight_smile:

now to your question, as for printing that is client side not server side, so you would not be using php for that. you could echo some javascript, but idk if that’s what you want. for mail it would be pretty easy, I would modify the above code first:

[php]
$message = ‘’;
foreach ($_POST as $key => $value) {
${$key} = $value;
$message .= $key. ’ = '. $value. ‘
’;
}

mail($email, ‘My Subject’, $message);
[/php]

that would send the email immediately, you probably want it behind a button, or have a simple extra script called email that you link to and pass the information.
[php]
$message = ‘’;
foreach ($_POST as $key => $value) {
${$key} = $value;
$message .= $key. ’ = '. $value. ‘
’;
}

mail($email, ‘My Subject’, $message);
[/php]

If it was for me, I’d jump over into javascript in a heartbeat…:frowning: Sadly it’s not my choice. I ended up creating a button with an onClick value that calls my ‘mail.php’ script, and I’ve just been experimenting ever since. I’ve scrapped the print button all together, with a lengthy note to my instructor about why PHP wasn’t a good choice for that particular idea.

My code is scary looking right now, but I’ve only been writing script for like three weeks worth of assignments. I’m going in this week to build a function around my operations. It worked really well just calling a new variable, and then adding the relevant fields…but I think it makes my code a little longer and more complicated. I’ve got some extra time this week, so I’m also going to be reformatting with concatenation.

As for giving the UX a little more “user friendliness”, I’m thinking about jumping into learning how to build a graph that compares user expenses to income, and then break those percentages down by each expense.

Thanks for you’re help, I’m going to take a look at the code you gave me and see how well I can make it work with my project. I really wished time was a luxury where I could go back and start again, but much slower with projects that fit into my current skill level…but I figure if I learned to swim by being thrown in the deep in…why not scripting :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service