soupi Posted October 29, 2013 Share Posted October 29, 2013 (edited) <?php $expression = ''; $input = array( 'a' => 5, 'b' => 10, 'c' => 15, 'd' => 20); $total = 0; $sum = ''; // Override default expression and input values if(isset($_POST['Calculate'])) { $input = $_POST['input']; //input being passed via POST method $expression = $_POST['expression']; //expression being passed via POST method } // make sure the users expression is safe to use if(preg_match('#^[a-z+-\s]+$#i', $expression)) { $sum = $expression; //evaluates sum as expression $input_letters = array(); //puts letters into a array $input_operators = array(); //puts operators into a array for ($i = 0; $i < strlen($expression) ; $i++) { //gets string length if (($i % 2) == 0) { $input_letters[] = $expression[$i]; //getting the character for the position i } else { $input_operators[] = $expression[$i]; } } // for ($i = 0 ; $i < sizeof($input_letters); $i++) { //size of= numbers (4) $value = $input[$input_letters[$i]]; //takes the value from the letters if ($i == 0) { $sum += $value; //adds the sum if = 0 } else { if ($input_operators[$i-1] == '+') { //checks to see if the operator is + $sum += $value; } else if ($input_operators[$i-1] == '-') { //checks to see if the operator is - $sum -= $value; } } print_r($value); } } else { echo "Error: expression not correct form"; //error message } ?> <form action="" method="post"> <!--Form begins--> Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/> Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br> Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br> Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br> <br> Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> = <?php echo $sum; ?><br> Sum: <?php echo $sum; ?><br> <input type="submit" name="Calculate" /> </form> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script> $( "form" ).submit(function( event ) { var myExpression = $('[name="expression"]').val(); if $("$input_letters").validate({ myExpression: { maxlength: 4 } }; } else { alert("Enter only four expressions"); } }); </script> in the code i provided, how would i add a javascript on the bottom check to see that user entered four letters, if they enter more then for expressions, then when submit is clicked, put a alert message saying you cant. it starts in the script section but Im not sure why it isnt working when i click submit after I run it. Edited October 29, 2013 by soupi Quote Link to comment https://forums.phpfreaks.com/topic/283414-help-with-alert-message-displaying/ Share on other sites More sharing options...
Solution soupi Posted October 30, 2013 Author Solution Share Posted October 30, 2013 SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/283414-help-with-alert-message-displaying/#findComment-1456202 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.