html - php empty data record deletion
Get the solution ↓↓↓There is no problem when the form is sent normally, but when I click on the add-phone button with jquery, input I have an inpute with name sub_phone_ and when I post it, it is saved in the content named sub_email, sub_map and sub_maps which I did not add to the database in php. I want that if there are no email map maps inputs, they should not write them in the database.
database record
[{"title":"1","address":"1","phone":"1","submenu":[{"email":"1","phone":null,"map":null,"maps":null}]}]
if empty
"phone":null,"map":null,"maps":null <<< delete.
HTML
<form action="" method="post" class="formify">
<div class="tab-pane" id="iletisim">
<div id="menus">
<ul id="menu" class="menu">
<li>
<div class="menu-item">
<a href="#" class="delete-menu">
<i class="fa fa-times"></i>
</a>
<div class="row">
<label class="col-sm-2 col-form-label">title</label>
<div class="col-sm-7">
<div class="form-group bmd-form-group">
<input class="form-control" type="text" name="title[]" placeholder="title">
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">address</label>
<div class="col-sm-7">
<div class="form-group bmd-form-group">
<textarea class="form-control" name="address[]" cols="30" rows="3" placeholder="address""></textarea>
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">Phone</label>
<div class="col-sm-7">
<div class="form-group bmd-form-group">
<input class="form-control" type="text" name="phone[]" placeholder="Phone">
</div>
</div>
</div>
</div>
<div class="sub-menu">
<ul class="menu"></ul>
</div>
<a href="#" class="btn add-email" data-background-color="warning">Add Email</a>
<a href="#" class="btn add-phone" data-background-color="warning">Add Phone</a>
<a href="#" class="btn add-map" data-background-color="warning">Add Map</a>
</li>
</ul>
</div>
</div>
<div class="menu-btn">
<button type="submit" value="1" name="submit" >Save</button>
</div>
</form>
JQUERY
$(document.body).on('click', '.add-email', function (e) {
var index = $(this).closest('li').index();
$(this).prevAll('.sub-menu:last').find('ul').append('<li>\n' +
'<div class="handle"></div><div class="menu-item">\n' +
' <a href="#" class="delete-menu">\n' +
' <i class="fa fa-times"></i>\n' +
' </a>\n' +
' <div class="row">\n' +
' <label class="col-sm-2 col-form-label">İletişim Maili</label>\n' +
' <div class="col-sm-7">\n' +
' <div class="form-group bmd-form-group">\n' +
' <input class="form-control" type="text" name="sub_email_' + index + '[]" '
+ 'placeholder="İletişim Maili">\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
'</li>');
e.preventDefault();
});
$(document.body).on('click', '.add-phone', function (e) {
var index = $(this).closest('li').index();
$(this).prevAll('.sub-menu:last').find('ul').append('<li>\n' +
'<div class="handle"></div><div class="menu-item">\n' +
'<a href="#" class="delete-menu">\n' +
' <i class="fa fa-times"></i>\n' +
'</a>\n' +
' <div class="row">\n' +
' <label class="col-sm-2 col-form-label">İletişim 2. Telefon</label>\n' +
' <div class="col-sm-7">\n' +
' <div class="form-group bmd-form-group">\n' +
' <input class="form-control" type="text" name="sub_phone_' + index + '[]" '
+ 'placeholder="İletişim 2. Telefon">\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
'</li>');
e.preventDefault();
});
$(document.body).on('click', '.add-map', function (e) {
var index = $(this).closest('li').index();
$(this).prevAll('.sub-menu:last').find('ul').append('<li>\n' +
'<div class="handle"></div><div class="menu-item">\n' +
' <a href="#" class="delete-menu">\n' +
' <i class="fa fa-times"></i>\n' +
' </a>\n' +
' <div class="row">\n' +
' <label class="col-sm-2 col-form-label">Yol Tarifi URL Adresi</label>\n' +
' <div class="col-sm-7">\n' +
' <div class="form-group bmd-form-group">\n' +
' <input class="form-control" type="text" name="sub_map_' + index + '[]" '
+ 'placeholder="Yol Tarifi URL Adresi">\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
'</li>');
e.preventDefault();
});
$(document.body).on('click', '.add-maps', function (e) {
var index = $(this).closest('li').index();
$(this).prevAll('.sub-menu:last').find('ul').append('<li>\n' +
'<div class="handle"></div><div class="menu-item">\n' +
' <a href="#" class="delete-menu">\n' +
' <i class="fa fa-times"></i>\n' +
' </a>\n' +
' <div class="row">\n' +
' <label class="col-sm-2 col-form-label">Map Görseli URL Adresi</label>\n' +
' <div class="col-sm-7">\n' +
' <div class="form-group bmd-form-group">\n' +
' <input class="form-control" type="text" name="sub_maps_' + index + '[]" '
+ 'placeholder="Map Görseli URL Adresi">\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
'</div>\n' +
'</li>');
e.preventDefault();
});
PHP
if (post('submit')) {
$menu = [];
$menu_title = post('menu_title');
if (!$menu_title) {
$error = 'Menü Başlığını Belirtin!';
} elseif (count(array_filter(post('title'))) == 0) {
$error = 'En Az Bir Menü İçeriği Girmeniz Gerekiyor!';
} else {
$address = post('address');
$phone = post('phone');
foreach (post('title') as $key => $title) {
$arr = [
'title' => $title,
'address' => $address[$key],
'phone' => $phone[$key],
];
if (post('sub_email_' . $key)) {
$submenu = [];
$subphone = post('sub_phone_' . $key);
$submap = post('sub_map_'.$key);
$submaps = post('sub_maps_'.$key);
foreach (post('sub_email_' . $key) as $k => $subemail) {
$submenu[] = [
'email' => $subemail,
'phone' => $subphone[$k],
'map' => $submap[$k],
'maps' => $submaps[$k]
];
}
$arr['submenu'] = $submenu;
}
$menu[] = $arr;
}
$query = $db->prepare('INSERT INTO menu SET menu_title = :menu_title, menu_content = :menu_content');
$result = $query->execute([
'menu_title' => $menu_title,
'menu_content' => json_encode($menu)
]);
Answer
Solution:
You can Try following code.
foreach (post('title') as $key => $title) {
$arr = [
'title' => $title,
'address' => $address[$key],
'phone' => $phone[$key],
];
if (post('sub_email_' . $key) != null) {
if (post('sub_email_' . $key)) {
$submenu = [];
$subphone = post('sub_phone_' . $key);
$submap = post('sub_map_' . $key);
$submaps = post('sub_maps_' . $key);
foreach (post('sub_email_' . $key) as $k => $subemail) {
if (($subphone[$k] != null) && ($submap[$k] != null) && ($submaps[$k] != null)) {
$submenu[] = [
'email' => $subemail,
'phone' => $subphone[$k],
'map' => $submap[$k],
'maps' => $submaps[$k]
];
}
}
if (count($submenu) > 0) {
$arr['submenu'] = $submenu;
}
}
}
$menu[] = $arr;
}
$result=DB::table('menu')
->insert([
'menu_title' =>$menu_title,//your menu title here
'menu_content' => json_encode($menu)
]);
I hope it's works for you.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.