Jump to content

[SOLVED] Whats the problem?


Recommended Posts

Hey, i don't want you to re write the whole program, but just fix the error or tell me what the error is:

 

I am making a Addition Form. The problem is use it, on the addition.php the answer always comes out as 0... the answer should be coming from maths.php here are the codes:

 

Maths.php

<html>
<head>
<title>Maths Addition</title>
</head>
<body bgcolor="black">
<font color="white">
<center><h1>Addition Form</h1>
<form method="post" action="addition.php">
<input type="text" name="x" value="0"> <font color="red"><b>+</b></font> <input type="text" name="y" value="0">
<br /><br />
<input type="submit" value="Do the addition!">
</form>
<br /><br /><br /><br /><br /><table border="1">
<tr>
<td><font color="white">Made by <font color="blue">Liam Monks</font></font></td>
</tr>
</table>
</font>
</body>
</html>

 

AND

 

Addition.php

<html>
<head>
<title>Maths Addition</title>
</head>
<body bgcolor="black">
<font color="white">
<center><h1>Addition Form</h1>
<?php

echo "The answer to your addition is...";
echo ($x + $y);

?>
<br /><br /><br /><br /><br /><table border="1">
<tr>
<td><font color="white">Made by <font color="blue">Liam Monks</font></font></td>
</tr>
</table>
</font>
</body>
</html>

 

Thanks, i can't  find the error

 

Link to comment
Share on other sites

You are assuming register_globals is on, which is very bad and is a huge security risk. Good thing they are not on (thus the zero).

 

Try this:

 

<html>
<head>
<title>Maths Addition</title>
</head>
<body bgcolor="black">
<font color="white">
<center><h1>Addition Form</h1>
<?php
$x = isset($_GET['x'])?$_GET['x']:0;
$y = isset($_GET['y'])?$_GET['y']:0;
echo "The answer to your addition is...";
echo ($x + $y);

?>
<br /><br /><br /><br /><br /><table border="1">
<tr>
<td><font color="white">Made by <font color="blue">Liam Monks</font></font></td>
</tr>
</table>
</font>
</body>
</html>

 

See if that gets you an answer.

Link to comment
Share on other sites

<?php
// if $_POST['x'] has been set, assign $x the value inside of $_POST['x'] else assign it zero.
// this is called the ternary operator (?  which is really a shortened version of 
// the if/else. 
$x = isset($_POST['x'])?$_POST['x']:0;
$y = isset($_POST['y'])?$_POST['y']:0;
echo "The answer to your addition is...";
echo ($x + $y);

?>

 

A side note, it should have been $_POST not $_GET as we are posting the data from the form and not getting.

 

Hope that helps for more information google php ternary operator

Link to comment
Share on other sites

Thanks, I noticed it should of been POST i was like what the hell... LOL

 

but anyway, here is the result: http://www.liamproductions.com/maths.php

 

using POST or GET is fine, but both of those reside in $_REQUEST - I would get into the habit of using $_REQUEST for submitted data instead of using either $_POST or $_GET, it makes the code cleaner and easier to read and understand.

Link to comment
Share on other sites

one problem, though it may be small, if you use really large number like 123455421526623616546542145 and try adding 215454842623656166532165 it gives a really odd answer like 1.3684765454894565E+17

 

Really odd answer? You do know that is a valid answer called Scientific notation right?

 

But yes if you using the standard math functions in PHP that is what you get, there is an extension, not sure what it is called, that can be installed to avoid that.

 

 

Reply to Yzerman,

 

$_REQUEST works, but how I see it is, I want to know that I am getting my data how I intended. Using the $_REQUEST, someone just has to append that data on to the URL and it will still be read, could be a potential security risk.

 

That is why I stick to $_GET and $_POST.

Link to comment
Share on other sites

Only on pages you are posting data to. You do not "HAVE" to do that, but that will avoid errors and make the script run more efficient. Plus provides a layer of security for you.

 

Just good coding practice.

Link to comment
Share on other sites

oh ok, but won't this make the pages more longer than normal?

 

No, not really I guess.

 

I may add a few extra lines for variable declaration, but that should be done anyways. Defining variables, although unnecessary in PHP is good practice to prevent unknown data from entering into the page which could be potentially harmful.

Link to comment
Share on other sites

Yeah but if you have register globals on... you don't have to type all that out do you?

 

You do not want register_globals on. Type all that out ? it really takes just a few seconds.

 

Anyhow if you want security, yes that is the best and securest way to do it. If you want to be left open, turn register_globals on. See how long it will take for a script kiddie to trash your site. Fun stuff.

Link to comment
Share on other sites

Thats when you get creative with the code.

 

<?php

for ($i=0; $i<50; $i++) {
      $questions[$i] = isset($_POST['question'.$i]):$_POST['question'.$i]:'';
}

echo 'First Question: ' . $questions[1];
?>

 

 

Link to comment
Share on other sites

Generally speaking, yes. Arrays are very nice because they are easily looped through, where as just a static variable, well you cannot really loop it etc.

 

You will find arrays have many uses, and make life much easier for the most part. Especially with x-dimensional arrays =)

Link to comment
Share on other sites

Yes, It was a very long time before I fully understood what arrays could be used for, and actually, I still don't know all about them. Like you, Liam, I am a little new to PHP, and finally got an ad-free server that runs Perl, PHP, MySQL, and a few others all for free. But if you are just testing, and don't want to do editing via FTP, if you do it that way, you can get WAMP(Windows Apache MySQL PHP) which is a free server that runs on your computer. http://www.wampserver.com/. But good luck on learning

Link to comment
Share on other sites

Thanks JP, I just installed Wamp Server about a hour ago. Its ok.

 

Its loops that i mainly don't understand but i guess i will if i keep looking at loop tutorials... jEdit helps seen as i have changed the colors to suit me... I like the black background i set because i feel like a coder then :P

Link to comment
Share on other sites

Thanks JP, I just installed Wamp Server about a hour ago. Its ok.

 

Its loops that i mainly don't understand but i guess i will if i keep looking at loop tutorials... jEdit helps seen as i have changed the colors to suit me... I like the black background i set because i feel like a coder then :P

 

black background? That would kill my eyes, I tried that a long time ago, never stuck.

 

My favorite program is Notepad++ for small projects and see my sig for eclispe for bigger projects.

 

Loops are actually pretty easy, but most people struggle with them. I remember a time when I used to get infinite loops, but now it just never happens.

 

Anyhow best of luck in your coding endeavors, remember to post here for help.

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.