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 2 of 2
  1. #1
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems combining JS, PHP and working with variables

    I found a JS script and I'm having problems figuring out how to modify it to use a different input source and return a different output source.

    Basically I want to pull a value from a field in MySQL and figure out the break down of that value (if you enter a value and press "Calculate Levels" you will see what I mean), but I need the results returned to a variable or an array.

    I've read thru a lot of posts and havn't found any thing to help, I'm kind of under the impression that I may not be able to due this because of the Server/Client side script running and may have to set it up as a straight php solution, but have very little JS exp so not sure what going on in the script and still week in this area of PHP.

    1. Is there a way to use $var_total as input rather then document.calc.org_total.value ($var_total will eventualy be a value from MySQL)
    2. Is there a way to use $var_lvl_a as output rather then document.calc.a.checked (this will eventualy be an array)
    3. If 1&2 is possible would some one with JS/PHP exp mind providing some help or If I cant do 1&2 would some one with JS/PHP exp mind giving me a hand converting this to a straight PHP solution.

    PHP Code:
    <?php
      $var_total 
    "53";
      
    $var_lvl_a "0";
      
    $var_lvl_b "0";
      
    $var_lvl_c "0";
      
    $var_lvl_d "0";
      
    $var_lvl_e "0";
      
    $var_lvl_f "0";
      
    $var_lvl_g "0";
      
    $var_lvl_h "0";
      
    $var_lvl_i "0";
      
    $var_lvl_j "0";
      
    $var_lvl_k "0";
      
    $var_lvl_l "0";
      
    $var_lvl_m "0";
      
    $var_lvl_n "0";
      
    $var_lvl_o "0";
      
    $var_lvl_p "0";
      
    $var_lvl_q "0";
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
      <title>Access Levels</title>
      <script language=JavaScript>
        function calcLevels() {
          var mask = 1;
          var lev = parseInt(document.calc.org_total.value);
          if ( (lev & mask) != 0 ) document.calc.a.checked = true;
            else document.calc.a.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.b.checked = true;
            else document.calc.b.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.c.checked = true;
            else document.calc.c.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.d.checked = true;
            else document.calc.d.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.e.checked = true;
            else document.calc.e.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.f.checked = true;
            else document.calc.f.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.g.checked = true;
            else document.calc.g.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.h.checked = true;
            else document.calc.h.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.i.checked = true;
            else document.calc.i.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.j.checked = true;
            else document.calc.j.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.k.checked = true;
            else document.calc.k.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.l.checked = true;
            else document.calc.l.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.m.checked = true;
            else document.calc.m.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.n.checked = true;
            else document.calc.n.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.o.checked = true;
            else document.calc.o.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.p.checked = true;
            else document.calc.p.checked = false;
          mask = mask << 1;
          if ( (lev & mask) != 0 ) document.calc.q.checked = true;
            else document.calc.q.checked = false;
        }
      // -->
      </script>
    </head>
    <body>
      <form name=calc method=POST>
        <p>
          00001 - <input type=checkbox name=a value=1><br />
          00002 - <input type=checkbox name=b value=2><br />
          00004 - <input type=checkbox name=c value=4><br />
          00008 - <input type=checkbox    name=d value=8><br />
          00016 - <input type=checkbox name=e value=16><br />
          00032 - <input type=checkbox name=f value=32><br />
          00064 - <input type=checkbox name=g value=64><br />
          00128 - <input type=checkbox name=h value=128><br />
          00256 - <input type=checkbox name=i value=256><br />
          00512 - <input type=checkbox name=j value=512><br />
          01024 - <input type=checkbox name=k value=1024><br />
          02048 - <input type=checkbox name=l value=2048><br />
          04096 - <input type=checkbox name=m value=4096><br />
          08192 - <input type=checkbox name=n value=8192><br />
          16384 -<input type=checkbox name=o value=16384><br />
          32768 - <input type=checkbox name=p value=32768><br />
          65536 - <input type=checkbox name=q value=65536>
        </p>
        <p>
          Enter value &nbsp;
          <input type="text" name="org_total">
          &nbsp;
          <input type="button" value="Calculate Levels"  onClick="calcLevels()">
        </p>
      </form>

    <!-- New Section -->
    <?php
       
    echo '<p>' .
        
    $var_lvl_a .'<Br />'
        
    $var_lvl_b .'<Br />'.
        
    $var_lvl_c .'<Br />'.
        
    $var_lvl_d .'<Br />'.
        
    $var_lvl_e .'<Br />'.
        
    $var_lvl_f .'<Br />'.
        
    $var_lvl_g .'<Br />'.
        
    $var_lvl_h .'<Br />'.
        
    $var_lvl_i .'<Br />'.
        
    $var_lvl_j .'<Br />'.
        
    $var_lvl_k .'<Br />'.
        
    $var_lvl_l .'<Br />'.
        
    $var_lvl_m .'<Br />'.
        
    $var_lvl_n .'<Br />'.
        
    $var_lvl_o .'<Br />'.
        
    $var_lvl_p .'<Br />'.
        
    $var_lvl_q .'<Br /> </p>';
    ?>
    </body>
    </html>
    Last edited by musher; 11-05-2006 at 10:07 PM.
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    So, you need some explanations first.

    JavaScript and PHP do NOT interact in a programmatic way. PHP generates web page source code. JavaScript runs in the browser.

    So, how do you get a variable from PHP to javascript? The variable has to be set in PHP before the page loads (that means you can't get the value from a select box to PHP without submitting a form), and then you have to generate the JavaScript code in PHP and echo it.

    Code:
    <?php
      $var_total = "53";
      $var_lvl_a = "0";
      $var_lvl_b = "0";
      $var_lvl_c = "0";
      $var_lvl_d = "0";
      $var_lvl_e = "0";
      $var_lvl_f = "0";
      $var_lvl_g = "0";
      $var_lvl_h = "0";
      $var_lvl_i = "0";
      $var_lvl_j = "0";
      $var_lvl_k = "0";
      $var_lvl_l = "0";
      $var_lvl_m = "0";
      $var_lvl_n = "0";
      $var_lvl_o = "0";
      $var_lvl_p = "0";
      $var_lvl_q = "0";
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
      <title>Access Levels</title>
      <script language=JavaScript>
        function calcLevels() {
          var mask = 1;
          var lev = <?php echo $var_total;?>
    You'll also see shorthand for echo like this:

    <?=$var_total;?>

    it does the same thing as echo, just with fewer keystrokes.

    I'm not really sure what you want to do with question 2, but hopefully this helps you along the way.


  •  

    Posting Permissions

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