Hi I am very new to PHP and was working through a tutorial to learn a little about PHP. I created a simple form that passes form variables to another page to be displayed. It's a coin counter that when the user enters certain amounts of different denominations of coins, it is then passed, added up and output.

I have all the code for both pages, and I dont get any errors, but the totals dont actually add. There is just "0" for the answers.
Here is what I have, please if anyone can tell me whats wrong and why, I would greatly appreciate it.

Here is my simple form:

<form action="fancyCoin.php" method="post">
        <table>
          <tr>
            <th>Pennies:</th>
            <td><input type="text" name="pennies" /></td>
          </tr>
          <tr>
            <th>Nickels:</th>
            <td><input type="text" name="nickels" /></td>
          </tr>
          <tr>
            <th>Dimes:</th>
            <td><input type="text" name="dimes" /></td>
          </tr>
          <tr>
            <th>Quarters:</th>
            <td><input type="text" name="quarters" /></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><input type="submit" value="Submit" /></td>
          </tr>
        </table>
      </form>

Here is my code:

<table width="200" border="1">
        <tr>
          <th colspan="2">Coin Counter</th>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Denomination</th>
          <td>Number</td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Pennies</th>
          <td align="right"><?php echo (int)$_POST['pennies'] ?></td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Nickels</th>
          <td align="right"><?php echo (int)$_POST['nickels'] ?></td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Dimes</th>
          <td align="right"><?php echo (int)$_POST['dimes'] ?></td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Quarters</th>
          <td align="right"><?php echo (int)$_POST['quarters'] ?></td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Total Coins</th>
          <td align="right"><?php echo $totalCents='pennies' + ('nickels'*5)+('dimes'*10)+('quarters'*25) ?></td>
        </tr>
        <tr>
          <th bgcolor="#CCCCCC">Total</th>
          <td align="right"><?php echo $total= ($totalCents/100) ?></td>
        </tr>
        <tr>
          <th>&nbsp;</th>
          <td>&nbsp;</td>
        </tr>
      </table>

Recommended Answers

All 2 Replies

'nickels' is a string, NOT a variable. Your variable is $_POST['nickels'] So do:
intval(VARIABLE) * MULTIPLIER:

<td align="right"><?php echo $totalCents=intval($_POST['pennies']) + (intval($_POST['nickels'])*5) + (intval($_POST['dimes'])*10) + (intval($_POST['quarters'])*25); ?></td>

Thanks so much! Works fine. Is this this best way to do thistype of page? or is there another shorter way?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.