在HTML输入值中使用echo

时间:2022-11-23 17:33:00

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:

我有以下代码,完全从PHP书籍的练习中复制。我有一个包含php echo语句的value属性的问题。根据该书,第一次加载页面时,输入框应为空,因为变量不包含任何数据。相反,我看到这样的事情:

<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>

1 个解决方案

#1


1  

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

这是因为您希望设置register_globals指令,而不是。

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

这意味着你必须得到$ _POST ['investement']而不是$ investment,你需要先检查它是否已经提交:

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

#1


1  

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

这是因为您希望设置register_globals指令,而不是。

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

这意味着你必须得到$ _POST ['investement']而不是$ investment,你需要先检查它是否已经提交:

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