Jump to content

PHP Syntax


mdsimmons

Recommended Posts

I'm a newbie with PHP code, but I've successfully built simple email forms... however, I can't seem to get the syntax right for capturing boolean results -I have a form with three checkboxes and a submit button. Depending upon which boxes are checked, I want to accumulate total values for each... for example:if $box1 is checked (true), add 35.00 to $total1,if $box2 is checked (true), add 40.00 to $total1,if $box3 is checked (true), add 45.00 to $total1,Display $total1.It should be simple, but I've missed something.Doug

Link to comment
Share on other sites

The php should look like this

$box1= $_POST['box1']; $box2= $_POST['box2']; $box3= $_POST['box3'];if ($box1 == "y") {$total1+=35.00;}if ($box2 == "y") {$total1+=40.00;}if ($box3 == "y") {$total1+=45.00;}

And the html inputs should look like this

<input type="checkbox" name="box1" value="y" /> <input type="checkbox" name="box2" value="y" /><input type="checkbox" name="box3" value="y" />

Link to comment
Share on other sites

Well, I was close, but no cigar! I had the beginning code right, but missed the if statements; used the wrong quotes; and the wrong accumulator (+=)I assume that if any box is not checked, that particular element { } won't fire, but will fall through to the next?I appreciate the quick response to my problem. Doug

Link to comment
Share on other sites

I rewrote my code as you suggested, and it seems to work, but now I can't get the $total1 value to display after manipulation.I tried:$total1 = $_POST['total1']; . . .. . .echo "Your total is: " . "$total1";and a couple of variations of that, but nothing displays.I also included <input type="text" name="total1" value="0.00">in the html form.Doug

Link to comment
Share on other sites

put $total1 = 0; before the if statements, other wise it will equal null if nothing is "y".Then it should display something.also it would be better to use:echo "Your total is: " . $total1;or:echo "Your total is: $total1";

Link to comment
Share on other sites

I tried your suggestion... still no result: Here's my code -

<?php$box1a = $_POST['box1a'];$box1b = $_POST['box1b'];$box1c = $_POST['box1c'];$total1 = 0;if($box1a=="y"){$total1+=35.00;}if($box1b=="y"){$total1+=40.00;}if($box1c=="y"){$total1 +=35.00;}echo "Your total is: $total1";?>

Doug

Link to comment
Share on other sites

put $total1 = 0; before the if statements, other wise it will equal null if nothing is "y".Then it should display something.also it would be better to use:echo "Your total is: " . $total1;or:echo "Your total is: $total1";

Actually...if $total1 remains equal to zero, that last statement would print 'Your total is: ' because setting $total1 to zero is also 'false' and 'null', which won't print. You might want to do something like this:1. set $total1 = 02. calculate the prices3. check to see if $total1 is equal to 04. if it is, do this: $total1 = '0'That way, the zero will actually print, so if no purchases are made it will still say 'Your total is: 0' instead of nothing :)
Link to comment
Share on other sites

I agree with the logic, but I'm trying to display a 'running total,' i.e., if only the first box is checked, $total1 should display 35.00; if box1 and box2 are checked, $total1 would be 75.00; if all three boxes are checked, $total1 should show 110.00... or, any combination of checked boxes should show the proper cumulative total. At the moment, It prints 0 in any case.My finished form will have more selections, but I'm keeping it simple here to illustrate my problem.Doug> 1. set $total1 = 0> 2. calculate the prices> 3. check to see if $total1 is equal to 0> 4. if it is, do this: $total1 = '0'> That way, the zero will actually print, so if no purchases are made it will still> say 'Your total is: 0' instead of nothing :)

Link to comment
Share on other sites

so you want to display different message when the total=0, if thats what you want, then use a IF before you display the message

<?php$box1a = $_POST['box1a'];$box1b = $_POST['box1b'];$box1c = $_POST['box1c'];$total1 = 0;if($box1a=="y"){$total1+=35.00;}if($box1b=="y"){$total1+=40.00;}if($box1c=="y"){$total1 +=35.00;}if ($total==0){echo "No purchases";}else{echo "Your total is: $total1";}?>

Link to comment
Share on other sites

What's the problem :) this is very straight forward, only a couple of extra lines were required from what i gave earlier.it displays fine when $total1=0;Here is the full code. save as 22.php

<?php$total1=0;if ( isset ($_POST['submit'])) {$box1= $_POST['box1']; $box2= $_POST['box2']; $box3= $_POST['box3'];if ($box1 == "y") {$total1+=35.00;}if ($box2 == "y") {$total1+=40.00;}if ($box3 == "y") {$total1+=45.00;}print"Your total is $$total1";} // End of handle form IF.else {form();// Display the form.}function form(){print'<form action="22.php" method="post"><input type="checkbox" name="box1" value="y" /> <input type="checkbox" name="box2" value="y" /><input type="checkbox" name="box3" value="y" /><input type="submit" value="submit" name="submit" />';}?>

Link to comment
Share on other sites

OK... I got it working with your code :) I've missed some of the logic, I guess, so I'll read it through, again. Thanks for your responses... and patience.Doug

What's the problem :) this is very straight forward, only a couple of extra lines were required from what i gave earlier.
Link to comment
Share on other sites

I've missed some of the logic, I guess, so I'll read it through, again.  Thanks for your responses... and patience.
No probs :) any questions with the logic just ask :)
Link to comment
Share on other sites

Not so much a logic question, as a how-to:I need to email this completed form, including $total1, to a recipient in the same format as the form() function. Is there an easy way to do this without reproducing the whole thing? My finished form will have 18 checkboxes in 4 columns (including a name textbox) of 6 rows. I'm not far enough into PHP to deal with arrays, yet. :) Doug

No probs :)  any questions with the logic just ask :)

Link to comment
Share on other sites

If you want to send it as an HTML email, which would require that the client has an HTML mail reader, then you could use an HTML table to lay it all out. If you are just sending normal text email, then you will probably want to make it display vertically as a list instead of a table.I'm not sure how your data is set up though, do you mean that you have a 4x6 table, with 24 cells, where each cell has 18 checkboxes? Or are there 18 checkboxes in the 4x6 table?

Link to comment
Share on other sites

You've raised an interesting decision point...I've created simple email forms which present a vertical list of data in the body of the message, and these have served well in the past, but now I'm interested in presenting this form in HTML format so that my client can print the form as completed by his customer. I realize I'll have to include "Contact Email:" ($sender), "To:" (&recipient), "Subject:" ($subject), and perhaps other fields as well before I finish the form.This is not a shopping cart page, only a schedule of events in which a potential customer wants to participate, with a $total1 of all fees charged for each event at the bottom of the table (fees are hard-coded into the headers, and included in the script). There are 18 checkboxes in the 4x6 table - the first column is a textbox for the name of each of 6 participants which may select one or more of three events (checkboxes), all on one row, with a single total at the bottom. Scott100 suggested an excellent way to combine the php/html form, which I have expanded to include all my fields, and it works perfectly. Now, if I can just upload that to my client's web site in a form that can be emailed back to him by his customers. As written, it first presents the html form for checking the boxes, then on 'Submit' it simply displays $total1 on a new screen with no further action, which is basically all I asked for in the beginning.It may well be that I can't see the forest for the trees! :) Doug

If you want to send it as an HTML email, which would require that the client has an HTML mail reader, then you could use an HTML table to lay it all out.  If you are just sending normal text email, then you will probably want to make it display vertically as a list instead of a table.I'm not sure how your data is set up though, do you mean that you have a 4x6 table, with 24 cells, where each cell has 18 checkboxes?  Or are there 18 checkboxes in the 4x6 table?

Link to comment
Share on other sites

If you'd like to see this page in its present form, go to my page at http://home.centurytel.net/mdsimmons/phpscripts/22_form.phpDoug

If you want to send it as an HTML email, which would require that the client has an HTML mail reader, then you could use an HTML table to lay it all out.... 

Link to comment
Share on other sites

OK, if the point is to get the client to see the data that has been entered by the public, then you have a few more options. You could always go with the email route, where you just email him all of the data, or you could also just email him a link to a page and have that page display all of the results for you. You could create a report where he could pull up info based the time and date of the submission, or filter however he wants. Or, he could just look up a single entry like he would get through email.But either way, it's going to require you to go into the database where you just saved everything, pull out the entry or entries that you are interested in, and either create an email and send it, or just display it on a web page. The concept is the same, the difference is how you use the data.

Link to comment
Share on other sites

These are all viable options, and I'll munch through them and see if I can figure out which way to go... I had not planned to utilize a database, though. This is a dynamic form, where customers can submit a schedule for one or more participants in a one-time series of events... annual reunions, for instance (the form will be modified for successive reunion events), and this suggests that the simple email route might be the easiest, with a vertical list of names and checked events. I've already accomplished that - it's in place on my client's site now - but I could not figure out how to accumulate the "$total1" on the html form page and combine it with the email. The alternative is to simply add up the events manually. Although I like the php structure, maybe Javascript would present an easier solution?Doug

OK, if the point is to get the client to see the data that has been entered by the public, then you have a few more options.  You could always go with the email route, where you just email him all of the data, .....

Link to comment
Share on other sites

but I could not figure out how to accumulate the "$total1" on the html form page and combine it with the email.  The alternative is to simply add up the events manually.  Although I like the php structure, maybe Javascript would present an easier solution?
Do you mean as the user checks a box on the client side you want the total to be updated and display, combining it with the email should be easy, just print it out to the body :)
Link to comment
Share on other sites

Do you mean as the user checks a box on the client side you want the total to be updated and display, combining it with the email should be easy, just print it out to the body :)

That's what I thought, and I've been trying various code but I can't seem to get it working... I'm afraid confusion is getting the upper hand while I'm learning this new script. :) Doug
Link to comment
Share on other sites

That's what I thought, and I've been trying various code but I can't seem to get it working... I'm afraid confusion is getting the upper hand while I'm learning this new script.   :) Doug

One problem at a time, have a look at this for client side calculations.I have coloured the sections that i have added to make it clearer. The javascript function goes in the head and the php in the body. The form is checked with an onclick handler and i have created an extra div to display the total.
<html>[color="red"]<head><script>[color="blue"]function check(){[/color]var tot=0;    if (document.myForm.box1.checked) {tot+=35;}    if (document.myForm.box2.checked) {tot+=40;}    if (document.myForm.box3.checked) {tot+=45;}          [color="green"]document.getElementById('t1').innerHTML="$"+tot;[/color]}</script></head>[/color]<body><?php$total1=0;if ( isset ($_POST['submit'])) {$box1= $_POST['box1']; $box2= $_POST['box2']; $box3= $_POST['box3'];if ($box1 == "y") {$total1+=35.00;}if ($box2 == "y") {$total1+=40.00;}if ($box3 == "y") {$total1+=45.00;}print"Your total is $$total1";} // End of handle form IF.else {form();// Display the form.}function form(){print'<form [color="red"]name="myForm"[/color] action="22.php" method="post" [color="blue"]onclick="java script:check()"[/color]>Bread: <input type="checkbox" name="box1" value="y" /> <br />Milk: <input type="checkbox" name="box2" value="y" /><br />Cheese: <input type="checkbox" name="box3" value="y" /><br /><br /><input type="submit" value="submit" name="submit" /><br /><br />[color="green"]<div>Total: <span id="t1"></span></div>[/color]';}?></body>

Did you follow that ok? :)

Link to comment
Share on other sites

I have coloured the sections that i have added to make it clearer.  The javascript function goes in the head and the php in the body.  The form is checked with an onclick handler and i have created an extra div to display the total.

[chuckle] Well, I've gotta say that's an elegant solution! I would never have thought of a .getElementByID to get my $tot field included in the form.I've also gotta say I've never seen such prompt and courteous responses to problems posted on a forum board. You guys have all been very kind with my newbie fumblings, and I appreciate the help.I'll adapt this script to my form, and when I get it working properly, I'll be back to show the finished result.Doug
Link to comment
Share on other sites

There's some sort of glitch in the php code here... when I loaded this page, this code appears on the page after the checkboxes - ' ; } ?> and the form doesn't work. The "Total: " statement appears, but nothing after it. Have I copied something wrong? I noticed that there was no closing </form> tag and I added that after the <div> tags, but it didn't make any difference.Doug

function form(){print'<form [color="red"]name="myForm"[/color] action="22.php" method="post" [color="blue"]onclick="java script:check()"[/color]>Bread: <input type="checkbox" name="box1" value="y" /> <br />Milk: <input type="checkbox" name="box2" value="y" /><br />Cheese: <input type="checkbox" name="box3" value="y" /><br /><br /><input type="submit" value="submit" name="submit" /><br /><br />[color="green"]<div>Total: <span id="t1"></span></div>[/color]';}?></body>

Link to comment
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
×
×
  • Create New...