Jump to content

rebelprincess

New Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by rebelprincess

  1. The exercise I was asked to do for a PHP class, gave instructions to create a PHP form that would call back and display the same user data entered. Meaning, it would retain the values submitted and just display them back again to the user. The form had other instructions too, such as totaling an 'Amount' column and creating an if/else statement to check for non-numeric values placed in the 'Amount' column. If errors are detected in the non-numeric values it is supposed to display an error message and then total up the number of errors on the form. I was having trouble with the if/else statement: <table> <?php // ERROR COLUMN OF THE TABLE for ($row=1; $row<5; $row++) { $value_of_amount = '$amount_cell'.$row; $value_of_amount = $_POST['value_of_amount']; if (is_numeric($value_of_amount)) { echo "<tr><td> $value_of_amount </td></tr>"; } else { echo "<tr><td> Value is not a number </td></tr>"; } } ?> </table> To my understanding, the syntax of this PHP statement appears to be correct, but I'm not sure..... As a novice, this is all very confusing to me, but I thank you for your reply. - Kimberly
  2. This is the code for the entire form: <html> <form method = post> <body> <h1> My Bills </h1> <table width="375"> <tr> <td width="125"> <th> Item </th> </td> <td width="125"> <th> Amount </th> </td> <td width="125"> <th> Errors </th> </td> </tr> </table> <?php // CREATE AND DEFINE VARIABLES $item_name = $_POST['item_name']; $amount_cell = $_POST['amount_cell']; ?> <table> <tr> <td> <table> <?php // ITEM COLUMN OF THE TABLE for ($row=1; $row<5; $row++) { $item_name = 'item_name'.$row; $item_value = $_POST[$item_name]; echo "<tr><td> <input type=text name=".$item_name." value='".$item_value."'> </td></tr>\n"; } ?> </table> <td> <table> <?php // AMOUNT COLUMN OF THE TABLE for ($row=1; $row<5; $row++) { $amount_cell = 'amount_cell'.$row; $value_of_amount = $_POST[$amount_cell]; echo "<tr><td> <input type=text name=".$amount_cell." value='".$value_of_amount."'> </td>"; } ?> </table> <td> <table> <?php // ERROR COLUMN OF THE TABLE for ($row=1; $row<5; $row++) { $value_of_amount = 'amount_cell'.$row; if (is_numeric($value_of_amount)) { echo "<tr><td> </td>"; } else { echo "<tr><td> Value is not a number </td>"; } } ?> </tr> </table> </tr> </table> <br> <input type = "submit" value = "Submit"> <br> <?php // INSTRUCTIONS TO GET THE AMOUNT TOTAL $total = $_POST['$total']; $total = 0; for ($row=1; $row < 5; $row++) { $amount_cell = 'amount_cell'.$row; $value_of_amount = $_POST[$amount_cell]; $total = $total + $value_of_amount; } ?> <p> Total Bills: <?php // TO PRINT THE AMOUNT TOTAL echo $total ?> </p> </body> </form> </html>
  3. Hi Ken, thanks for your reply! I was using the for loop because I thought it would test the value of each line entered. I'm basically trying to get the code to do this: If the value entered is a number, I want the value to be echoed after the user clicks the submit button. If the value entered is not a number, I want "Value is not a number" to be echoed. Would I need to incorporate the $row as part of the if/else statement? (I'm still trying to understand the logic of PHP, sorry if I get confused on things )
  4. Hi, I'm a beginner just learning PHP right now, so my question is probably a simple one but I can't seem to figure this out. I'm trying to code the following if/else statement. When I've tested the code, the echo "Value is not a number" is appearing as if it were not part of the if/else statement. Is it not working because of the semicolon at the end of the first echo statement? Or am I missing another set of curly braces? I'm not sure how to approach this . . . <?php for ($row=1; $row<5; $row++) { $value_of_amount = $_POST['value_of_amount']; if (is_numeric($value_of_amount)) { echo "<tr><td> $value_of_amount </td>"; } else { echo "<tr><td> Value is not a number </td>"; } } ?>
×
×
  • 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.