PDA

View Full Version : Resolved passing a variable to next program



mcolton
09-26-2015, 02:54 PM
I have shown some code below. I'm trying to pass the variable "price" (or totalPrice) onto the next called program that sends out an email. Code shown is from registration.php


<script>
function getTotal()
{
var price = 40 + (extraguests * 20);
document.getElementById('totalPrice').innerHTML = " Please send a check (made out to 'LOTA') for $"+price+" to:";
}
</script>
...

<form action="registration2.php">
....
<label>Number of non-playing people you are going to bring to the dinner party ($20 per person):</label>
<select id="extraguests" name="extraguests" required
onchange="getTotal()">
<option value="">Select</option>
<option value="0" >0</option>
<option value="1" >1</option> When user selects an option, getTotal() is run and "totalPrice" is printed on the page correctly.
<option value="2" >2</option> When submit is hit, all the other fields in the form are passed to "registration2.php" but
<option value="3" >3</option> "totalprice" is null.
<option value="4" >4</option>
</select><br /><br />
....
<span id="totalPrice"></span>
....

Thanks for any help.

Beverleyh
09-26-2015, 04:02 PM
It isn't clear what you're trying to do with the way you've formatted your post - could be a multi-stage form from all the steps you're describing.

Please try to separate your code from your process description.

Either way, I don't see a form field that is carrying the "totalPrice" output in its value attribute - this would usually be done via a hidden field.

If you need more help, please provide a working demo and the contents of the registration2.php file

mcolton
09-26-2015, 04:34 PM
Thanks for replying. I will give you whatever you need. Where was this moved to

mcolton
09-26-2015, 04:43 PM
not sure what you mean by working demo. Go to http://lotatennis.com/classic-registration.php to see the form.
If you select an option in "number of non-playing people" you will see "totalprice" popup.
After entering all the info and submitting, registration2.php runs and sends out emails. The emails work correctly except "totalPrice" is sent as a blank field.
here is registration2.php


<?php
$yfname = filter_input(INPUT_POST, "zfname");
$ylname = filter_input(INPUT_POST, "zlname");
$yemail = filter_input(INPUT_POST, "zemail");
$yhome = filter_input(INPUT_POST, "zhome");
$ycell = filter_input(INPUT_POST, "zcell");
$ypartner = filter_input(INPUT_POST, "zpartner");
$ylevel = filter_input(INPUT_POST, "zlevel");
$yshirt = filter_input(INPUT_POST, "zshirt");
$yusta = filter_input(INPUT_POST, "zusta");
$ysignature = filter_input(INPUT_POST, "zsignature");
$yextras = filter_input(INPUT_POST, "zextras");
$yparty = filter_input(INPUT_POST, "zparty");


try
{
$connect = new PDO('mysql:host=localhost;dbname=lota_classic', "lota_mcolton", "lotatest");
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$statement = $connect->prepare("INSERT INTO classic(firstname, lastname, email, homephone, cellphone, partner, level, USTA, shirt, party, extras, signature, entryfee)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
$statement->execute(array($yfname, $ylname, $yemail, $yhome, $ycell, $ypartner, $ylevel, $yusta, $yshirt, $yparty, $yextras, $ysignature, $yentryfee));

$connect = null;
} // end try

catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}

$subject = "LOTA Classic Registration Form";
$to = "martyc@windstream.net";
$to2 = "$yemail";
$emailheader = "From: $yemail\n";

$message = "The following person has registered for the LOTA Classic.
Name: $yfname $ylname
Email Address: $yemail
Home Phone: $yhome
Cell Phone: $ycell
Partner: $ypartner
USTA Rating: $yusta
Level: $ylevel
T-shirt: $yshirt
Signature: $ysignature
Attending Dinner: $yparty
Extra Guests: $yextras
Entry Fee: $yentryfee";

$message2 = "Thank you for registering for the LOTA Classic.
Below is the information we received:
Name: $yfname $ylname
Email Address: $yemail
Home Phone: $yhome
Cell Phone: $ycell
Partner: $ypartner
USTA Rating: $yusta
Level: $ylevel
T-shirt: $yshirt
Signature: $ysignature
Attending Dinner: $yparty
Extra Guests: $yextras
Entry Fee: $yentryfee

Remember to send a check made out to LOTA to
Larry Kludt, Tournament Director
1030 Tailwater Unit B
Greensboro, GA 30642";

mail($to,$subject,$message,$emailheader);
mail($to2,$subject,$message2,$emailheader);
header("Location: index.htm");
?>

Beverleyh
09-26-2015, 05:16 PM
Referring back to my first reply, I don't see anything in the form that is attempting to pass "totalPrice" to the processing script. It's being printed in a span but that's it - a span is not a form field, so it isn't being sent via the form at all.

Typically you would use a hidden form field where the "totalPrice" output is inserted into the value attribute.

Then on the processing side, there also needs to be a line that posts the data to a variable. Currently there is nothing in the PHP that would be attempting to process the (not yet present) "totalPrice" data passed from the form.

More info https://www.google.co.uk/search?q=using+hidden+firm+fiekds+to+pass+data

mcolton
09-26-2015, 08:18 PM
I added the following to registration.php

<input type="hidden" name="membership_type"
value="<?php echo $_POST['totalPrice']; ?>">

and added
$yentryfee = filter_input(INPUT_POST, "totalPrice");
to registration2.php and I still got a null field for totalPrice

Beverleyh
09-26-2015, 08:28 PM
I added the following to registration.php

<input type="hidden" name="membership_type"
value="<?php echo $_POST['totalPrice']; ?>">

Do you mean the "classic-registration.php" page?

You can't echo $_POST['totalPrice']; to the form field because it isn't being set by PHP (and the form isnt posted at this point anyway). totalPrice is being set via the JavaScript function, so as well as printing to the DOM in your visible span, you also need to add it to the value of the hidden form field.

Also watch that your post data is named correctly from your form field into your processing code.

mcolton
09-26-2015, 09:35 PM
Yes to classic-registration.php (I simplified the code when I cut and pasted it).

I'm sorry but I'm a self-taught programmer and a lot of what you are saying I don't understand. DOM ?? field being set ??

I assumed that when I ran the function getTotal() (shown below), the var price or totalPrice (which should be =) would be available in registration2.php like all the other form fields.
totalPrice is correctly filled in on the form when the user selects an option from number of guests added.


<script>
function getTotal()
{
var price = 40 + (getguestsPrice() * 20);
document.getElementById('totalPrice').innerHTML = " Please send a check (made out to 'LOTA') for $"+price+" to:";
}
</script>

Thanks for the help

Beverleyh
09-26-2015, 10:06 PM
Unfortunately, a JavaScript variable is not interchangeable with a PHP variable. JavaScript runs on the client-side, while PHP runs on the server-side, so in order for JS to cross the void into server-side scripting, JS-generated data needs to be posted to the server-side processing language via an appropriate mechanism. A form field provides such a mechanism.

In order for this to work, try looking at the processes in order.

The JavaScript function already calculates and inserts the required totalPrice variable to the innerHTML of the span element, so you can reuse the price variable from that. For the form field, it would be more appropriate to use the value property (rather than innerHTML). Learn more here https://www.google.co.uk/search?q=change+the+value+of+input+field+with+javascript

Once you've written that into your function, make sure your HTML markup contains the appropriate attributes;
- id (used by the JS function)
- name (used by the PHP processing script)

You're already doing this in your existing page, so take your cues from there.

Lastly, make sure the PHP receives the appropriate data from the form field via the name attribute. Again, you already have this code for other form fields so follow the patterns of what you've already written.

mcolton
09-27-2015, 11:51 AM
I know I'm getting close because now my "entryfee" database field is empty and not "NULL".
The totalPrice field of $60 is showing up on the page when I select 1 guest.
I added the following to registration.php

<span id="totalPrice"></span>
<input type="hidden" name="entryfee" id="entryfee" value="<? echo $totalPrice; ?>" />

and added to registration2.php the following line
$yentryfee = filter_input(INPUT_POST, "entryfee");

Beverleyh
09-27-2015, 01:17 PM
You've got your PHP line sorted - that's fine and like you say, you're progressing in the right direction. However, you're still trying to mix a PHP echo'd variable into you're markup that doesn't exist (and never will). You can't do that - you have to insert the JS price variable there instead from inside the getTotal() JS function, and you would do it with JavaScript's "value" property. I think you must have missed my previous link because any of the results on the first page show how to set the value of an input element with JS https://www.google.co.uk/search?q=change+the+value+of+input+field+with+javascript

mcolton
09-27-2015, 02:31 PM
Thanks for all your help. I've looked at all the links you sent me and I'm just as confused.
I'm thinking that I'm complicating things by trying to send totalPrice to registration2.php (I assume that's what you mean by the "php").
registration.php is working correctly and showing totalPrice just as I wanted.

Maybe the easier way is to do the calculation in the php because that's the place I need it. I'm going to try that and let you know.

mcolton
09-27-2015, 02:48 PM
I can't believe how easy that was to do. I just added the following lines to registration2.php and it worked perfectly.
Thanks for all your help

$yentryfee = 40 + ($yextras * 20);
$yentryfee = "$".$yentryfee;

Beverleyh
09-27-2015, 04:14 PM
Glad you got it sorted :)