PHP: get the value of TEXTBOX then pass it to a VARIABLE

PHP: get the value of TEXTBOX then pass it to a VARIABLE

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Mismatch Between Form Submission and Data Retrieval

The error Notice: Undefined index: name is a classic symptom of a fundamental misunderstanding in PHP regarding how data flows through a web request. In your specific scenario, you have defined an HTML form that submits data using the method='post' attribute. However, your PHP script attempts to retrieve this value using the $_GET superglobal array. This mismatch is why the variable remains undefined; $_GET only contains data sent via URL query strings, which are irrelevant when a form performs a POST operation.

To resolve this, you must align your retrieval method with the submission method. Since your form tag explicitly sets method="post", the browser packages the input fields into the request body and sends them to the server. On the receiving end, PHP populates the $_POST superglobal array with these values. You need to change your retrieval line from $name = $_GET['name']; to $name = $_POST['name'];.

Consider the following corrected implementation for your testing2.php file:

<?php
// Retrieve the value from the POST request
if (isset($_POST['name'])) {
    $name = $_POST['name'];
    
    // Sanitize and validate before use
    $name = htmlspecialchars($name); 
    
    echo "Received Name: " . $name;
} else {
    echo "No name value was sent via POST.";
}
?>

The isset() check is critical here. While it might seem redundant if you are sure the form submitted, real-world applications often encounter scenarios where a URL is accessed directly (bypassing the form) or where JavaScript pre-fills fields that fail to submit properly. Without this guard clause, accessing $_POST['name'] directly would trigger the undefined index notice you are currently seeing, even if the value exists in the array.

Beyond simply fixing the syntax error, there is a deeper architectural consideration regarding how you handle data transfer between pages. Your current approach relies on server-side variables echoing text directly back to the browser. This works for simple demonstrations but lacks the structure required for scalable applications. If you intend to persist this value or display it in a complex layout, passing raw strings via GET parameters (e.g., testing2.php?name=John) is generally discouraged because it exposes sensitive data in URLs and limits input length.

If you are building a larger application, consider leveraging a framework like Laravel. Instead of manually handling the retrieval and echoing logic in separate PHP files, you can define a route that accepts POST requests and renders a view with the data pre-bound. This approach separates concerns: one file handles the form submission logic, and another handles the display logic using Blade templates.

For instance, a Laravel controller action would look like this:

public function store(Request $request) {
    // Validate that 'name' was actually submitted
    $request->validate([
        'name' => 'required|string|max:255'
    ]);

    return view('testing2', ['name' => $request->input('name')]);
}

In this pattern, the testing2 view receives the $name variable via a variable interpolation (e.g., {{ $name }}) rather than echoing it directly in PHP. This separation of logic and presentation reduces the risk of accidentally exposing debug information or writing insecure code. It also allows you to easily redirect users after submission, such as sending them back to a dashboard with a success message, which is a common requirement in modern web development.

Finally, always remember that $_POST data is never trusted until it has been validated and sanitized. The htmlspecialchars() function included in the example above prevents Cross-Site Scripting (XSS) attacks by escaping special characters like < and >. This ensures that if a user inputs malicious HTML into the textbox, it will be rendered as plain text rather than executing code in your browser.

Our network