Jump to content

Any clue as to where i went wrong?


GetReady

Recommended Posts

This wont work for some reason and its begining to get to me, any chance of some help?

 

<?php

/* Calculator */


if($submit)
{
       echo $numa / $numb / $numb2;
   {
     
} else { ?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
  
  <p>number 1: </p>
  <p>
    <input type="text" name="numa" size="10">
  </p>
  <p>number 2: </p>
  <p>
    <input type="text" name="numb" size="10">
  </p>
  <p>number 3: </p>
  <p>
    <input type="text" name="numb2" size="10">
  </p>
  <p>
    <input type="submit" value="Calculate output!" name="submit">
        </p>
</form>
<?php } ?>

Link to comment
Share on other sites

Initially, you have an extra closing curly brace, but what is this code supposed to do? Echo the result of $numa divided by $numb divided by $num2, or just echo their values separated by slashes?

 

 

EDIT: It also appears to rely on register_globals = On, which it shouldn't be, so you need to assign integer values to the variables via $numa = (int) $_POST['numa'];, etc.

 

if($submit)
{
       echo $numa / $numb / $numb2;
   { // get rid of this one.     
} else { 
?>

Link to comment
Share on other sites

Sorry my php skills are amateur to say the least, Its supposed to divide each number e.g numa divided by numb divided by numb2, Its how ever not echoing the results... I take it ive gone wrong with that function somehow also? Any help is appreciated Thanks!

 

Link to comment
Share on other sites

Try something like this

 

<?php
// This says if submit has been hit process it otherwise show the form
if (isset($_POST['submit'])) {
// process
        // Get all the values the form sent from $_POST
$numa = $_POST['numa'];
$numb = $_POST['numb'];
$num2 = $_POST['num2'];
        // Do the division
$result = $numa/$numb/$num2;
        // echo the result
echo $result;
} else {
// display
?>
<form method="POST"> <!-- No need to use PHP_SELF...if it's not specified, it will automatically use itself -->
<p>Number 1:</p><p><input type="text" name="numa" size="10"></p>
<p>Number 2:</p><p><input type="text" name="numb" size="10"></p>
<p>Number 3:</p><p><input type="text" name="num2" size="10"></p>
<p><input type="submit" name="submit" value="Calculate output!" /></p>
</form>
<?php
}
?>

 

Hope I understood you right. This allows you to verify all the variables hold what they should

Link to comment
Share on other sites

If you mean without re-loading the page, you will need some sort of client side AJAX script, I use jQuery.

 

The other option is to submit to itself (like you are) and then put the echo command after the form (remove the if statement to check if it has been submitted). If you wanted the values to stay in the form, use value="<?php echo $_POST['numa']; ?>" to set the values of the field.

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.