Jump to content

help with $-Post function


happy02134

Recommended Posts

I coded this form calculator using php..while there are no syntax errors, I have messed something up in the coding and I cannot figure out what. Everything works however each button does not do it's job. i.e. when i press * for multiply it adds and same for all the other buttons. It only adds: below is my code :

 

 

 
<?php
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] ==""))
  
if ($_POST["calc"] = "+") {
  $result = $_POST[num1] + $_POST[num2];
} else if ($_POST[calc] == "-") {
  $result = $_POST[num1] - $_POST[num2];
} else if ($_POST[calc] == "*") {
  $result = $_POST[num1] * $_POST[num2];
} elseif ($_POST[calc] == "/") {
  $result = $_POST[num1] / $_POST[num2];
}
 
?>
 
<!doctype html> 
<html lang="eng"> 
<head> 
<meta charset="utf-8"> 
<title>Multiply</title> 
</head> 
 
<body style="background-color:lightgrey;"> 
<div id="header"> 
    <h1>Multiply</h1> 
    <hr /> 
</div> 
 
<div id="content"> 
    <form name="calc" action="multiply.php" method="post"> 
        <p>Number 1: <input type="text" name="num1"></p> 
        <p>Number 2: <input type="text" name="num2"></p> 
        <input type='submit' name='calc' value='+'> 
        <input type='submit' name='calc' value='-'>
        <input type='submit' name='calc' value='*'>
        <input type='submit' name='calc' value='/'>
        <?php
echo "<p>The result of the calculation is: $result</p>";
?>
    </form> 
    
</div> 
 
 
</body> 
 
</html>
Link to comment
Share on other sites

Please use [php] or [code] tags for your PHP code.

 

Striving for no syntax errors is really great, but you should avoid warnings and notices too. Your script has a few of them.

 

help with $-Post function

$_POST is a variable, not a function.

if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] ==""))

- If I haven't submitted the form yet then $_POST will not contain anything. You should check that these things actually exist before trying to use them.

- For anything other than a trivial if block you should use {}s.

if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] ==""))
// and almost everywhere else you use $_POST

val1, val2, and calc are not constants. If you want strings then you need to use quotes.

if ($_POST["calc"] = "+") {

Count your equals signs.

Edited by requinix
Link to comment
Share on other sites

Thanks for the help. I understand the $_Post variable now and fixed my issues. I am just a little confused still about the output. For example, I am currently adding the is_numeric () function. I coded it like this:

 

 

 

<?php
if (is_numeric ("num1 . num2"))
{
echo "";
} else {
echo "Numbers only please";
}
?>

 

 

 

However, "numbers only please" appears on the page before any values are even entered. I noticed it has done that before. I added this:

 

 echo "<p>The result of the calculation is: $result</p>"; 
 

 

under my form so the answer would appear there when clicked but it always appears there even before any buttons are clicked. Shouldn't it appear only when I click the button? I am new to PHP, and everything I am reading tells me to uses those variables/functions. Should I be using a different approach to achieve that?

Edited by happy02134
Link to comment
Share on other sites

You fixed it *how*?

<?php 
/* 
Name: Brittany Hafer 
Date: Feb. 28th 2013
Purpose: Homework 2  

*/ 
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$result = $num1 + $num2;

 
?> 
<!doctype html> 
<html lang="eng"> 
<head> 
<meta charset="utf-8"> 
<title>Add</title> 
</head> 

<body style="background-color:lightgrey;"> 
<div id="header"> 
    <h1>Add</h1> 
    <hr /> 
</div> 

<div id="content"> 
    <form name="frmInput" action="add.php" method="post"> 
        <p>Number 1: <input type="text" name="num1"></p> 
        <p>Number 2: <input type="text" name="num2"></p> 
        <p><input type='submit' name='btnSubmit' value='Add'></p> 

<?php echo "$num1 plus $num2 is " . number_format("$result",2);
if (is_numeric ("text"))
{
echo "";
} else {
echo "Numbers only please";
}
?>
    </form> 


</div> 

<div id="footer"> 
    <hr /> 
    <cite>Created for MIS3501</cite> 
</div> 

</body> 

</html>

 

I didn't fix my problems I am having but I changed the string. I just don't fully understand how to use these functions and variables 

Edited by happy02134
Link to comment
Share on other sites

I imagine it would have fixed most, if not all, of your problems since you're using completely different code.

 

is_numeric() wants something to check. If you tell it to look at the string "text" then it's going to respond with "no, that is not numeric".

it doesn't respond. "no, that is not numeric" just constantly appears  on the page

Link to comment
Share on other sites

The homework is due today. Between here and Dev Shed it seems you need more personalized attention than we can give over the Internet. I suggest you set up an appointment with Mr. Shafer (apparently he's appointment only :( 12-1pm) and turn in the assignment a day or two late. But be sure to turn it in within a week: you'll lose many more points if you don't submit it at all.

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.