Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP/MySQL: Get value from one field, subtract 2, multiply by 20, and insert value into another column on form submit

LEGEND ,
Jul 20, 2007 Jul 20, 2007
Hello all,

I'm pulling my hair out--thank you for any help you can give me.

I have a form that a person fills out that has a field that asks "How
many visitors including you" and I'd like them to type in a number, and
on submit of the form, have that value -2 multiplied by 20 and inserted
into a "total cost" column.

I'm using the standard DW server behaviors and I've searched the
internet. How do I perform basic math on that value and insert it into
the database?

If you have any pointers, that would be great.

Thank you!

-John
TOPICS
Server side applications
6.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
John R. Lenz wrote:
> I have a form that a person fills out that has a field that asks "How
> many visitors including you" and I'd like them to type in a number, and
> on submit of the form, have that value -2 multiplied by 20 and inserted
> into a "total cost" column.

That's not the best way to use a database. You can always calculate the
total cost if you know the number of visitors. Let the database or your
PHP code perform the calculation when retrieving the value from the DB.

You also need a bit more conditional logic to prevent nonsensical
amounts. What happens if just two people are coming? Is the cost 0? Or
if just one person comes, does that person get a refund of 20?

Although storing the total cost in the DB is not necessarily the best
idea, this is how you can do it:

Create a hidden field in your form for total_cost, so that the Insert
Record server behavior can build the necessary code for the SQL query.

Right at the very top of the page, create a separate PHP code block like
this:

<?php
if (isset($_POST['visitors'])) {
if ($_POST['visitors'] < 3) {
$num = 1;
} else {
$num = $_POST['visitors'] - 2;
}
$_POST['total_cost'] = $num * 20;
}
?>

That calculates the value of total_cost, and replaces the blank value
submitted by the hidden field.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 23, 2015 Sep 23, 2015
LATEST

This is extremely helpful for me as well!  I'm trying to build a data variable from 2 input fields on a form.  One is text and the other indicates style.  If the input is "October 10, 2015" and "GreenHdr", I'd like $_POST['p_heading'] to be <span style="GreenHdr">October 10, 2015</span>.  Obviously I should hold only the text and style in separate fields in the database, combining them when time to display.  I've got a database whose data is like this, and I realize I need to change it!  I'll have to work out how now to break the pages while I'm redoing it, but THANKS for the advice!  I looked for one thing and found a better one!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
Thank you David--I have your book right in front of me right now and
it's excellent. To answer your questions:

> You also need a bit more conditional logic to prevent nonsensical
> amounts. What happens if just two people are coming? Is the cost 0? Or
> if just one person comes, does that person get a refund of 20?

One person is invited (free to attend), and their first guest is also
free. However, each additional guest is $20, so if the value returns 0,
that's not a problem. This column is just to keep a record of how much
the event is raising. I'd do it in excel, but the client likes to see it
visually.

> Although storing the total cost in the DB is not necessarily the best
> idea, this is how you can do it:

> Create a hidden field in your form for total_cost, so that the Insert
> Record server behavior can build the necessary code for the SQL query.
>
> Right at the very top of the page, create a separate PHP code block like
> this:
>
> <?php
> if (isset($_POST['visitors'])) {
> if ($_POST['visitors'] < 3) {
> $num = 1;
> } else {
> $num = $_POST['visitors'] - 2;
> }
> $_POST['total_cost'] = $num * 20;
> }
> ?>
>
> That calculates the value of total_cost, and replaces the blank value
> submitted by the hidden field.
>

Thank you David--I'll plug this in right now. I'm looking forward to
purchasing your upcoming book.

-John
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
John R. Lenz wrote:
> One person is invited (free to attend), and their first guest is also
> free. However, each additional guest is $20, so if the value returns 0,
> that's not a problem.

In that case, you need to change the code slightly like this:

<?php
if (isset($_POST['visitors'])) {
if ($_POST['visitors'] < 3) {
$num = 0;
} else {
$num = $_POST['visitors'] - 2;
}
$_POST['total_cost'] = $num * 20;
}
?>

That would give you the first two visitors free, and charge 20 for each
subsequent one. Without this, you would end up with -20 for anyone
coming on their own.

> Thank you David--I'll plug this in right now. I'm looking forward to
> purchasing your upcoming book.

Thanks, John. I hope it comes up to your expectations.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
Your code below works great, and I just noticed links to your book were
available on your web site, and it's in the mail to me. Looking forward
to it.

I have one more question below:

> <?php
> if (isset($_POST['visitors'])) {
> if ($_POST['visitors'] < 3) {
> $num = 0;
> } else {
> $num = $_POST['visitors'] - 2;
> }
> $_POST['total_cost'] = $num * 20;
> }
> ?>

After the form is posted, I'd like to address the person who posted it
by their first name and last name, as well as some of the details. How
would you go about doing this in the best way? Would you set a variable
and echo it on the 'Thank you' page, or are there better ways of doing
it with a recordset and filtering it on form submit?

Thank you again,

-John
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
John R. Lenz wrote:
> Your code below works great, and I just noticed links to your book were
> available on your web site, and it's in the mail to me. Looking forward
> to it.

Thanks, hope you find it useful.

> After the form is posted, I'd like to address the person who posted it
> by their first name and last name, as well as some of the details. How
> would you go about doing this in the best way? Would you set a variable
> and echo it on the 'Thank you' page, or are there better ways of doing
> it with a recordset and filtering it on form submit?

To pass the details to a thank you page, you would need to store the
information in $_SESSION variables. You could amend the earlier script
like this:

<?php
session_start();
if (isset($_POST['visitors'])) {
if ($_POST['visitors'] < 3) {
$num = 0;
} else {
$num = $_POST['visitors'] - 2;
}
$_POST['total_cost'] = $num * 20;
$_SESSION['total_cost'] = $_POST['total_cost'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
}
?>

In the thank you page, put the following at the top of the page:

<?php session_start(); ?>

It must go before anything is output to the browser, so put it above the
DOCTYPE declaration, and make sure there's no space before it.

You can then use the $_SESSION variables in the thank you page.

At the end of the page, add this to clear up the session variables:

<?php
$_SESSION = array();
destroy_session();
?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2007 Jul 21, 2007
On Fri, 20 Jul 2007 19:59:36 +0100, David Powers <david@example.com>
wrote:

><?php
>session_start();
>if (isset($_POST['visitors'])) {
> if ($_POST['visitors'] < 3) {
> $num = 0;
> } else {
> $num = $_POST['visitors'] - 2;
> }
> $_POST['total_cost'] = $num * 20;
> $_SESSION['total_cost'] = $_POST['total_cost'];
> $_SESSION['first_name'] = $_POST['first_name'];
> $_SESSION['last_name'] = $_POST['last_name'];
>}
>?>

How about something like:

<?php
session_start();
if (isset($_POST['visitors']) && is_numeric($_POST['visitors'])) {
$num = max($_POST['visitors']-2,0);
$_POST['total_cost'] = $num * 20;
$_SESSION['total_cost'] = $_POST['total_cost'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
}
?>

The only differences are that it checks to ensure that the value entered
by the user is numeric and a slightly less wordy calculation of the
total.

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2007 Jul 21, 2007
Gary White wrote:
> The only differences are that it checks to ensure that the value entered
> by the user is numeric and a slightly less wordy calculation of the
> total.

Good idea. As for being less wordy, I'm always hesitant to use shorthand
techniques when showing someone a solution in an online forum. The more
verbose solution is usually easier to read and, therefore, understand.
Even when coding for myself, I tend to use the verbose method, and then
tighten it up when I know that everything is working properly.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2007 Jul 21, 2007
On Sat, 21 Jul 2007 15:28:52 +0100, David Powers <david@example.com>
wrote:

>Good idea. As for being less wordy, I'm always hesitant to use shorthand
>techniques when showing someone a solution in an online forum. The more
>verbose solution is usually easier to read and, therefore, understand.
>Even when coding for myself, I tend to use the verbose method, and then
>tighten it up when I know that everything is working properly.

I think the aversion to wordiness is just ingrained in my nature going
back to programming when all I had was 8k of ram. Back then, code *had*
to be tight. Now it's just a part of my OCD nature. ;-)

Of course either of our proposals will just ignore an error. A more
complete solution would generate a message to let the user know they had
done something wrong and not do the database insert, or whatever comes
next.

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2007 Jul 23, 2007
Thank you both. And yes, I need it to be a bit wordier while I work
through learning this stuff.

Thank you again,

-John

Gary White wrote:
> On Sat, 21 Jul 2007 15:28:52 +0100, David Powers <david@example.com>
> wrote:
>
>> Good idea. As for being less wordy, I'm always hesitant to use shorthand
>> techniques when showing someone a solution in an online forum. The more
>> verbose solution is usually easier to read and, therefore, understand.
>> Even when coding for myself, I tend to use the verbose method, and then
>> tighten it up when I know that everything is working properly.
>
> I think the aversion to wordiness is just ingrained in my nature going
> back to programming when all I had was 8k of ram. Back then, code *had*
> to be tight. Now it's just a part of my OCD nature. ;-)
>
> Of course either of our proposals will just ignore an error. A more
> complete solution would generate a message to let the user know they had
> done something wrong and not do the database insert, or whatever comes
> next.
>
> Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2007 Jul 23, 2007
On Mon, 23 Jul 2007 09:21:02 -0400, "John R. Lenz" <user@domain.invalid>
wrote:

>Thank you both. And yes, I need it to be a bit wordier while I work
>through learning this stuff.

You're welcome. Good luck!

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines