rebelprincess Posted March 2, 2011 Share Posted March 2, 2011 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>"; } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 2, 2011 Share Posted March 2, 2011 What is the value of the variable you're testing? The syntax looks correct, but why are you using a for loop? Since you don't use the variable $row in the if statement, you're testing the same value every time. Ken Quote Link to comment Share on other sites More sharing options...
rebelprincess Posted March 2, 2011 Author Share Posted March 2, 2011 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 ) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 2, 2011 Share Posted March 2, 2011 What does your form look like? Ken Quote Link to comment Share on other sites More sharing options...
rebelprincess Posted March 2, 2011 Author Share Posted March 2, 2011 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 2, 2011 Share Posted March 2, 2011 When posting code in this forum, please place the code between tags. I think part of your problem is that you don't realize that the PHP script to process the form doesn't see the values until after the submit button is pressed. There's no interaction with the user. If you want on-the-fly validation, you need to use Javascript. Also, your HTML is invalid. The opening <form> tag need to come after the <body> tag. Ken Quote Link to comment Share on other sites More sharing options...
rebelprincess Posted March 3, 2011 Author Share Posted March 3, 2011 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2011 Share Posted March 3, 2011 This should be what you're looking for. Be sure that you understand it. <?php $num_errors = 0; $tot_amount = 0; if (isset($_POST['submit'])) { $errors = array(); $num_errors = 0; $tot_amount = 0; for($i=0;$i<count($_POST['amount']);++$i) { if (!ctype_digit($_POST['amount'][$i]) && $_POST['amount'][$i] != '') { $errors[$i] = true; $num_errors++; } else { $tot_amount += $_POST['amount'][$i]; } } } ?> <html> <head> <title>Simple Form</title> <style> body, html { font-family: sans-serif; font-size: 100%; } label { font-weight: bold; } .error { color: red; } </style> </head> <body> <form method="post" action=""> <p> <?php $tmp = array(); for($i=0;$i<5;++$i) { $error_class = ($errors[$i])?' class="error"':''; $error_msg = ($errors[$i])?' <span class="error">Value not numeric</span>':''; $value = (isset($_POST['submit']))?$_POST['amount'][$i]:''; $tmp[] = '<label for="amount_' . $i . '">Amount ' . $i . ': </label><input' . $error_class . ' style="width:10%" type="text" name="amount[' . $i . ']" id="amount_' . $i . '" value="' . $value .'">' . $error_msg; } echo implode("<br>\n",$tmp) . "<br>\n"; ?> <input type="submit" name="submit" value="Send Amounts"> </p> </form> <?php if ($num_errors != 0) { echo "Number of errors found: $num_errors<br>\n"; } if ($tot_amount != 0) { echo "Total Amount entered: " . number_format($tot_amount); } ?> </body> </html> Ken Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 3, 2011 Share Posted March 3, 2011 Here you assign a value to $value_of_amount, then immediately overwrite it with another value. $value_of_amount = '$amount_cell'.$row; $value_of_amount = $_POST['value_of_amount']; One thing of note is that is_numeric() will return TRUE for values such as 12E456, and 0xEF32A, so if you want the field to contain nothing but digits, it would be better to use ctype_digit(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.