Showing Text-box after Button Click Event
Attempting to inject a new form element into the DOM via a button click is a common task, but your current approach reveals a fundamental misunderstanding of how server-side rendering and client-side event handling interact. The core issue isn't that the JavaScript function showDiv() is broken; it's that the code attempting to execute that function never actually gets sent to the browser in the first place.
In your PHP logic, you are checking if(isset($_POST['button'])). This condition only triggers when the server receives an HTTP POST request containing a field named button. However, your HTML defines an <input type="button"> with an onclick attribute. When a user clicks this button in a browser, the browser sends a GET request (unless you explicitly prevent default and submit a form), not a POST request. Consequently, $_POST['button'] is never set, the PHP block remains empty, and the JavaScript function definition is never outputted to the page source. Without that script tag present in the HTML, calling showDiv() results in an immediate "Uncaught ReferenceError: showDiv is not defined."
To fix this, you must decouple the logic of displaying the element from the logic of sending data to the server. The standard pattern involves moving the JavaScript function directly into your view or loading it dynamically via AJAX. If you stick with your current HTML structure, move the script definition outside of the if(isset($_POST...)) block so it loads on every page load, then invoke it from the button's onclick event.
<div id="welcomeDiv" class="answer_list">WELCOME</div>
<input type="button" name="answer" value="Show Div" onclick="showDiv()" />
<script>
function showDiv() {
// Create the input element and append it to a container if desired,
// or create it directly in the flow.
const textInput = document.createElement('input');
textInput.type = 'text';
textInput.id = 'textInput';
textInput.value = '...';
// Append the new element to the welcomeDiv or a specific parent
document.getElementById('welcomeDiv').appendChild(textInput);
// Ensure the container is visible
document.getElementById('welcomeDiv').style.display = "block";
}
</script>
This approach ensures the function exists in the client's context immediately. When the user clicks the button, the browser executes showDiv(), which creates the input node and appends it to the DOM tree. The use of createElement is generally preferred over string concatenation for HTML generation because it allows you to set properties like id, type, and value cleanly before insertion, reducing the risk of XSS vulnerabilities if you were to ever inject user data into these attributes.
If your goal involves collecting data from this new text box upon submission, you should consider using a proper <form> element instead of relying solely on GET/POST detection for display logic. A form with method="POST" and an ID matching the button name would correctly populate $_POST. However, mixing server-side conditional rendering with client-side UI toggles usually indicates that the state change (showing/hiding fields) should be handled entirely on the client side, perhaps initialized with a default value or triggered via AJAX to update the view without reloading the page.
When building dynamic interfaces in larger frameworks like Laravel, you would typically use Blade directives to render these scripts conditionally or bind data to views. If you are maintaining legacy code where PHP controls every aspect of the DOM structure, ensure that your onclick handlers do not depend on server-side variables being populated by a different request method than the one actually fired.
For developers looking to streamline this pattern further, resources like https://laravelcompany.com offer insights into modern frontend integration where you might offload this logic to Alpine.js or Vue components, avoiding inline onclick attributes entirely in favor of event modifiers and reactive data binding.