3

Sorry for the bad English, I'm still learning...

Well i have in HTML a table with only

<tr id="linha_1">
    <td><input type="text" name="nome_cor[]" value="" /></td>
    <td><input type="text" name="cod_cor[]" value="" /></td>
    <td><input type="file" name="img_cor[]" /></td>
    <td><input type="button" value="Remover" id="remove" onclick="$.removeLinha(this);" /></td>
</tr>

I also have a JS script that adds new rows to this table, this script copy the first line, so the user can add more colors...

$(document).ready(function()
{
    var row_limit = 0;
    $('#add').click(function()
    {
        var linha_total = $('tbody#repetir tr').length;
        if (row_limit == 0 || row_limit > linha_total)
        {
            var linha = $('tbody#repetir tr').html();
            var linha_total = $('tbody#repetir tr').length;             
            var linha_id = $('tbody#repetir tr').attr('id');                
            $('tbody#repetir').append('<tr id="linha_' + (linha_total + 1) + '">' + linha + '</tr>');
        }
        else
        {
         alert("Desculpe, mas você só pode adicionar até " + limite_linhas + " linhas!");
         }                      
});

Using malsup form plugin to send the data via post:

$("#send").click(function(event) {
    event.preventDefault();
    $("#form-tapete").ajaxForm({        
        // validation and progress bar (*not important to the question i think*)
    },
    url: 'dados-tapete.php',
    resetForm: false
 }).submit();

I'm trying to read this in PHP with this code:

for ($i=0; $i < sizeof($cod_cor); $i++)
{
    $path_cor = "catalogo/tapete/cor/";
    $path_cor = $path_cor.basename($_FILES["img_cor"]["name"][$i]); 
    if (!move_uploaded_file($_FILES["img_cor"]["tmp_name"][$i], $path_cor)) 
        die("Ocorreu um erro ao enviar a imagem, tente novamente!"); 
    $cor_img_name = "catalogo/tapete/cor/".$_FILES["img_cor"]["name"][$i]; 
    $cadastra_cores = $con->sql_query("INSERT INTO tape_cores VALUES ('$nome_tapete','$cod_cor[$i]','$nome_cor[$i]','$path_cor')"); 
}

The PHP script can read and send the values to the database, but only the first value... The rows that was added dynamically don't appear in the $_POST array...

I searched for this and found some asked questions here, but no solution...

Sorry for the bad English again, if you don't understand me I can try to explain in other words... thanks

8
  • show to javascript code which is generating the input fields, the input field should generate with the same parameters if you need a output in array like <input type="text" name="nome_cor[]" value="" /> Commented Dec 2, 2013 at 18:49
  • i included the script that create the dynamic input wi jQuery append(); Commented Dec 2, 2013 at 18:59
  • So how do you actually POST the data? You should show this code. Commented Dec 2, 2013 at 19:02
  • i send the data to php script with malsup jquery form plugin, its easy to send input files without refreshing the page, code added... thanks for your attention (: Commented Dec 2, 2013 at 19:16
  • Are you sure that your dynamic fields are truly get appended? And what is the value of sizeof($cod_cor) here? Commented Dec 2, 2013 at 19:21

1 Answer 1

2

I would use JQuery with something like this :

$.post( "test.php", $( "#testform" ).serialize() );

For Malsup JQuery, use formSerialize();

var queryString = $('#myFormId').formSerialize();  
$.post('myscript.php', queryString);
Sign up to request clarification or add additional context in comments.

7 Comments

I'm sending the form with Malsup's jQuery form plugin, i have to change it?
I don't know Malsup, but regarding the doc seems possible and not so far away from the pure JQuery way : jquery.malsup.com/form/#api
i don't get problems to send the values via $_POST, the problem is im only sending the first value of the input arrays... thanks for the attention
If you add "echo $i;" in the for loop what result do you get ?
the "echo $i;" returns 0
|

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.