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

PHP代码运行异常:提示'name'未定义 寻求技术协助

Fixing the "Undefined index: name/email" Error in Your PHP Form

Hey there! Let's break down why you're seeing those errors and how to fix them quickly.

What's Causing the Error?

The "Undefined index" notice pops up because when you load welcome_get.php directly (without submitting the form first), the $_GET superglobal doesn't contain the name and email keys. PHP throws this warning when you try to access an array index that doesn't exist yet. Even if you submit the form, empty input fields might still trigger this depending on your PHP configuration.

Solution 1: Check if Parameters Exist First

The safest approach is to verify that the name and email values exist in the $_GET array before trying to echo them. Use the isset() function for this check:

Here's the updated welcome_get.php code:

<html>
<body>
<?php
// Check if both name and email are present in the GET request
if (isset($_GET["name"]) && isset($_GET["email"])) {
    echo "Welcome " . $_GET["name"] . "<br>";
    echo "Your email address is: " . $_GET["email"];
} else {
    echo "Oops! Please fill out the form and submit it first.";
}
?>
</body>
</html>

To prevent users from submitting the form without filling required fields, add the required attribute to your input elements. This works directly in the browser and stops empty submissions:

Updated form code (save this as index.html):

<html>
<body>
<form action="welcome_get.php" method="get">
    Name: <input type="text" name="name" required><br>
    E-mail: <input type="text" name="email" required><br>
    <input type="submit" value="Submit">
</form>
</body>
</html>

Solution 3: Use PHP 7+'s Null Coalescing Operator (Cleaner Syntax)

If you're running PHP 7 or later, the null coalescing operator (??) lets you set default values for missing parameters in a concise way:

<html>
<body>
Welcome <?php echo $_GET["name"] ?? "Guest"; ?><br>
Your email address is: <?php echo $_GET["email"] ?? "Not provided"; ?>
</body>
</html>

Quick Notes

  • Always access the form page first (e.g., index.html) and submit it, don't load welcome_get.php directly.
  • Double-check that your input fields have the correct name attributes (yours are already set properly—nice work!).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:50:11