How to get input field value using PHP

How to get input field value using PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to get input field value using PHP

Retrieving a value from an HTML form and persisting it in a session is a fundamental operation in web development. However, the approach you take depends entirely on how your application architecture handles data flow. You are essentially asking two distinct questions: how do you extract the string "Car Loan" from the DOM, and how does that data traverse from the browser to the server?

When working with jQuery, the extraction happens in the client-side JavaScript environment before the data is ever sent to PHP. If your goal is to read the value directly from the input element using jQuery, you utilize the .val() method. This method returns the current content of the specified form field as a string.

Here is how you would capture that specific value in a script:

var subjectValue = $("#subject").val();
// At this point, subjectValue holds "Car Loan"

While jQuery provides an elegant syntax for DOM manipulation, it does not have a built-in mechanism to send data directly to a PHP session variable. Sessions are server-side constructs; they exist within the PHP runtime environment on the web server. Therefore, JavaScript can only prepare the data or trigger the event that causes the data to be transmitted. To actually store the value in $_SESSION['subject'], you must submit the form via an HTTP request, such as a POST or GET, and handle the retrieval within a PHP script.

Assuming the standard model where a user fills out a form and submits it, the HTML input element uses the name attribute to identify the data key during submission. In your example, the input has name="subject". When the form is submitted via POST, the browser constructs a request body containing subject=Car%20Loan. The server-side PHP script receives this and populates the $_POST superglobal array.

To assign this value to a session variable, you access the array key matching the input name and pass it to the session_start() function. It is critical to call session_start() at the very top of your PHP file, before any output is sent or before accessing $_POST.

<?php
session_start();

// Check if the value exists in POST data
if (isset($_POST['subject'])) {
    $inputValue = $_POST['subject'];
    
    // Assign to session
    $_SESSION['subject'] = $inputValue;
}
?>

Using isset() or checking for an empty string is a necessary safety step. If the form submission fails or if the input is left blank, accessing $_POST['subject'] directly without validation could result in undefined index notices or storing empty strings unintentionally. Additionally, while not shown here, sanitizing or escaping the input depending on how it will be used later (e.g., in a database query or HTML output) remains best practice to prevent injection attacks.

If you are building an application with Laravel, the framework abstracts much of this boilerplate code away. In that specific ecosystem, you would typically use request()->input('subject') to grab the value and then assign it to the session using session()->put(). For more details on handling forms and sessions within a modern Laravel context, you can refer to resources like https://laravelcompany.com.

Ultimately, jQuery is responsible for the client-side selection and reading of the value, while PHP handles the server-side storage. There is no magic method in jQuery that bypasses the HTTP request cycle to write directly to a PHP session. The data must travel through the standard form submission mechanism, where the name attribute on the input element serves as the bridge between the client's view and the server's session state.

Our network