Tudor Ciotlos
Tudor Ciotlos

Reputation: 1845

Using echo inside a HTML input value

I have the following code, copied exactly from a exercise from a PHP book. I am having a problem with the value attribute which contains a php echo statement. According to the book, the first time the page is loaded the input boxes should be empty because the variables won't contain any data. Instead, I see something like this:

<br /><b>Notice</b>:  Undefined variable: investment in     <b>C:\xampp\htdocs\book_apps\ch02_future_value\index.php</b> on line <b>20</b><br />. 

Any suggestions?

<form action="display_results.php" method="post">

    <div id="data">
        <label>Investment Amount:</label>
        <input type="text" name="investment"
               value="<?php echo $investment; ?>"/><br />

        <label>Yearly Interest Rate:</label>
        <input type="text" name="interest_rate"
               value="<?php echo $interest_rate; ?>"/><br />

        <label>Number of Years:</label>
        <input type="text" name="years"
               value="<?php echo $years; ?>"/><br />
    </div>

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

</form>

Upvotes: 0

Views: 4911

Answers (1)

Milad Naseri
Milad Naseri

Reputation: 4118

This is because you are expecting the register_globals directive to be set, while it is not.

This means you have to get $_POST['investement'] instead of $investment and you need to first check if it's been submitted:

$investment = array_key_exists('investment', $_POST) ? $_POST['investment'] : "";

Upvotes: 1

Related Questions