Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: passing a variable to next program

  1. #1
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default passing a variable to next program

    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

    Code:
    <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.
    Last edited by mcolton; 09-27-2015 at 04:26 PM. Reason: Format: [code][/code] Tags

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Thanks for replying. I will give you whatever you need. Where was this moved to

  4. #4
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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
    Code:
    <?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");
    ?>
    Last edited by Beverleyh; 09-26-2015 at 05:19 PM. Reason: Formatting and direct link inserted (home page and instructions to reach the form via additional pages removed)

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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=us...s+to+pass+data
    Last edited by Beverleyh; 09-26-2015 at 05:39 PM. Reason: More info
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. #6
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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.
    Last edited by Beverleyh; 09-26-2015 at 08:39 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  8. #8
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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.
    Code:
    <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
    Last edited by Beverleyh; 09-26-2015 at 10:06 PM. Reason: Formatting

  9. #9
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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=ch...ith+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.
    Last edited by Beverleyh; 09-26-2015 at 10:21 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  10. #10
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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");

Similar Threads

  1. Passing Variable
    By kathycam in forum JavaScript
    Replies: 2
    Last Post: 06-14-2012, 12:06 AM
  2. Passing variable to PHP in JS
    By hemi519 in forum JavaScript
    Replies: 3
    Last Post: 05-27-2012, 12:43 PM
  3. Passing a Variable to a Function - Need Help
    By the penguin in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 11-06-2009, 09:57 PM
  4. passing a parameter with the value of a variable
    By dariuc in forum JavaScript
    Replies: 2
    Last Post: 09-16-2009, 01:45 AM
  5. Resolved Help with variable passing
    By cr3ative in forum JavaScript
    Replies: 4
    Last Post: 12-13-2008, 06:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •