0

I'm doing the noobie PHP coding thing this semester. I have an assignment which has all html in a php file. I am to add if statements which will generate error messages if any field is empty and if a negative number is entered in the numeric fields.

This is really simple, and the answer is preferably simple. No arrays or anything like that. Just straight if/else and such. I'm also positive that it's something really obvious and simple I'm just not getting; I'm a clumsy coder outside SQL, at best. You can see my attempts at getting the error message to stick, which I've left in even though it doesn't work. Hopefully that leads to where my flawed thinking is headed.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Product Discount Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<body>
    <div id="content">
        <h1>Product Discount Calculator</h1>
        <form action="display_discount.php" method="post">
            <?php $error_message = " "; ?>

            <div id="data">
                <label>Product Description:</label>
                <input type="text" name="product_description"/><br />
                <?php if ( empty($product_description) ) {$error_message = 'This field cannot be blank'}; ?>
                <span class="error">* <?php echo $error_message;?></span>

                <label>List Price:</label>
                <input type="text" name="list_price"/><br />

                <label>Discount Percent:</label>
                <input type="text" name="discount_percent"/>%<br />
            </div>

            <div id="buttons">
                <label>&nbsp;</label>
                <input type="submit" value="Calculate Discount" /><br />
            </div>

        </form>
    </div>
</body>
</html>
1
  • unless you're running a stone-age or incredibly badly configured PHP install that has register_globals enabled, you'll NEVER get anything in $product_description, so your error message will always get set. Commented Sep 5, 2014 at 14:55

1 Answer 1

1

So there are a couple things that should be pointed out here.

HTML is client side. PHP is sever side. So generally you would submit the form to some PHP code, find out if the fields that were submitted would cause and error, and then set some variables to get the errors to come out.

When submitting forms, and wanting to know what the values are inside of them, you will need ID's on your input elements. This will allow you to talk to the $_POST array(I know you said no arrays, but that's the way submitting forms to PHP works).

Having said all of that, normally we would not allow them to enter invalid data in the inputs at all. Normally some JavaScript would be used to warn the user that their input is invalid, resulting in a better user experience.

As for this being a homework assignment, take a look at $_POST in php and decide if you can/want to use javascript to stop the errors before they happen. (there are TONS of articles/posts out there to help with both of the above)

Sign up to request clarification or add additional context in comments.

7 Comments

$_POST is actually included in this chapter! There's a second page with some of that which is fine; the results page. So I need to add some POST arrays here is what you're saying? Like set the initial $error_message = $_POST ('error_message') and then set the error message for each individual field in an if statement?
Your form action will send a $_POST array of information to display_discount.php that PHP file will have all the values of the form (after you add ID's) in the $_POST array. You can then test for empty input fields and display errors accordingly.
It's come to my attention that the ID's are not required, but if you ever need to access a specific fields with javascript having ID's on your input fields would be necessary. Not required in your situation, but I feel like it's good practice. Here's a good link. stackoverflow.com/questions/7470268/html-input-name-vs-id
If I'm following, I need to add a $_POST for each html field (eg $product_description = $_POST['product_description']. I then need to stick the $error_message in a $_POST for each field in an if statement. Correct?
No. When you click the submit button on your form, HTML will gather up the information on the inputs in your form and make a variable array. It will then pass that variable array to your php page. In display_discount.php, you will then have access to the info in a $_POST variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.