Jump to content

[SOLVED] How do you detect operators?


yakoup46

Recommended Posts

<html>

<title>File Test</title>

<body>

<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">

Enter Number <input type="text" name="num1">

<select>

  <option name="plus">+</option>

  <option name="sub">-</option>

  <option name="divid">/</option>

</select>

<input type="text" name="num2">

<input type="submit" value="Caclulate">

</form>

 

<?php

$num1 = @$_POST['num1'];

$num2 = @$_POST['num2'];

$plus = @$_POST['plus'];

$sub = @$_POST['sub'];

$divid = @$_POST['divid'];

if(empty($num1))

echo " ";

?>

 

</body>

</html>

 

So in this code i have a drop down menu with +,- , /. I was wondering how to have php detect whether operator i am using so that it can do the calculations?

Link to comment
https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/
Share on other sites

Should be like this instead:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter Number <input type="text" name="num1">
<select name="operator">
 <option value="plus">+</option>
 <option value="sub">-</option>
 <option value="divid">/</option>
</select>
<input type="text" name="num2">
<input type="submit" value="Calculate">
</form>

 

From thereon it should be fairly simple to figure out. You can get the operator using $_POST['operator'].

Because $_POST['plus'] doesn't exist. There is no field called "plus" in the form, but rather a field with the value "plus". There is a difference. Also, = is the assignment operator, and == is the comparison operator. It's covered in the language reference chapter in the manual.

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.