isuckat_php Posted March 29, 2010 Share Posted March 29, 2010 I am running into a few problems. 1) It calculates: The freegumpher handicap is:20.3 The problem with this is that i dont want it to output anything until the user gives numbers. i dont know where it came up with his number 2) Everytime I input a number it outputs: Oops, you forgot to supply some information. Please correct the problem below. The problem with this is that i dont want that message to show up unless one of the boxes is not filled after calculation button is clicked. this is the webpage: http://athena.ecs.csus.edu/~cs010129/freegumph.php this is the adjusted code: <html> <body> <?php if ($submit) { if (($score_1 == "") || ($score_2 =="") || ($score_3 =="") || (!$calc)) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot to supply some information. Please correct the problem below.</b></font><br>"); } elseif ((!ereg("[0-9]",$score_1)) || (!ereg("[0-9]",$score_2) || (!ereg("[0-9]",$score_3)))) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict your input to only numbers.</b></font><br>"); } } else { $avg=($score_1 + $score_2 + score_3)/3; if ($avg >= 29) { echo $total = 0; } else { $total = ((29 - $avg) * (7/10)); } print("The freegumpher handicap is:". $total); } ?> <form method="post" action="freegumph.php"> Enter your three most <i>recent</i> Freegumpher scores to calculate Handicap:<br/> <br/> <input type="text" name="score_1" size=8 <input type="text" name="score_2" size=8 <input type="text" name="score_3" size=8 /><br /><br/> <input type="submit" name="submit" value="Calculate Handicap"/> </form> </body> </html> Help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
o3d Posted March 29, 2010 Share Posted March 29, 2010 To test wether a form or variable was submitted it is good practice to test with the following method: <?PHP if (isset($_GET['submit'])) { if (isset($_GET['score_1']) && $_GET['score_1'] != '' && isset($_GET['score_2']) && $_GET['score_2'] != '' && isset($_GET['score_3']) && $_GET['score_3'] != '') { if (do other checks ie. is_numeric... regexpr...) { //error echo "<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict your input to only numbers.</b></font><br>"; } else { //good to continue $avg=($_GET['score_1'] + $_GET['score_2'] + $_GET['score_3'])/3; if ($avg >= 29) { $total = 0; else $total = ((29 - $avg) * (7/10)); print("The freegumpher handicap is:". $total); } } else { //error echo "<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot to supply some information. Please correct the problem below.</b></font><br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted March 29, 2010 Share Posted March 29, 2010 <html> <body> <?php if($HTTP_SERVER_VARS['REQUEST_METHOD']=='POST'){ $submit=$_POST["submit"]; $score_1=$_POST["score_1"]; $score_2=$_POST["score_2"]; $score_3=$_POST["score_3"]; if ($submit) { if (($score_1 == "") || ($score_2 =="") || ($score_3 =="")) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot to supply some information. Please correct the problem below.</b></font><br>"); } elseif ((!ereg("[0-9]",$score_1)) || (!ereg("[0-9]",$score_2) || (!ereg("[0-9]",$score_3)))) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict your input to only numbers.</b></font><br>"); } else { $avg=($score_1 + $score_2 + $score_3)/3; if ($avg >= 29) { echo $total = 0; } else { $total = ((29 - $avg) * (7/10)); } print("The freegumpher handicap is:". $total); } } } ?> <form method="post" action="freegumph.php"> Enter your three most <i>recent</i> Freegumpher scores to calculate Handicap:<br/> <br/> <input type="text" name="score_1" size=8 <input type="text" name="score_2" size=8 <input type="text" name="score_3" size=8 /><br /><br/> <input type="submit" name="submit" value="Calculate Handicap"/> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.