2

I have a select in my HTML form:

<form name="correo" id="correo" method="post" action="#" enctype="multipart/form-data" onSubmit="<!--return checkFields();-->" ><div class="multi-field-wrapper" name="multi-field-wrapper">
                    <div class="multi-fields" name="multi-fields">
                        <div class="multi-field" name="multi-field">
                            <div>
                                <label for="penviadas[]"> Cantidad </label>
                                <input type="number" name="penviadas[]" class="penviadas" id="penviadas" maxlength="70" placeholder="¿Cuántas?" onClick="removerIcon('iconcant');" > 
                            </div>
                         </div>
                    </div>
                    <button type="button" class="add-field">Añadir otra referencia</button>
                </div>

And I can add fields dynamically (or what's the same, I can repeat the code above many times; the code below works).

$('.multi-field-wrapper').each(function() {
  var $wrapper = $('.multi-fields', this);
  $(".add-field", $(this)).click(function(e) {
    $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
  });
  $('.multi-field .remove-field', $wrapper).click(function() {
    if ($('.multi-field', $wrapper).length > 1)
      $(this).parent('.multi-field').remove();
  });
});

Let's say I have 3 different selects (penviadas). What I want is to get all of them in my PHP file once I submit the form. It used to work, but now, for some reason, I can only get THE FIRST select (penviadas). Why am I not getting all the values from penviadas array?

PD: I print it in my PHP in different ways but they all return ONLY THE FIRST ELEMENT from penviadas, not the rest:

var_dump($_REQUEST['penviadas']);

2 Answers 2

1

I figured it out after many hours playing stupid. With the information in the OP it was impossible to discover where there was a problem. I found the solution here: Submitting form from different <div> HTML

Basically, I had this structure:

<div...
<form...
</div...
</form>

I thought it was alright and didn't think for a moment this could be affecting. Thus, it wasn't the PHP/JS but the html tags that were incorrect. Thanks for your time.

Sign up to request clarification or add additional context in comments.

Comments

0

PHP Code :

<?php
if(isset($_POST['test']))
{
$data = $_POST['referenciasnuevas'];
 foreach ($data as $key => $value) {
    echo $value . "<br />";
}
}
?>

Html Code:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

</head>
<body>
<form method="post" action="#">
<div class="multi-field-wrapper" name="multi-field-wrapper">
<div class="multi-fields" name="multi-fields">
<div class="multi-field" name="multi-field">
<label for="referenciasnuevas[]">Referencia pieza</label>
<select name="referenciasnuevas[]" id="referenciasnuevas" class="referenciasnuevas" style="width: 105px"  onClick="removerIcon('iconref');">
<option selected value='-1'> ¿cliente? </option></select>
</div>
</div>
<button type="button" class="add-field">Añadir otra referencia</button>
</div>
<input type="submit" name="test" value="Submit">
</form>
<script>
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
    $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
    if ($('.multi-field', $wrapper).length > 1)
        $(this).parent('.multi-field').remove();
});
});
</script>
</body>
</html>

The above code works for me.please look my code

2 Comments

I've tried your code and indeed, it works. However, when I embed it into my form, it doesn't. I will edit the first question with a more simple part of my code which has the same problem. Maybe you can find why your form works in my page but not if I put it into my own form? Thanks a lot
No, but I already posted the answer. It was a malformed div. Thanks

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.