Jump to content

$input->post empty with dynamically added input fields


helmut2509
 Share

Recommended Posts

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

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.

form.gif.0aecb0163f3f5f7c2cdda7b773fb5cbc.gif

Link to comment
Share on other sites

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.

form.gif.0aecb0163f3f5f7c2cdda7b773fb5cbc.gif

 

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...