Results 1 to 7 of 7

Thread: Javascript / PHP question

  1. #1
    Senior Member j1979's Avatar
    Join Date
    Apr 2007
    Location
    Mars
    Posts
    2,038
    Thanks
    339
    Thanked
    209 times in 143 posts

    Javascript / PHP question

    <?php
    $single = $_SESSION['tickets'];
    var_dump($single); // This returns the integer value of 24.. (or the cost of tickets)
    ?>

    <script type="text/javascript">

    var test = "<?= $single ?>";

    <b>Total: </b>£<span id="test">0</span>

    // i expected this to return the same value as above (24). but the value is 0 / nul.

    does anyone know what im doing wrong? (this is not the full script, but all the other code does not deal with these variables) .



  2. #2
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,231
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: Javascript / PHP question

    I assume <?= $single ?> is equivalent to <? echo $single ?> ? Have you tried the verbose version?

    The snippet above is lacking a </script>, or any method of putting the variable test into the page? Are these just typos in the snip from the full page? if not, where is the value of test meant to show in the page? If it's in the span then you'll need some javascript that does something like document.getElementById("test").innerHtml = test; (I think innerHtml is the correct property, but you can look that up on the w3 website... )

  3. Received thanks from:

    j1979 (23-05-2010)

  4. #3
    Senior Member j1979's Avatar
    Join Date
    Apr 2007
    Location
    Mars
    Posts
    2,038
    Thanks
    339
    Thanked
    209 times in 143 posts

    Re: Javascript / PHP question

    Quote Originally Posted by scaryjim View Post
    I assume <?= $single ?> is equivalent to <? echo $single ?> ? Have you tried the verbose version?

    The snippet above is lacking a </script>, or any method of putting the variable test into the page? Are these just typos in the snip from the full page? if not, where is the value of test meant to show in the page? If it's in the span then you'll need some javascript that does something like document.getElementById("test").innerHtml = test; (I think innerHtml is the correct property, but you can look that up on the w3 website... )
    nice one!! not tried it yet as the beer garden is calling.

    but thanks i recon this should sort it... knowledge is power

  5. #4
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,231
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: Javascript / PHP question

    No worries, always happy to help where possible I'm a little confused as to why you wouldn't just put
    Code:
    <span id="test"><?= $single ?></span>
    though - do you need to use the value elsewhere in the script? Seems long winded to assign it to a variable in javascript if all you need to do is stick it in the body of the page...?

  6. Received thanks from:

    j1979 (03-06-2010)

  7. #5
    Senior Member j1979's Avatar
    Join Date
    Apr 2007
    Location
    Mars
    Posts
    2,038
    Thanks
    339
    Thanked
    209 times in 143 posts

    Re: Javascript / PHP question

    it turns out is was missing the semicolon ; after the PHP variable. i can't believe i didn't notice for so long.... now the script works apart from the value passed to paypal.. this line ..


    <input type="hidden" name="amount" value="?????">




    here is the full script.


    <?php

    $single = $_SESSION['tickets'];
    $rate = $single + $_SESSION['services'];

    ?>

    <script language="javascript" type="text/javascript">
    var test = "<?php echo $single; ?>";
    var rate = "<?php echo $rate; ?>";

    var total = rate;
    function priceUpdate(tog)
    {
    var tCheck = tog.checked;
    if (tCheck)
    {
    total = parseInt(rate,10) + parseInt(test,10)
    }
    else
    {
    total = rate
    }
    tPrice = document.getElementById('totalprice');
    tPrice.innerHTML = total;
    }
    -->
    </script>
    <input type="checkbox" id="chk1" onClick="priceUpdate(this)"/><b>Return Ticket</b>
    <br />

    <h1><b>Your Total £<span id="totalprice"><script>document.write(total);</script></span></h1><br/>



    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="GSMCxxxXD5TMA">
    <input type="hidden" name="lc" value="GB">
    <input type="hidden" name="item_name" value="Your Tickets">

    <input type="hidden" name="amount" value="?????"> // Any idears how to get the javascript variable to pass to paypal..?

    <input type="hidden" name="currency_code" value="GBP">
    <input type="hidden" name="button_subtype" value="services">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="no_shipping" value="2">
    <input type="hidden" name="rm" value="1">
    <input type="hidden" name="return" value="http://mysite/returnpage">
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
    <input type="image" src="https://www.paypal.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
    <img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">

    </script>
    </form>


    before i had no problem passing the value when it was a PHP variable.. but now i have to send a javascript variable... i have tried so many options.
    Last edited by j1979; 03-06-2010 at 10:04 PM.

  8. #6
    PHP Geek Flash477's Avatar
    Join Date
    Dec 2008
    Location
    Devon
    Posts
    822
    Thanks
    51
    Thanked
    72 times in 65 posts

    Re: Javascript / PHP question

    Changes added in red. I added the PHP variables as the default values, as it guarantees the values exist even if the javascript doesn't run.

    The <?=?> syntax shouldn't really have any spaces - hence why you needed the ;

    <?php

    $single = $_SESSION['tickets'];
    $rate = $single + $_SESSION['services'];

    ?>

    <script language="javascript" type="text/javascript">
    var test = "<?=$single?>";
    var rate = "<?=$rate?>";

    var total = rate;
    function priceUpdate(tog)
    {
    var tCheck = tog.checked;
    if (tCheck)
    {
    total = parseInt(rate,10) + parseInt(test,10)
    }
    else
    {
    total = rate
    }
    tPrice = document.getElementById('totalprice');
    tPrice.innerHTML = total;
    document.getElementById('pp_amount').value = total;
    }
    -->
    </script>
    <input type="checkbox" id="chk1" onClick="priceUpdate(this)"/><b>Return Ticket</b>
    <br />

    <h1><b>Your Total £<span id="totalprice"><?=$rate?></span></h1><br/>



    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="GSMCxxxXD5TMA">
    <input type="hidden" name="lc" value="GB">
    <input type="hidden" name="item_name" value="Your Tickets">

    <input type="hidden" name="amount" value="<?=$rate?>" id="pp_amount"> // Any idears how to get the javascript variable to pass to paypal..?

    <input type="hidden" name="currency_code" value="GBP">
    <input type="hidden" name="button_subtype" value="services">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="no_shipping" value="2">
    <input type="hidden" name="rm" value="1">
    <input type="hidden" name="return" value="http://mysite/returnpage">
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
    <input type="image" src="https://www.paypal.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
    <img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">

    Not needed -> </script>
    </form>
    Last edited by Flash477; 04-06-2010 at 12:43 AM.

  9. Received thanks from:

    j1979 (04-06-2010)

  10. #7
    Senior Member j1979's Avatar
    Join Date
    Apr 2007
    Location
    Mars
    Posts
    2,038
    Thanks
    339
    Thanked
    209 times in 143 posts

    Re: Javascript / PHP question

    Flash and scaryjim you are a wonderful people!!

    i am truly grateful

    if you're ever in Ukraine and need a train

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. The random question thread.
    By matty-hodgson in forum General Discussion
    Replies: 44
    Last Post: 12-04-2010, 01:03 AM
  2. How to ask an anonymous question?
    By Allen in forum General Discussion
    Replies: 26
    Last Post: 03-12-2009, 02:36 PM
  3. Creationist question on GCSE Biology exam
    By JPreston in forum General Discussion
    Replies: 17
    Last Post: 06-07-2009, 08:35 PM
  4. Javascript Help
    By Basher in forum Software
    Replies: 31
    Last Post: 30-08-2003, 09:19 PM

Posting Permissions

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