Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    PHP vars getting set in JS section

    I am trying to initialize a PHP variable inside my JS section of my form. Although I am runnng Firefox, the var 'php_category_cbo_width' is getting set inside the opera if statement below. This does not make any sense to me - I thought I could set some PHP vars in the JS section and use if statements to control when and if a php var gets set? But my debugging shows me when m form executes, the 'php_category_cbo_width' value is 14 and 16 as intended.


    PHP Code:
    if ($.browser.name == 'firefox')
       {
         <?php
         $_SESSION
    ['php_category_cbo_width'] = 16;
         
    ?>
         document.getElementById('record_textarea').rows = 20;
      }
    if ($.browser.name == 'opera')
       {
         <?php
         $_SESSION
    ['php_category_cbo_width'] = 14;
         
    ?>
         document.getElementById('cat_cbo').size = 9;
       }
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,982
    Thanks
    3
    Thanked 483 Times in 472 Posts
    PHP code on a page is execuited on the server when the page is requested.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. #3
    Regular Coder JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    What CFMaBiSmAd said. See, when that page is loaded, the server parses that PHP code and sends it to the user...it doesn't check the Javascript to see which PHP code to parse...it does it all at once. So in your example, the server first sets $_SESSION['php_category_cbo_width'] to 16 and then moves on to the next and sets it to 14, since it doesn't look at Javascript.

  4. #4
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    and on that note you not outputing any thing you can do stuff like this with javascript and php
    PHP Code:
    <script>
    var mytest = <?php echo $myvar?> ;
    </script>


 

Posting Permissions

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