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
commandmatches exactly'$name', we directly echo the value of our$namevariable ("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
相关产品推荐
相关产品推荐

