0

The value entered in field "term" arrives to test.php but values in fields "AttrName" and "Attribute" does not, even without adding any more fields, addition of more fields works, but values are not sent.

Code:

 <head>
    <script src='https://code.jquery.com/jquery-1.12.4.min.js' integrity='sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ' crossorigin='anonymous'></script>
    <script src='https://www.jqueryscript.net/demo/dynamic-forms-fields/js/dynamic-form.js'></script>
    </head>
    <body>
    <form action='test.php' method='post'>
    <input type='text' name='term' size='60' placeholder='term' required /><br />
    <div id='dynamic_form'>
    <input type='text' name='AttrName' placeholder='Attribute name' />: <input type='text' name='Attribute' placeholder='Atributte' /> 
    <a href='javascript:void(0)' id='plus'> &nbsp; +</a> 
    <a href='javascript:void(0)' id='minus'> &nbsp; -</a>
    </div>
    <script>
    var dynamic_form =  $('#dynamic_form').dynamicForm('#dynamic_form','#plus', '#minus', {
    // the maximum number of form fields
        limit: 10,
    });
    </script>
    <br />
    <input type='submit' name='submit' id='submit' />
    </form>
    </body>

UNDATE

test.php:

<?php
echo $term=$_POST['term']."<br />";
echo $AttrName=$_POST['AttrName']."<br />";
echo $Attribute=$_POST['Attribute']."<br />";
?>
11
  • What does dynamicForm() even do here, since $('#dynamic_form') doesn't find anything? Can you demonstrate the state of the form when it's submitted, after it's been modified by this JavaScript? Or maybe provide a runnable example to demonstrate? Commented Oct 28, 2020 at 19:07
  • 1
    in PHP I would run print_r($_POST); to see what is getting posted. Commented Oct 28, 2020 at 19:12
  • 1
    The field names are generated like: dynamic_form[dynamic_form][0][AttrName] Commented Oct 28, 2020 at 19:27
  • 2
    Both answers below are correct, but you also need to add id attributes to your AttrName and Attribute inputs or dynamic-form.js will complain and only send one entry. Commented Oct 28, 2020 at 19:41
  • 1
    @RobRuchte, yes, you're right, I will try to find out how to do that! thanks to all of you! Commented Oct 28, 2020 at 19:45

2 Answers 2

2

Your form seems to be correctly submitting the values, however you are accessing the dynamic-form values incorrectly in PHP:

dynamic-form creates a nested array so you need to use the following reference:

echo $AttrName=$_POST['dynamic_form']['dynamic_form'][0]['AttrName']."<br />";
echo $Attribute=$_POST['dynamic_form']['dynamic_form'][0]['Attribute']."<br />";

As your form is dynamically generated and there can be any amount of fields, you need to loop on $_POST['dynamic_form']['dynamic_form'] e.g

forEach($_POST['dynamic_form']['dynamic_form'] as $form) {
  echo $form['AttrName'];
  echo $form['Attribute'];
}

From @Rob Ruchte's comment:

Also add id attributes to your html for dynamic-form to work properly:

<div id='dynamic_form'>
  <input type='text' id='attrName' name='AttrName' placeholder='Attribute name' />:
  <input type='text' id='attribute' name='Attribute' placeholder='Atributte' /> 
  <a href='javascript:void(0)' id='plus'> &nbsp; +</a> 
  <a href='javascript:void(0)' id='minus'> &nbsp; -</a>
</div>
Sign up to request clarification or add additional context in comments.

Comments

2

You can loop through the dynamic_form array like this:

$dynamic_form = $_POST["dynamic_form"]["dynamic_form"];

foreach($dynamic_form as $row){
  echo $row["AttrName"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.