Jump to content


Photo

help with alert message displaying



  • Please log in to reply
1 reply to this topic

#1 soupi

soupi

    Member

  • Members
  • PipPip
  • 22 posts

Posted 29 October 2013 - 02:43 PM

<?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, 29 October 2013 - 02:44 PM.


#2 soupi

soupi

    Member

  • Members
  • PipPip
  • 22 posts

Posted 30 October 2013 - 10:25 AM   Best Answer

SOLVED






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com