Form submission in Blade appends parameters instead of showing JSON, though it works in JSFiddle.
HTML:
<section>
<form id="jsonForm">
<input type="text" name="userName" placeholder="Your Name" />
<input type="submit" value="Submit" />
</form>
<div id="displayArea"></div>
</section>
JavaScript:
document.getElementById('jsonForm').addEventListener('submit', function(evt){
evt.preventDefault();
const inputs = new FormData(this);
const dataObj = {};
inputs.forEach((val, key) => dataObj[key] = val);
document.getElementById('displayArea').textContent = JSON.stringify(dataObj, null, 2);
});
The issue might be related to how Blade processes your JavaScript and the HTML in your view. I encountered a similar problem where the form action wasn’t correctly intercepted because of Blade’s built-in syntax handling. In my case, ensuring that the JavaScript code was placed after the DOM elements were loaded resolved the issue. Moreover, I double-checked that there were no conflicting characters, such as the curly braces which Blade might interpret as directives. Moving the script block to the end of the body or wrapping it inside a DOM ready event helped me bypass this conflict.
hey, i had a similar odditiy in my blade setup. try moving your script block right before the closing tag so the dom is completely loaded. it fixed mine. also, check for any stray spaces in your form element names.