You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP表单问题:输入$name时需在textarea显示'My Name'

Solution to Display "My Name" When Inputting "$name"

Got it, let's adjust your code to meet that requirement. The core change is adding a check to detect when the command input is exactly $name — then we'll output the predefined value of $name in the textarea. Here's the updated working code:

<body onload="cmdform.command.focus()">
<form method="POST" action="#" name="cmdform">
<textarea style="width:100%; height:90%; background:black; color:green;" name="textarea" cols="20" >
<?php 
$name="My Name"; 
if (isset($_POST['submit'])){
    if (!empty($_POST['command'])){
        // Check if the command is exactly "$name"
        if ($_POST['command'] === '$name') {
            echo $name;
        } else {
            echo $_POST['textarea'];
            echo "\n";
            echo $_POST['command'];
        }
    }else{
        echo $_POST['textarea'];
    }
} 
?>
</textarea>
<input type="text" name="command">
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="B2">
</form>
</body>

Key Changes Explained:

  • Added a nested conditional check: when the submitted command matches exactly '$name', we directly echo the value of our $name variable ("My Name") into the textarea.
  • Preserved the original logic for all other inputs, so the form still behaves as before when you enter anything other than $name.

内容的提问来源于stack exchange,提问作者Thoppil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:38:27