Здравствуйте!
Сначала объясню суть вопроса. Есть сложная форма. Данные из этой формы я хочу отправить в базу данных. При этом я хочу использовать ajax.
На картинке покажу форму. У каждого игрока может быть несколько команд. У каждой команды может быть несколько игроков.
Я полагаю мне нужно привести данные в такой вид:
Object { owner_name_h2: "Руслан", team_name_h3: "Манчестер Ю", player_name: "Ибрагимович", goal_count: "4" }
Object { owner_name_h2: "Руслан", team_name_h3: "Манчестер Ю", player_name: "Руни", goal_count: "2" }
Object { owner_name_h2: "Клим", team_name_h3: "Ювентус", player_name: "Мората", goal_count: "1" }
итд
Не могу этого добиться. Подтолкните на верный путь.
Такой html
<form class="striker_form" id="striker_form">
<div class="player_row">
<h2><input class="owner_name_h2" name="owner_name_h2" value="" type="text" placeholder="Имя владельца"></h2>
<div class="owner_teams">
<div class="team">
<h3><input class="team_name_h3" name="team_name_h3" value="" type="text" placeholder="Название команды"></h3>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<button type="button" class="add_player">Добавить игрока</button>
</div>
<div class="team">
<h3><input class="team_name_h3" name="team_name_h3" value="" type="text" placeholder="Название команды"></h3>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<button type="button" class="add_player">Добавить игрока</button>
</div>
</div>
</div>
<div class="player_row">
<h2><input class="owner_name_h2" name="owner_name_h2" value="" type="text" placeholder="Имя владельца"></h2>
<div class="owner_teams">
<div class="team">
<h3><input class="team_name_h3" name="team_name_h3" value="" type="text" placeholder="Название команды"></h3>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<button type="button" class="add_player">Добавить игрока</button>
</div>
<div class="team">
<h3><input class="team_name_h3" name="team_name_h3" value="" type="text" placeholder="Название команды"></h3>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<div class="player">
<input class="player_name" name="player_name" value="" type="text">
<input class="goal_count" name="goal_count" value="" type="text">
</div>
<button type="button" class="add_player">Добавить игрока</button>
</div>
</div>
</div>
<button id="sendStriker" type="button" name="striker_update_submit"/>Сохранить информацию о Бомбардирах</button>
</form>
Пробую делать так:
jQuery("#sendStriker").click(function() {
var strikerform = jQuery("#striker_form").serializeObject();
var st = jQuery("#headerpage").html();
var player_array = [];
jQuery('.owner_name_h2').each(function() {
console.log(strikerform);
player_array.push(jQuery(this).val());
var title = jQuery("#headerpage");
jQuery('.player_name').each(function() {
player_array.push(jQuery(this).val());
var put = jQuery("#striker_form").serializeObject();
jQuery('.player').each(function() {
jQuery.ajax({
url: myajax.ajax_url,
type: 'POST',
dataType: 'JSON',
data: {
action: 'add_striker',
// data: put,
datak: st,
},
success: function( response ) {
title.after('Добавлено');
},
error: function(error) {
// title.after(' не добавлено');
}
});
});
});
});
});
C ajax'ом пока проблем нет.