Jump to content

help with alert message displaying


soupi
Go to solution Solved by soupi,

Recommended Posts

<?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 by soupi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.