helmut2509 Posted September 11, 2017 Share Posted September 11, 2017 Hi, I just noticed that the $input->post variable is empty when there are dynamically added input fields (by javascript) in the form. However when I make a print_r($_POST), all form fields and values do appear, inluding those which have been added dynamically. Is this a pw bug or am I making a mistake? Link to comment Share on other sites More sharing options...
abdus Posted September 11, 2017 Share Posted September 11, 2017 I don't have that behavior. Can you post your code? If your inputs do not have name attribute, they wont be sent with POST requests This is my test setup: <?php namespace ProcessWire; ?> <style> form { max-width: 500px; margin: 5rem auto; } input { display: block; width: 100%; } </style> <form method="POST"> <div class="inputs"> <input name="myField[]" type="text"> </div> <hr> <button id="addMore" type="button">Add More Inputs</button> <hr> <button type="Submit" name="submit" value="submit">Submit Form</button> <hr> <?php if ($input->post->submit): // DUMP POST DATA ?> <pre><?= wireEncodeJSON($input->post->getArray(), true, true) ?></pre> <?php endif; ?> </form> <script> let addMore = document.getElementById('addMore'); let inputs = document.querySelector('.inputs'); addMore.addEventListener('click', function () { let newInput = document.createElement('input'); newInput.placeholder = Math.round(Math.random() * 1000); newInput.name = 'myField[]'; inputs.appendChild(newInput); }); </script> I add new text inputs with JS. When I post, I can get the values with no problem. Link to comment Share on other sites More sharing options...
helmut2509 Posted September 11, 2017 Author Share Posted September 11, 2017 28 minutes ago, abdus said: I don't have that behavior. Can you post your code? Hi Abdus, I found out the cause of the error. In order to check if there has been a submit, I used "if ($input->post->submit).." and "if($input->post)..". The first one didn't work because I used a "button type=submit" instead of "input type=submit". I cannot guess the failure of "$input->post" because with a foreach loop, I get names and values. Another strange thing : when you check for " $input->post->anyfield" and this field is empty then you get a "false" returncode.... So you can only use a field of which you are sure that it is not empty... thanx for your help, though! 28 minutes ago, abdus said: If your inputs do not have name attribute, they wont be sent with POST requests This is my test setup: <?php namespace ProcessWire; ?> <style> form { max-width: 500px; margin: 5rem auto; } input { display: block; width: 100%; } </style> <form method="POST"> <div class="inputs"> <input name="myField[]" type="text"> </div> <hr> <button id="addMore" type="button">Add More Inputs</button> <hr> <button type="Submit" name="submit" value="submit">Submit Form</button> <hr> <?php if ($input->post->submit): // DUMP POST DATA ?> <pre><?= wireEncodeJSON($input->post->getArray(), true, true) ?></pre> <?php endif; ?> </form> <script> let addMore = document.getElementById('addMore'); let inputs = document.querySelector('.inputs'); addMore.addEventListener('click', function () { let newInput = document.createElement('input'); newInput.placeholder = Math.round(Math.random() * 1000); newInput.name = 'myField[]'; inputs.appendChild(newInput); }); </script> I add new text inputs with JS. When I post, I can get the values with no problem. Link to comment Share on other sites More sharing options...
abdus Posted September 11, 2017 Share Posted September 11, 2017 1 minute ago, helmut2509 said: The first one didn't work because I used a "button type=submit" instead of "input type=submit". Both should work as long as they have a name=submit and a non-empty value <input type="submit" value="submit"> <button type="submit" value="1">Submit</button> 12 minutes ago, helmut2509 said: when you check for " $input->post->anyfield" and this field is empty then you get a "false" returncode When accessing a field with $input->post->myField, you get empty string if that field is not filled, or its value attribute does not exist. It should not return you false. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now