Php

richm8026

New Member
Messages
138
Reaction score
0
Points
0
Alright, I wrote this calculator program and it's working now, but how can I do it instead of a using a header("Location: index2.html");
exit;

???

I want to write all of the codes inside of one PHP Script, named index.php, I tried it the same with the header, but it throw an error about always looping, how can I fix this???
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Use either $_GET or $_POST to check what action has been called. There is no need for the header command.
 

richm8026

New Member
Messages
138
Reaction score
0
Points
0
Alright

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

What goes in here???

}
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Oh for the love of...

If anythings blank then surely the application should just fail? Use die("Incomplete form");
 

richm8026

New Member
Messages
138
Reaction score
0
Points
0
Thanks, I am pretty new to PHP, my PHP5 Book and all it showed was the Header thingy... I got the calculatator from that book, I am just glad I remember it all now, I've been deleting the files and then redoing them for the sake of memorizing...
 

richm8026

New Member
Messages
138
Reaction score
0
Points
0
I tried that man, it didn't work, all it did was display Known File Type
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
I want to help you but I really don't understand what you are trying to do. You are trying to put all the code in one file and submit to that same file to calculate the results? Then what is it you need help with? If you could post in the code.. You don't need to use header if you are going to go to the same page, afterall you are already in there.
 

richm8026

New Member
Messages
138
Reaction score
0
Points
0
So, I don't need the if (($_POST[val1] == " ") || ($_POST[val2] == " ") || ($_POST[calc] == " ")) {

Some other codes;

}

????
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
post the whole code, and i'll try to help out. I can't really tell what's going wrong by just displaying the $_POST vars conditional. also, if the vars are being sent through the url ?var1=5&var2=2&calc=true , then you would use $_GET.
 
Last edited:

richm8026

New Member
Messages
138
Reaction score
0
Points
0
Code:
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) {

    die("Unknown file name");

}

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

     $result = $_POST[val1] + $_POST[val2];

     }

else if ($_POST[calc] == "subtract") {

     $result = $_POST[val1] - $_POST[val2];

     }

else if ($_POST[calc] == "multiply") {

     $result = $_POST[val1] * $_POST[val2];

     }

else if ($_POST[calc] == "divide") {

     $result = $_POST[val1] / $_POST[val2];

     }

?>










<html>

<head>

<title>Calculator</title>

</head>

<body>

<form method="POST" action="index.php">

<p>Value 1: <input type="text" name="val1" size=10></p> <br>
<p>Value 2: <input type="text" name="val2" size=10></p> <br>
<p><input type="radio" name="calc" value="add"> Add</p> <br>
<p><input type="radio" name="calc" value="subtract"> Subtract</p> <br>
<p><input type="radio" name="calc" value="multiply"> Multiply</p> <br>
<p><input type="radio" name="calc" value="divide"> Divide</p> <br> <br>


<p><input type="submit" name="submit" value="view results" align="left"></p><br><br>
</form>



<p>The result of the calculations is: <?php echo "$result"; ?>


</body>

</html>
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
ok, gonna debug it now and test it out

the following works:
PHP:
<?php

if (isset($_POST['submit'])) // checks if you clicked submit (name='submit')
{
	// or else this would always be parsed false because you didn't get to enter anything yet.
	// empty is better than if ($value == '') because it also strips any spaces iirc.  !empty is better than isset also ;)
	if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['calc']))
		die("Missing Field");
		
	if ($_POST['calc'] == 'add')
	{
	    $result = $_POST['val1'] + $_POST['val2'];
	}
	elseif ($_POST['calc'] == 'subtract')
	{
	    $result = $_POST['val1'] - $_POST['val2'];
	}
	elseif ($_POST['calc'] == 'multiply')
	{
	    $result = $_POST['val1'] * $_POST['val2'];
	}
	elseif ($_POST['calc'] == 'divide')
	{
	    $result = $_POST['val1'] / $_POST['val2'];
	}
}
?>

<html>
<head>
<title>Calculator</title>
</head>
<body>
<?php if (!empty($result)): // little mod by me.  it just hides the results until the var $result returns true. ?>
<p>The result of the calculations is: <?php echo $result; ?></p>
<?php endif; ?>
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<p>Value 1: <input type="text" name="val1" size=10></p>
<p>Value 2: <input type="text" name="val2" size=10></p>
<p><input type="radio" name="calc" value="add"> Add</p>
<p><input type="radio" name="calc" value="subtract"> Subtract</p>
<p><input type="radio" name="calc" value="multiply"> Multiply</p>
<p><input type="radio" name="calc" value="divide"> Divide</p><br />
<p><input type="submit" name="submit" value="view results" align="left"></p>
</form>
</body>
</html>
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
alright, there's your working code (my post above), with comments to help you out along the way ;). the problem with your code was that there wasn't anything preventing the first if ($_POST['var1'] == '') from executing, thus it would always execute on load, giving you the error because you didn't set the value yet.

and i'm gonna post another version, doing the same thing, just with a diff method ;)

PHP:
<?php

if (isset($_POST['submit'])) // checks if you clicked submit (name='submit')
{
    // or else this would always be parsed false because you didn't get to enter anything yet.
    // empty is better than if ($value == '') because it also strips any spaces iirc.  !empty is better than isset also ;)
    if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['calc']))
        die("Missing Field");

    $calc = $_POST['calc'];
    switch($calc)
    {
        case 'add':
            $result = $_POST['val1'] + $_POST['val2'];
            break;

        case 'subtract';
            $result = $_POST['val1'] - $_POST['val2'];
            break;

        case 'multiply':
            $result = $_POST['val1'] * $_POST['val2'];
            break;

        case 'divide':
            $result = $_POST['val1'] / $_POST['val2'];
            break;
    }
}
?>

<html>
<head>
<title>Calculator</title>
</head>
<body>
<?php if (!empty($result)): // little mod by me.  it just hides the results until the var $result returns true. ?>
<p>The result of the calculations is: <?php echo $result; ?></p>
<?php endif; ?>
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<p>Value 1: <input type="text" name="val1" size=10></p>
<p>Value 2: <input type="text" name="val2" size=10></p>
<p><input type="radio" name="calc" value="add"> Add</p>
<p><input type="radio" name="calc" value="subtract"> Subtract</p>
<p><input type="radio" name="calc" value="multiply"> Multiply</p>
<p><input type="radio" name="calc" value="divide"> Divide</p><br />
<p><input type="submit" name="submit" value="view results" align="left"></p>
</form>
</body>
</html>
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
np, i try to make it easier for people to learn php. the next language i want to learn is c++ just for the sake of it. that or finish learning the rest of javascript (i'm okay, but not great :D)
 
Last edited:

richm8026

New Member
Messages
138
Reaction score
0
Points
0
Ooooh, I am learning WPF in Visual C# on Microsoft Windows, I use QT4 in Linux, so, I do know more C++ and C# than I do PHP, or JavaScript, but I know more HTML, and CSS than anything.
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
alright, there's your working code (my post above), with comments to help you out along the way ;). the problem with your code was that there wasn't anything preventing the first if ($_POST['var1'] == '') from executing, thus it would always execute on load, giving you the error because you didn't set the value yet.

and i'm gonna post another version, doing the same thing, just with a diff method ;)

PHP:
<?php

if (isset($_POST['submit'])) // checks if you clicked submit (name='submit')
{
    // or else this would always be parsed false because you didn't get to enter anything yet.
    // empty is better than if ($value == '') because it also strips any spaces iirc.  !empty is better than isset also ;)
    if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['calc']))
        die("Missing Field");

<?php if (!empty($result)): // little mod by me.  it just hides the results until the var $result returns true. ?>
<p>The result of the calculations is: <?php echo $result; ?></p>
<?php endif; ?>
The only problem with empty() is that "0" and 0 are also considered empty. And if the $result of calculation is 0 then it won't get displayed. I guess $value == '' is still better than isset() though.
PHP:
 if (isset($_POST['submit'])) // checks if you clicked submit (name='submit')
is indeed a good way to check it. ;)

As for $result, isset() will do fine since it will always contain the answer of the calculation only if it's set.

PHP is waaaay waaaay easier than C++... although it has almost the same syntax, if only the pointers and C strings were not there C++ would be easy to handle though... but it would lose it's power and lose its purpose
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
my first version actually had if (isset($result)): :D i should have left it, but i was trying to be consistent to avoid confusion ;)
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
your code was well written though. let's just call empty() a bug lol
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
ty, we actually don't need the {} in the if/elseif statements above since there's only one line of code ;) actually, I learned to clean my code considerably by working with fluxbb/punbb, since it's _very_ clean as far as the coding go. very easy to identify something o_O

so after further cleaning, and _bug_ removed:
PHP:
<?php

if (isset($_POST['submit'])) // checks if you clicked submit (name='submit')
{
    // or else this would always be parsed false because you didn't get to enter anything yet.
    // empty is better than if ($value == '') because it also strips any spaces iirc.  !empty is better than isset also ;)
    if (trim($_POST['val1']) == '' || trim($_POST['val2']) == '' || empty($_POST['calc']))
        die("Missing Field");
        
    if ($_POST['calc'] == 'add')
        $result = $_POST['val1'] + $_POST['val2'];

    elseif ($_POST['calc'] == 'subtract')
        $result = $_POST['val1'] - $_POST['val2'];

    elseif ($_POST['calc'] == 'multiply')
        $result = $_POST['val1'] * $_POST['val2'];

    elseif ($_POST['calc'] == 'divide')
        $result = $_POST['val1'] / $_POST['val2'];

}
?>

<html>
<head>
<title>Calculator</title>
</head>
<body>
<?php if (isset($result)): // little mod by me.  it just hides the results until the var $result returns true. ?>
<p>The result of the calculations is: <?php echo $result; ?></p>
<?php endif; ?>
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<p>Value 1: <input type="text" name="val1" size=10></p>
<p>Value 2: <input type="text" name="val2" size=10></p>
<p><input type="radio" name="calc" value="add"> Add</p>
<p><input type="radio" name="calc" value="subtract"> Subtract</p>
<p><input type="radio" name="calc" value="multiply"> Multiply</p>
<p><input type="radio" name="calc" value="divide"> Divide</p><br />
<p><input type="submit" name="submit" value="view results" align="left"></p>
</form>
</body>
</html>
 
Last edited:
Top