Jump to content

[SOLVED] Trying to perform calculation with post data


dbillings

Recommended Posts

I've setup a form that submits two values and a select menu with a list of bitwise operators. The problem I'm having is I can't get the php to do the math when I display the results it simply displays the values as characters. I'm sure it's something simple that I should be able to figure out on my own. For instance if I submit 0111 >> 2 that is exactly what I see in my php how do I get it to complete the right shift?

 

<?php

IF(isset($_REQUEST['submit'])){

$a = $_REQUEST['a'];
$b = $_REQUEST['b'];
$op = $_REQUEST['operator'];


$example= "\$a ". $op ." \$b = ";
$bitmath= "$a $op $b";

echo "Format: $example \n";
echo "$a $op $b";

} 
$operator = array("AND"=>"&","OR"=>"|","XOR"=>"^","NOT"=>"~","SHIFT-LEFT"=>"<<","SHIFT-RIGHT"=>">>");

?>

<fieldset><legend>Bit Wise - Hands on learning</legend>
<form name="convert" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>$a </label><input type="text" name="a">
<select name="operator">
<?php foreach($operator as $key => $value){

echo "<option title=\"$key\" value=\"$value\">$value</option>";

}

?>
</select>
<label>$b </label><input type="text" name="b"><br /><br />
<input align="center" type="submit" name="submit" value="Calculate">

</form>
</fieldset>

Here's a bump with a summary of the problem. I want to turn my post data into an operator and execute the math and display the result for instance....

<?php



IF(isset($_REQUEST['submit'])){

$a= $_REQUEST['a'];
$b= $_REQUEST['b'];
$op= $_REQUEST['operator'];



echo "$a $op $b";

}

$operator= array("Multiply"=>"*","Divide"=>"/","Add"=>"+","Subtract"=>"-");

?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Value $a </label><input type="text" name="a">
<select name="operator">
          <option value="">-Operator-</option>
<?php
          foreach($operator as $key => $value){

          echo "<option title=\"$key\">$value</option>";

          }
?>
</select>
<label>Value $b </label><input type="text" name="b">
<br />
<input name="submit" type="submit" value="submit">
</form>

Good enough.

<?php



IF(isset($_REQUEST['submit'])){

$a= intval($_REQUEST['a']);
$b= intval($_REQUEST['b']);
$op= $_REQUEST['operator'];

echo "$a $op $b = ";

IF($op == "&"){


echo $a & $b;

}elseif($op == "|"){


echo $a | $b;

}elseif($op == "^"){


echo $a ^ $b;

}elseif($op == "~"){


echo ~$a & $b;

}elseif($op == ">>"){


echo $a >> $b;

}elseif($op == "<<"){


echo $a << $b;

}

}

$default= $_REQUEST['operator'];



$operator= array("AND"=>"&","OR"=>"|","XOR"=>"^","NOT"=>"~","Shift left"=>">>","Shift right"=>"<<");

?>
<fieldset><legend>Bitwise examples </legend>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Value $a </label><input type="text" name="a">
<select name="operator">
          <option>-Operator-</option>
<?php
          foreach($operator as $key => $value){

	IF($default== $value){

		$selected= "selected";

	}else{

		$selected= "";

	}

          echo "<option $selected title=\"$key\">$value</option>";

          }
?>
</select>
<label>Value $b </label><input type="text" name="b">
<br />
<input name="submit" type="submit" value="submit">
</form>
</fieldset>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.