www.webdeveloper.com
Results 1 to 9 of 9

Thread: Passing span value to PayPal

  1. #1
    Join Date
    May 2014
    Posts
    8

    Passing span value to PayPal

    I have a js script which prints a value to innerText of a span. I am trying to take this value and pass it onto paypal as an amount.

    The process goes:

    form.php --> process.php --> PayPal

    Because i am using a script i found online i cannot go directly to PayPal as the form action is already set to the send.php. Therefore i need to pull the value from span in form.php, into process.php and then into PayPal. I have tried using $_SESSION variables but it didn't work and when i var_dumped the session it showed an array when i was expecting a simple integer.

    Im not quite sure which code is useful/relevant

    Code:
    <div class='pcontainer'>
        <label for='words' >Words:</label><br/>
        <span id="words"> </span><br/>
        <span id='contactus_email_errorloc' class='error'></span>
    </div>
    <div class='pcontainer'>
        <label for='tprice' >Price:</label><br/>
        <span id="tprice"> </span><br/>
        <span id='contactus_email_errorloc' class='error'></span>
    </div>
    Because the transition from form.php to proces.php is a re-direct and not a form action, could i still use POST/GET?

    Code:
    <?PHP
    session_start();
    
    $_SESSION["price"] = $_GET["tprice"];
    Any help would be greatly appreciated,

    Thanks!

  2. #2
    Join Date
    May 2014
    Posts
    8
    Essentially all i need to do is get the value of the span into a variable, is this possible or could anyone recommend a better way?

    Thanks

  3. #3
    Join Date
    May 2014
    Posts
    834
    Well, you have ID's on them, so innerText on the element would be easiest. For example your "words" property... I'd probably make a function for that to do the getElementById and the cross browser innerText thus:

    Code:
    function getText(id) {
      var e = document.getElementById(id);
      return e ? (e.innerText || e.textContent) : false;
    }
    Then you could just:

    Code:
    var
      words = getText('words'),
      tprice = getText('tprice');
    ...and so forth.

    Though I have to ask, why do you have LABEL pointing at SPAN? LABEL is for INPUT, TEXTAREA and SELECT inside a FORM, (preferably with a FIELDSET wrapping said form elements). If you really are going to use LABEL, maybe you should use a <input type="readonly"> since it's far easier/reliable/faster to read .value from an INPUT than innerText from a span.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    May 2014
    Posts
    8
    Thanks for the help, i apologize for my very limited knowledge!

    The label's are inside a fieldset which is inside a form! haha

    So after :

    Code:
    var
      words = getText('words'),
      tprice = getText('tprice');
    Are they global variables which could be used on another page ?

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    You STILL have label tags pointing to a span tag - not what they are used for.

    Regarding your limited knowledge: Not knowing how this 'link' to a paypal process is going to facilitate whatever you are doing, but does your limited knowledge provide you with the right concerns for security and input validation and protection against potential malfeasance with your paypal connection? If I were you I would NOT be using my current skillset for such advanced (and perhaps tragic?) implementations.

    PS - It's not nice to laugh at people who are attempting to help you, while still ignoring what they told you.

  6. #6
    Join Date
    May 2014
    Posts
    8
    Firstly i wasn't laughing at him, i meant it as light humour.

    And yes i am probably a bit out of my depth but i have found over time that this is the fastest way i learn, but i wouldn't jeopardise someone's website until i understand what i am doing and implement it correctly.

  7. #7
    Join Date
    May 2014
    Posts
    834
    Quote Originally Posted by JoeHillyard View Post
    Are they global variables which could be used on another page ?
    They are global in terms of JavaScript, but couldn't be used on other pages because when you refresh the page globals are erased. To get them to another page you'd have to submit them as formdata, use javascript with AJAX to pass them in a fake form or as URI getData, or store it in a cookie.

    Oh, and don't sweat the 'haha' -- it's good to hear you at least have the form and fieldset part right, it's just those SPAN don't make any sense. LABEL are for INPUT, SELECT and TEXTAREA -- and that's IT. You don't use them to point at other tags.

    REALLY looking at this, it starts to sound like you should just be using readOnly input and doing normal form submissions, instead of playing with JavaScript, but that's really hard to dial in closer without seeing the full form.
    Java is to JavaScript as Ham is to Hamburger.

  8. #8
    Join Date
    May 2014
    Posts
    8
    Appreciate the understanding!

    I have made an advance but have come across another problem.

    I got the variable to the next page using local storage, formatted the variable to a float with 2 decimal places (correct paypal format).

    Code:
    <script type="text/javascript">
    		
    		var pfinal = localStorage.getItem("pprice");
    
    		var npfinal = parseFloat(pfinal).toFixed(2);
    
    		alert(npfinal);
    
    		</script>
    The alert displays a perfectly normal, 2 decimal number, however when i put this:
    Code:
    <input type="hidden" name="amount" value="npfinal">
    It says it is incorrectly formatted.

    I know the topic has slightly changed and the post may now be in the wrong place, but if anyone has any info would be great!

    Thanks

  9. #9
    Join Date
    May 2014
    Posts
    834
    value has to be that value, you can't just say a javascript variable name as a value, all you've done is set value to the STRING "npfinal" and passing that to PP. Markup does NOT have access to JavaScript variables.

    put an ID on that input like:
    <input type="hidden" name="amount" id="hiddenAmount" value="0" />

    Then in JavaScript:
    document.getElementById('hiddenAmount').value = npFinal;

    You could also reference it by it's form using it's NAME, but that's really more of a Nyetscape 4 style scripting that's frowned upon in modern circles.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles