0

I want to add input whit js and send value whit jQuery but value dosnt send.

Actually fields value dosn't define in jQuery.

function addElement(myDiv, type) {
    var ni = document.getElementById(myDiv);
    var numi = document.getElementById('theValue');
    var num = (document.getElementById('theValue').value - 1) + 2;
    numi.value = num;
    var newdiv = document.createElement('div');
    var divIdName = 'my' + num + 'Div';
    newdiv.setAttribute('id', divIdName);

    newdiv.innerHTML = '<input class="name" name="name[]" value="" placeholder="عنوان"><input class="value" name="value[]" value="" placeholder="توضیحات"><input type="hidden" class="type" name="type[]" value="' + type + '"><input type="hidden" class="position" name="position[]" value="' + myDiv + '">'
    ni.appendChild(newdiv);
}

$('#add_field').live('click', function() {    
    var id = $('#id').val();
    var name = $('#name').val();
    var value = $('#value').val();
    var type = $('#type').val();
    var position = $('#position').val();

    var name = [];
    var value = [];
    var type = [];
    var position = [];

    $(".name").each(function() {
        name.push($(this).val());
    });

    $(".value").each(function() {
        type.push($(this).val());
    });

    $(".position").each(function() {
        size.push($(this).val());
    });

    $.ajax({
        type: 'POST',
        url: '../inc/add.field.php?id=' + id,
        data: {
            name: name,
            type: value,
            size: type,
            position: position
        },
        success: function(data) {
            $('#result').html(data);
        }
    });
});

PHP

print_r($_POST['name']);
print_r($_POST['value']);
print_r($_POST['type']);
print_r($_POST['position']);
echo $_GET['id'];

Notice: Undefined index: name

Notice: Undefined index: value

Notice: Undefined index: type

Notice: Undefined index: position

  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Milap Feb 24 '16 at 10:58
  • Thanks but it dosnt help me – creavaehahea Feb 24 '16 at 11:00
  • Also check if variable is set using `isset` before `$_POST['name'])` value. – Milap Feb 24 '16 at 11:01
  • I think, it should be `var id = $('.id').val();var name = $('.name').val(); var value = $('.value').val();var type = $('.type').val();var position = $('.position').val();` Because, No `id` attribute in input. – Nana Partykar Feb 24 '16 at 11:01
  • You are sending javascript arrays , not single values. That's why PHP doesn't recognize them. Maybe you need to serialize the form – Marcos Pérez Gude Feb 24 '16 at 11:02
  • @nana if i add field whit js , jQuery never dosnt sent and dont show any error but before add input just show thats errors . – creavaehahea Feb 24 '16 at 11:05
  • @creavaehahea did you read my comment? Your problem is that you are trying to pass a javascript array object instead of an array of key=>values. PHP doesn't recognize a javascript array object as a valid value – Marcos Pérez Gude Feb 24 '16 at 11:14
  • @creavaehahea put something from your part. It's hard to help someone that it's expecting to make his job without using the brain to achieve his own solution. – Marcos Pérez Gude Feb 24 '16 at 11:18
  • @Marcos Yes i read you comment and thank you very much but i cant solve my problem yet. – creavaehahea Feb 24 '16 at 11:27
  • I share a fiddle that's working. If you want to change your code you can solve it. If not, sorry, I can't lose more time in this topic (at least while you don't explain why or what you can't edit your own code) – Marcos Pérez Gude Feb 24 '16 at 11:32
  • This man doesn't explain the problem he has. I fell like I'm lost my time here. Sorry but I can't help you more since my answer works. Good luck – Marcos Pérez Gude Feb 24 '16 at 11:38

1 Answers1

0

Use this ajax. Note that the $('form') selector needs to be a valid selector that targets your <form> tag. If you serialize, the arrays will be passed as arrays in HTTP, not as a javascript array value. With this mode, PHP will recognize them as arrays, not as [OBJECT object] value

$.ajax({
    type: 'POST',
    url: '../inc/add.field.php?id=' + id,
    data: $('form').serialize(); // that's the change
    success: function(data) {
        $('#result').html(data);
    }
});

EDIT

Test with serializeArray():

$.ajax({
    type: 'POST',
    url: '../inc/add.field.php?id=' + id,
    data: $('form').serializeArray(); // that's the change
    success: function(data) {
        $('#result').html(data);
    }
});

More info: https://api.jquery.com/serializeArray/

EDIT 2

As I said in comments, you can make it with serialize():

http://jsfiddle.net/tZPg4/15519/

It works perfectly.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • Please, see the edition. Your problem is that I said to you. you are trying to pass javascript arrays objects, and php doesn't understand them – Marcos Pérez Gude Feb 24 '16 at 11:13
  • Thanks . But i dont use form tag and it dosnt work anyway. – creavaehahea Feb 24 '16 at 11:16
  • 1
    Why don't you use form tag? I think I'm lost my time with you. You are not interesting putting something from your part. Maybe you need to change your code to make it working, or not? This should work because this is your problem. Don't say `This doesn't work` and not explain why don't you use a correct html markup – Marcos Pérez Gude Feb 24 '16 at 11:17
  • Dear @Marcos i used this script for add and remove fields http://jsfiddle.net/jaredwilli/tzpg4/4/ But in my Theme need to use jQurey lib and have Disorder responsive theme and i dont use form tag because i wrote in that method and if i want to use form i want to have big change on my source – creavaehahea Feb 24 '16 at 11:24
  • @creavaehahea change the parent div with the form and it still works, you don't have chances with this. http://jsfiddle.net/tZPg4/15519/ And with a form tag, you can serialize all the content (no matter how many inputs there are). – Marcos Pérez Gude Feb 24 '16 at 11:27
  • Dear @marcos i never said jsfiddle.net/tZPg4/15519 dosnt work. I said it work but it have Disorder whit my theme and i cant use because it have Disturbance whit my theme . – creavaehahea Feb 24 '16 at 11:33
  • Ok, so you must wait for another solution, but your problem is that I explain here: you are passing wrong objects to php. If you can't add a single `
    ` tag, I don't know how you will achieve another solutions for your problems.
    – Marcos Pérez Gude Feb 24 '16 at 11:34
  • Changing a `
    ` to `
    ` must not disturb your theme, at least if you don't target `div` with css/js, the good practice will be target `#someid` and doesn't matter if the tag is a `

    ` or even a ``, while this tags are block level elements.

    – Marcos Pérez Gude Feb 24 '16 at 11:36
  • I have exactly this problem http://stackoverflow.com/questions/35575742/need-to-use-jquery-1-min-js-and-jquery-min-js-together . exactly. – creavaehahea Feb 24 '16 at 11:36
  • Are you kidding us? Your question doesn't say nothing about this topic. In the URL you share, you have a solution. Good luck. – Marcos Pérez Gude Feb 24 '16 at 11:38
  • Heh never . But thank you very much anyway for give your time . – creavaehahea Feb 24 '16 at 11:40