Jump to content

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.

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.