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 6 of 6

Thread: code problem

  1. #1
    e^z
    e^z is offline
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code problem

    I am currently working on a game and I am having problems with a script. I want a form to recognize 2 things while the user is putting info into the form. 1) I want it to recognize a user variable (in this case energy_left) and 2) I want it to recognize how much energy is needed for the workout chosen in the select input. Now I got it to calculate the energy needed for the workout and show the user what it is. And now I want it to show if the user has sufficient energy for the workout. But I cannot get it to work. If I could get some help, that would be great. Thanks.


    heres my code:

    Code:
    echo "<form action=school.php?action=work$g_link METHOD=POST name=\"gym\">";
    echo "<table border=0 width=86%><tr><td>&nbsp;</td><td>Workout</td><td>Difficulty</td></tr><tr>";
    echo "<td><b>Resistance Training</b></td><td><SELECT name=\"res_train\"><option value=\"\">Select Workout<option value=dumb_bells>Dumb Bells<option value=bench>Benchpress<option value=leg_press>Legpress<option value=curls>Curls</select></td><td><SELECT name=\"diff_res\" onFocus=\"startCalc();\" onBlur=\"stopCalc();\">><option>Select Difficulty<option value=0.3>1<option value=0.6>2<option value=0.9>3<option value=1.2>4<option value=1.5>5</select></td></tr><tr>";
    echo "<td><b>Cardiovascular Training</b></td><td><SELECT name=\"cardio\"><option value=\"\">Select Workout<option value=bike>Bike<option value=treadmill>Treadmill<option value=skip>Skipping<option value=long_run>Running</select></td><td><SELECT name=\"diff_cardio\" onFocus=\"startCalc();\" onBlur=\"stopCalc();\"><option>Select Difficulty<option value=0.1>1<option value=0.2>2<option value=0.4>3<option value=0.8>4<option value=1.6>5</select></td><input type=text name=test_energy value=\"$user[ene_left]\"></tr><tr>";
    ?>
    <script type="text/javascript">
    function startCalc(){
      interval = setInterval("calc()",1);
    }
    function calc(){
      one = document.gym.diff_res.value;
      two = document.gym.diff_cardio.value; 
      document.gym.tot_energy.value = (one * 1) + (two * 1);
    }
    function stopCalc(){
      clearInterval(interval);
    }
    </script>
    <td colspan=1><b>Energy Required</b></td>
    <td colspan=2><input type=text name=tot_energy size=16></td>
    </tr>
    <tr>
    <td colspan=1><b>Sufficient Energy</b></td>
    <td colspan=2>
    <script type="text/javascript">
    if ('document.gym.tot_energy.value' < document.gym.test_energy.value) 
    {
    document.write("<img src=green.gif>");
    }
    else if ('document.gym.tot_energy.value' > document.gym.test_energy.value)
    {
    document.write("<img src=red.gif>");
    }
    else
    {
    document.write("<img src=yellow.gif>");	
    }	
    </script>
    </td>
    <?
    echo "<tr><td><INPUT type=\"submit\" name=\"submit\" value=Submit size=\"14\" maxlength=\"20\"></form></td></tr></table>";
    echo "<br><center><a href=school.php?action=gym$g_link>Back</a></center>";

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you've possibly got an interesting question, but when you say you can't the other bit to work, at which point couldn't it work, or what where you trying to do, or etc...

    Unless the problem is actually a javascript variable access propblem or display problem, then this question would be better resolved by precisely stating the logic flow in plain english. One would imagine, as wild guess, the solution would be a simply negation... but even on reading the code below, their is nothing ( that I can see ) that updates the document.writes... so where to start if you see what I mean... also not sure if a select list is really the best way of specifying the values unless, that it is you're trying to dynamically change these values during runtime ?

    In either case, why not write out the calc for the one that does work and the exected calc for the one that doesn't ?

  • #3
    e^z
    e^z is offline
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok here is an example of what happens:

    Energy: 2.05/2.55



    Workout Difficulty
    Resistance Traning: Curls 4(should take 1.2 energy)
    Cardiovascular Training: Bike 5(should take 1.6 energy)

    Energy Required: 2.8
    Sufficient Energy: red or green dot SHOULD show up



    Now, I have a hidden field in there that takes a value from the database and sets how much energy you have left into a field (test_energy). Everything works except the sufficient energy part, I will hosw you the code I have been trying to use.

    Code:
    <script type="text/javascript">
    function startTest(){
      if (document.gym.tot_energy.value <= document.gym.test_energy.value) 
      {
      testy=1;
      }
      else
      {
      testy=2;
      }
    }
    </script>
    <td colspan=1><b>Energy Required</b></td>
    <td colspan=2><input type=text name=tot_energy size=16 onChange="startTest();"></td>
    </tr>
    <tr>
    <td colspan=1><b>Sufficient Energy</b></td>
    <td colspan=2>
    <script type="text/javascript">
    if (testy == 1)
    {
    document.write("<img src=green.gif>");
    }
    else
    {
    document.write("<img src=red.gif>");	
    }	
    </script>

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You shouldn't really of had any problems... heres a similiar test page.
    Code:
    <html>
    <head>
    <title>Main</title>
    <script type="text/javascript">
      function startTest()
      {
        if (document.gym.tot_energy.value <= document.gym.test_energy.value)
          document.getElementById('energy').src="green.gif";
        else
          document.getElementById('energy').src="red.gif";
      }
    </script>
    </head>
    <body>
    <form name="gym">
    <h4>Energy Required</h4>
    <input type="text" name="tot_energy" size="16" onChange="startTest();" />
    <input type="hidden" name="test_energy" value="8"/>
    <br />
    <h4><b>Sufficient Energy</h4>
    <img id="energy" src="red.gif">
    <form>
    </body>
    </html>

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heres a combined test page...
    Code:
    <html>
    <head>
    <title>Main</title>
    <script type="text/javascript">
      function startTest()
      {
        
    		//if (document.gym.tot_energy.value <= document.gym.test_energy.value)
          //document.getElementById('energy').src="green.gif";
        //else
          //document.getElementById('energy').src="red.gif";
    			
    		if (document.gym.tot_energy.value < document.gym.test_energy.value)
    			
    			document.getElementById('energy').src="green.gif";
    			
    		else if (document.gym.tot_energy.value > document.gym.test_energy.value)
    		
    			document.getElementById('energy').src="red.gif";
    			
    		else
    		
    			document.getElementById('energy').src="yellow.gif";	
      }
    
      function startCalc()
      {
        interval = setInterval("calc()",1);
      }
    
      function calc()
      {
        one = document.gym.diff_res.value;
        two = document.gym.diff_cardio.value;
        document.gym.tot_energy.value = (one * 1) + (two * 1);
        startTest();
      }
    
      function stopCalc()
      {
        clearInterval(interval);
      }
    </script>
    </head>
    <body>
    <form name="gym">
    
    <h4>Resistance Training</h4>
    <SELECT name="res_train"><option value="">Select Workout<option value=dumb_bells>Dumb Bells<option value=bench>Benchpress<option value=leg_press>Legpress<option value=curls>Curls</select></td><td><SELECT name="diff_res" onFocus="startCalc();" onBlur="stopCalc();">><option>Select Difficulty<option value=0.3>1<option value=0.6>2<option value=0.9>3<option value=1.2>4<option value=1.5>5</select>
    <h4>Cardiovascular Training</h4>
    <SELECT name="cardio"><option value="">Select Workout<option value=bike>Bike<option value=treadmill>Treadmill<option value=skip>Skipping<option value=long_run>Running</select></td><td><SELECT name="diff_cardio" onFocus="startCalc();" onBlur="stopCalc();"><option>Select Difficulty<option value=0.1>1<option value=0.2>2<option value=0.4>3<option value=0.8>4<option value=1.6>5</select>
    <br /><br />
    <h4>Energy Required</h4>
    <input type="text" name="tot_energy" size="16" onChange="startTest();" />
    <br />
    <input type=text name=test_energy value="8">
    <br />
    <h4><b>Sufficient Energy</h4>
    <img id="energy" src="yellow.gif">
    <form>
    </body>
    </html>

  • #6
    e^z
    e^z is offline
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why wont it work.... Do0es anyone have msn or something I can talk to you live, I need you to login to my game and check it out. If anyone could, that would be great. Thanks.


  •  

    Posting Permissions

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