Why Hidden Inputs Don't Always Capture Their Intended Values
Passing data from JavaScript to PHP via a hidden form field is a common pattern, but it often fails silently if the execution order isn't managed correctly. In the scenario you presented, the JavaScript function func_load3 successfully updates the DOM element with id hidden1, yet $salarieid remains empty in the PHP script. This disconnect usually stems from one of two fundamental issues: the timing of the form submission or a syntax error in the PHP code that prevents the variable assignment from completing before execution halts.
First, consider the lifecycle of the page. When a browser loads an HTML document containing both inline scripts and PHP tags, the rendering process is strict. The browser parses the HTML structure first. If it encounters a <form> element, it initializes the form object. However, JavaScript functions defined in <script> tags are not executed automatically upon parsing; they must be invoked explicitly.
In your code snippet, func_load3 is defined but never called. Unless there is a button click or another event listener triggering this function after the page loads, the line document.getElementById("hidden1").value = sa; never runs. Consequently, the hidden input retains its default empty state. Even if you add an alert to verify the value exists in JavaScript, the form submission will still arrive at the server with an empty $_POST['hidden1'].
To resolve this, you must ensure the function executes before the form submits. If you are using a button or a link to trigger the action, attach the click event to func_load3 and then submit the form programmatically (e.g., via form.submit()). Alternatively, if the logic should run immediately upon page load, wrap the call in window.onload or move the script tag to the bottom of the body just before the closing </body> tag. This guarantees the DOM elements referenced by document.getElementById exist and that the function has a chance to execute.
There is also a critical syntax error in your PHP logic that needs addressing. Look closely at this line:
action="<?php echo $_SERVER['$PHP_SELF']; ?>"
The variable name inside the single quotes is incorrect. You are wrapping $PHP_SELF in quotes, which treats it as a literal string rather than a server variable. The correct syntax is:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
Removing the dollar sign and the surrounding quotes ensures $_SERVER['PHP_SELF'] resolves to the actual current script path. Without this fix, the form action might point to an incorrect or broken URL.
Furthermore, the security implications of your query construction are significant. You are concatenating user-derived data directly into a SQL string:
$query = "select * from salarie where salarieid = ".$salarieid;
If $salarieid contains unexpected characters or if the input is manipulated, this approach opens your application to SQL injection attacks. Modern development standards mandate the use of prepared statements. In legacy environments using mysql_* functions (which are deprecated), you should at least sanitize inputs with mysql_real_escape_string, though migrating to PDO or MySQLi is strongly recommended for long-term stability.
If you are working within a modern PHP ecosystem, frameworks like Laravel abstract away these manual DOM manipulations and SQL queries entirely. They provide robust request handling where hidden fields are automatically bound to model properties, and SQL injection is prevented by default using Eloquent's query builder. For more information on how these frameworks handle form requests and data binding securely, you can explore resources from https://laravelcompany.com.
Ultimately, the failure to pass the variable is not a mystery of PHP but a matter of execution timing and syntax precision. Ensure your JavaScript runs before submission, fix the $_SERVER variable reference, and adopt prepared statements to secure your database interactions.
