php empty data record deletion - php

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>
Add Email
Add Phone
Add Map
</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)
]);

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.

Related

How to validate multiple files in array Laravel?

I have use this form for uploading multiple files. But i have used jQuery for append multiple file input. I have try multiple method for validate in laravel but its not working. If we used only single file validate its working. But if we used multi file validation its not working.
If we use this and upload all file at a time in with multiple attribute file input its working.
If we upload multiple file one by one after that its not working.
// My View
#include('flash.flash_message')
<form action="{{route('company.upload')}}" method="post" id="formUpload">
<div id="newinput">
<input type="file" name="files[]" class="form-control" />
</div>
<input type="submit" value="uplaod" />
</form>
<div class="btn btn-info" id="add_btn">Add</div>
// My jQuery
$(document).on('click','#add_btn',function(){
var newRowAdd =
'<input type="file" name="files[]" class="form-control" />';
$('#newinput').append(newRowAdd);
});
// My Controller
<?php
public function store(Request $request)
{
$request->validate([
'files' => 'required',
'files.*' => 'mimes:doc,docx|max:15720',
] );
try
{
$array = [
'created_at'=>date('Y-m-d H:i:s')
];
$data = Company::create($array);
// there my uplaoding code
if($request->hasFile('file'))
{
$newData = Company::findOrFail($data->id);
$docs = array();
$files = $request->file('file');
if($files){
foreach($files as $file) {
$fileName = time().rand(100000,999999).'_'.$data->id.'.'.$file->extension();
$docs[] = $fileName;
$file->move(public_path('company'), $fileName);
}
}
$newData->doc = !empty($result) ? serialize($result) :'';
$newData->updated_at = date('Y-m-d H:i:s');
$newData->save();
}
}
catch(\Exception $e)
{
return redirect()->back()->with('error',$e->getMessage());
}
}
// I Also used this
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'files.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'files.*.required' => 'Please upload an image',
'files.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'files.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);
if ($validator->fails()) {
// Validation error..
}
var i = 1;
jQuery("#addRow").click(function () {
var html = '';
var scr = '';
html += '<div id="addedRow" class="form-row mb-4">';
html += '<div class="form-group col-md-4">';
html += '<label for="filename[' + i + ']">File type</label>';
html += '<span class="text-danger">*</span>';
html += '<select class="form-control" id="filename' + i + '" name="filename[' + i + ']" required><option selected="selected" value="">Select file type</option><option value="claim">Claim</option><option value="reimbursements">Reimbursements</option><option value="policy">Policy</option><option value="ecards">Ecards</option><option value="data">Data</option><option value="others">Others</option></select>';
html += '<span class="badge">';
html += '<small id="sh-text4" class="form-text mt-0">Select the file type you are uploading/linking</small>';
html += '</span>';
html += '</div>';
html += '<div id="Other' + i + '" class="form-group col-md-4">';
html += '<label for="other[' + i + ']">Specify file type</label> <span class="text-danger">*</span>';
html += '<input class="form-control" placeholder="Specify the file type" name="other[' + i + ']" type="text" id="other[' + i + ']" >';
html += '<span class="badge">';
html += '<small id="sh-text4" class="form-text mt-0">Specify the file type as you selected the file as "Other"</small>';
html += '</span>';
html += '</div>';
html += '<div id="upload' + i + '" class="form-group col-md-4">';
html += '<label for="upload[' + i + ']">Upload file</label>';
html += '<span class="text-danger">*</span>';
html += '<input class="form-control" name="upload[' + i + ']" type="file" id="upload[' + i + ']" required>';
html += '<span class="badge">';
html += '<small id="sh-text4" class="form-text mt-0">Upload the file | Formats allowed: <strong>PDF, DOC, ZIP, JPG, PNG GIF and Excel</strong></small></span>';
html += '</div>';
html += '<div class="form-group col-md-4" style="padding-top: 30px;">';
html += '<button id="removeRow" type="button" class="btn btn-danger">Remove</button>';
html += '</div>';
html += '</div>';
jQuery('#newRow').append(html);
scr += '<script';
scr += '>$(document).ready(function(){ $("#filename' + i + '").change(function () { if ($(this).val() === "others") {$("#Other' + i + '").show(); document.getElementById("other[' + i + ']").required=true}else{ $("#Other' + i + '").hide(); document.getElementById("other[' + i + ']").required=false;}}); $("#Other' + i + '").hide(); }); </';
scr += 'script>';
jQuery('#newScript').append(scr);
i++;
});
// remove row
jQuery(document).on('click', '#removeRow', function () {
i--;
$(this).closest('#addedRow').remove();
});
This is how I have implemented this in my project you can see there is a variable that you need to pass here
<input type="file" name="files['+i+']" class="form-control" />;
You have to increase the value of i once the row is added

Unable to use multiple ORDER BY in php using AJAX

Am trying to use order by date and price low to high in this statement
SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle' ORDER BY pdt DESC, prs ASC
i used echo $query; to get the query and here is how i did,
looking for ORDER BY pdt DESC, is default query i want prs to be user option .
when i select order by low to high or high to low the statement changes but query does nothing, its not sorting by price
How do i sort by prs any solution?
pdt means Date and prs means Price
HTML
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="sortby">
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="asc" value="ASC">
<label for="asc">Price : Low to High</label>
</div>
</li>
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="desc" value="DESC">
<label for="desc">Price : High to Low</label>
</div>
</li>
</div>
Script
$(document).ready(function () {
filter_data();
function filter_data() {
$.post(
"fetch.php",
{
action: 'fetch_data',
cate: get_filter('cate'),
brand: get_filter('brand'),
model: get_filter('model'),
sort: get_filter('sort'),
date: get_filter('date')
}
)
.done(function (data) {
$('.filter_data').html(data);
});
}
function get_filter(class_name) {
var filter = [];
$('.' + class_name + ':checked').each(function () {
filter.push($(this).val());
});
return filter;
}
$('.filter_all').click(function () {
filter_data();
});
});
PHP
if (isset($_POST["action"])) {
$query = "SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle'";
if (!empty($_POST['cate'])) {
$query .= " AND sca IN (" . str_repeat("?,", count($_POST['cate']) - 1) . "?)";
} else {
$_POST['cate'] = []; // in case it is not set
}
if (!empty($_POST['brand'])) {
$query .= " AND product_brand IN (" . str_repeat("?,", count($_POST['brand']) - 1) . "?)";
} else {
$_POST['brand'] = []; // in case it is not set
}
if (!empty($_POST['model'])) {
$query .= " AND mdl IN (" . str_repeat("?,", count($_POST['model']) - 1) . "?)";
} else {
$_POST['model'] = []; // in case it is not set
}
$query .= " ORDER BY pdt DESC";
if (!empty($_POST['sort'])) {
if ($_POST["sort"][0] == "ASC" || $_POST["sort"][0] == "DESC") { //simplistic whitelist
$query .= ", prs " . $_POST['sort'][0];
}
}
echo $query;
$stmt = $conn->prepare($query);
$params = array_merge($_POST['cate'], $_POST['brand'], $_POST['model']);
$stmt->execute($params);
$result = $stmt->fetchAll();
$total_row = $stmt->rowCount();
$output = '';
Add a third button for "Sort by date".
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="sortby">
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="asc" value="ASC">
<label for="asc">Price : Low to High</label>
</div>
</li>
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="desc" value="DESC">
<label for="desc">Price : High to Low</label>
</div>
</li>
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="date" value="date">
<label for="date">Date : High to Low</label>
</div>
</li>
</div>
if (empty($_POST['sort']) || $_POST['sort'][0] == "date") {
$query .= " ORDER BY pdt DESC";
} elseif ($_POST["sort"][0] == "ASC" || $_POST["sort"][0] == "DESC") {
$query .= " ORDER BY prs " . $_POST['sort'][0];
}

How to display the selected value in a multiple dropdownlist?

I am created form to select value to display in my page table.Below is my code to select multiple values and search to display, but it is not displaying the selected values (type and dates) in multiple dropdownlist. Anyone can help me solve this problem? Thanks.
My frontend coding:
<div class="box inverse">
<div class="row">
<div class="col-lg-12">
<header>
<h5>Search</h5>
</header>
<form id="transaction_search">
<div class="form-group">
<div class="col-lg-12">
<div class="col-lg-3">
</div>
<div class="col-lg-12">
<div class="form-group">
<div class="col-lg-12">
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-5">
<select id="select_type" class="form-group form-control required"">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register"><?php echo $language["register"]; ?></option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
</div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created:</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search" type="button" class="btn btn-sm btn-primary" onclick="search_('transaction_search','transaction_result','transaction_table')">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="body" id="transaction_result" style="overflow:auto;">
</div><!--body-->
</form>
</div>
</div>
My backend coding (This is part of coding I try to select "Withdraw" option to test output, but did't display any data in the table. This coding is want to select "withdraw" and select what I choose the "date"):
<?php
foreach ($_POST as $key => $value) {
$_POST[$key] = trim(preg_replace('/\s+/', ' ', ($value)));
}
$arr_val = $_POST;
$loc = $arr_val['loc'];
$action = $arr_val['action'];
$select_type = $_POST['select_type'];
unset($arr_val['loc']);
unset($arr_val['action']);
unset($arr_val['select_type']);
$tbl_name = 'withdrawal_record';
if ($action == 'search' && $select_type == 'withdraw' ) {
if ($_POST['select_type'] != '' || $_POST['start_date'] != '' || $_POST['end_date'] != '' ) {
$sql = 'SELECT * FROM ' . $tbl_name . ' WHERE id is not null';
if($_POST['start_date']!='' && $_POST['end_date']!= '') {
$sql .=' and a.created between "' . date('Y-m-d', strtotime($_POST['start_date'])) . '" and "' . date('Y-m-d', strtotime($_POST['end_date'])) . '"';
}
$result_arr['sql'] = $sql;
$result_arr = get_data_list($sql);
$i = 1;
echo '<table id="dataTable_1" class="dataTable table table-bordered table-condensed table-hover table-striped" style="padding:0px;" border="1">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
foreach ($result_arr as $rs_search) {
echo "<tr>";
echo "<td>" . $i++ . "</td>";
echo "<td>" . $rs_search['created'] . "</td>";
echo "<td>" . $rs_search['withdraw_amount'] . "</td>";
echo '</td>';
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
}
}
?>
Below is jquery function:
function search_(form_id, div_id, act_file) {
var action = 'search';
var extra = '&action=' + action;
var serialized = $('#' + form_id).serialize();
var form_data = serialized + extra;
$.ajax({
//dataType: 'json',
type: 'POST',
url: '?f=' + act_file,
data: form_data,
beforeSend: function() {
show_overLay();
$('#' + div_id).html('');
},
success: function(data) {
hide_overLay('');
if (data) {
$("#" + div_id).append(data);
$('.dataTable').dataTable({
pageLength: 25,
destroy: true
});
} else {
hide_overLay("Please fill in the field.");
}
//console.log(data);
}
});
}
Below is my "withdrawal_record" table:
withdrawal_record
Below is my output, and didn't show the data what I select. Actually I want to select date between 04/11/19 and 07/11/19 and select type is "Withdraw" :
Output 1
If success , that will show like below the output picture:
Output 2
Error output:
Output 3
In html add multiple attribute to select :
<select id="select_type" class="form-group form-control required" multiple>
In JQuery make these changes:
Remove these :
var action = 'search';
var extra = '&action=' + action;
var serialized = $('#' + form_id).serialize();
var form_data = serialized + extra;
And in Ajax request:
Remove these lines:
data: form_data,
Add these lines:
data:{action:"search","loc":$("#select_type").val().toString()},
In PHP remove these lines :
foreach ($_POST as $key => $value) {
$_POST[$key] = trim(preg_replace('/\s+/', ' ', ($value)));
}
$arr_val = $_POST;
$loc = $arr_val['loc'];
$action = $arr_val['action'];
And these lines instead:
$loc = $_POST['loc'];
$action = $_POST['action'];
$loc =explode(","$loc );
foreach($loc AS $val)
{
echo $val; // your locations
}

Unique id/class increment to show different Input using Jquery

I've got a form which currently works as a single line allowing different input fields based on the Production drop down.
i.e. If you choose CPE or CPM you get the inputs for Impressions and Targeting. If you choose Eggs2Go then you only get Impressions.
While this works so far, if I add a line and use the drop down menu it affects the first ID tag changing that line and not the new lines options.
function showTargetCheck(that) {
if (that.value == "CPM" || that.value == "CPE" || that.value == "SWIFT") {
document.getElementById("showTarget").style.display = "block";
document.getElementById("showImpressions").style.display = "none";
}
else if (that.value == "EGGS") {
document.getElementById("showImpressions").style.display = "block";
document.getElementById("showTarget").style.display = "none";
}
else {
document.getElementById("showTarget").style.display = "none";
document.getElementById("showImpressions").style.display = "none";
}
}
Is there a way I can give an auto increment to each new line so that the showTarget and ShowImpressions act individually?
Details in the fiddle
You can't repeat ID's in a page, they must be unique by definition.
The common approach to repeating rows like this is to use common classes for like elements and isolate instances within your event handlers by looking up to parent row element and then looking inside that specific row for needed elements
Assume we change all the repeating ID's to same class name you would then do something like the following in jQuery
$('#tableContainer').on('change', 'select', function(e){
// get row instance starting from `this`
var $row = $(this).closest('.tableRow'),
currVal = $(this).val(),
// flags for toggling various classes
showImpressions = false,
showTarget = false;
if (currVal == "CPM" || currVal == "CPE" || currVal == "SWIFT") {
showTarget = true;
}
// other conditionals to set flags based on other values
// now toggle display of instances of classes within row instance
$row.find('.showTarget').toggle(showTarget);
$row.find('.showImpressions').toggle(showImpressions);
});
As mentioned by charlietfl, ids are unique.
Using your existing coding format
Change all id to class
Find the second parentNodes of your select tag
Once the main parentNode is Found, find your siblings input boxes that you need to change
Snippet below
/*
New IO Line
*/
$('#addNewIOLine').click(function() {
$('#IOLine').append(
'<div class="tableRow">' +
'<div class="tableLeft">' +
'<input type="date" name="startDate" placeholder="MM/DD/YYYY" class="date">' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="date" name="endDate" placeholder="MM/DD/YYYY" class="date">' +
'</div>' +
'<div class="tableMiddle">' +
'<select name="products" onchange="showTargetCheck(this);">' +
'<option>Select one</option>' +
'<option disabled>---</option>' +
'<option value="PRODUCTION">Production</option>' +
'<option value="CPE">CPE</option>' +
'<option value="CPM">CPM</option>' +
'<option value="SWIFT">Swift</option>' +
'<option value="EGGS">Eggs2Go</option>' +
'</select>' +
'</div>' +
'<div class="showImpressions" style="display: none;">' +
'<div class="tableMiddle">' +
'<input type="number" placeholder="1000" class="imps">' +
'</div>' +
'</div>' +
'<div class="showTarget" style="display: none;">' +
'<div class="tableMiddle">' +
'<input type="text" placeholder="Targeting">' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="number" placeholder="1000" class="imps">' +
'</div>' +
'</div>' +
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="rates">' +
'</span>' +
'</div>' +
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="grosscost">' +
'</span>' +
'</div>' +
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="netcost">' +
'</span>' +
'</div>' +
'<div class="tableRight">' +
'<input type="text" placeholder="Notes" class="notes">' +
'</div>' +
'</div>');
});
#tableContainer {
display: table;
}
.tableRow {
display: table-row;
}
.tableLeft,
.tableRight,
.tableMiddle {
display: table-cell;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function showTargetCheck(that) {
if (that.value == "CPM" || that.value == "CPE" || that.value == "SWIFT") {
that.parentNode.parentNode.getElementsByClassName("showTarget")[0].style.display = "block";
that.parentNode.parentNode.getElementsByClassName("showImpressions")[0].style.display = "none";
} else if (that.value == "EGGS") {
that.parentNode.parentNode.getElementsByClassName("showImpressions")[0].style.display = "block";
that.parentNode.parentNode.getElementsByClassName("showTarget")[0].style.display = "none";
} else {
that.parentNode.parentNode.getElementsByClassName("showTarget")[0].style.display = "none";
that.parentNode.parentNode.getElementsByClassName("showImpressions")[0].style.display = "none";
}
}
</script>
<!-- IO Table -->
<div id="tableContainer">
<!-- IO Row -->
<div class="tableRow">
<!-- Start Date -->
<div class="tableLeft">
<h4>Start</h4>
<input type="date" name="startDate" placeholder="MM/DD/YYYY" class="date">
</div>
<div class="tableMiddle">
<h4>End</h4>
<input type="date" name="endDate" placeholder="MM/DD/YYYY" class="date">
</div>
<!-- End Date -->
<!-- Products -->
<div class="tableMiddle">
<h4>Products</h4>
<select name="products" onchange="showTargetCheck(this);">
<option>Select one</option>
<option disabled>---</option>
<option value="PRODUCTION">Production</option>
<option value="CPE">CPE</option>
<option value="CPM">CPM</option>
<option value="SWIFT">Swift</option>
<option value="EGGS">Eggs2Go</option>
</select>
</div>
<!-- Products -->
<!-- Show Impressions Only -->
<div class="showImpressions" style="display: none;">
<div class="tableMiddle">
<h4>Impressions</h4>
<input type="number" placeholder="1000" class="imps">
</div>
</div>
<!-- Show Impressions Only -->
<!-- Show Targeting & Impressions -->
<div class="showTarget" style="display: none;">
<div class="tableMiddle">
<h4>Targeting</h4>
<input type="text" placeholder="Targeting">
</div>
<div class="tableMiddle">
<h4>Impressions</h4>
<input type="number" placeholder="1000" class="imps">
</div>
</div>
<!-- Show Targeting & Impressions -->
<!-- Rates -->
<div class="tableMiddle">
<h4>Rate</h4>
<span class="input-symbol-dollar">
<input type="number" placeholder="0.00" min="0.01" class="rates">
</span>
</div>
<!-- Rates -->
<!-- Gross Cost -->
<div class="tableMiddle">
<h4>Gross</h4>
<span class="input-symbol-dollar">
<input type="number" placeholder="0.00" min="0.01" class="grosscost">
</span>
</div>
<!-- Gross Cost -->
<!-- Net Cost -->
<div class="tableMiddle">
<h4>Net</h4>
<span class="input-symbol-dollar">
<input type="number" placeholder="0.00" min="0.01" class="netcost">
</span>
</div>
<!-- Net Cost -->
<!-- Notes -->
<div class="tableRight">
<h4>Notes</h4>
<input type="text" placeholder="Notes" class="notes">
</div>
<!-- Notes -->
</div>
<!-- IO Row -->
</div>
<!-- ADD NEW IO LINE -->
<div id="IOLine"></div>
<button type="button" id="addNewIOLine">+ Add New Line</button>
<!-- ADD NEW IO LINE -->
(Posted on behalf of the OP).
Working version here if anyone needs the reference:
/*
Toggle Impressions and Targeting Fields
*/
$('#tableContainer').on('change', 'select', function(e) {
// get row instance starting from `this`
var $row = $(this).closest('.tableRow'),
currVal = $(this).val(),
showImpressions = true,
showTarget = true,
showDefault = true;
if (currVal == "CPM" || currVal == "CPE" || currVal == "SWIFT") {
showTarget = true;
showImpressions = false;
showDefault = false;
} else if (currVal == "EGGS") {
showImpressions = true;
showTarget = false;
showDefault = false;
} else if (currVal == "DEFAULT" || currVal == "") {
showDefault = true;
showTarget = false;
showImpressions = false;
}
// other conditionals to set flags
// now toggle instances of classes within row
$row.find('.showTarget').toggle(showTarget);
$row.find('.showImpressions').toggle(showImpressions);
$row.find('.showDefault').toggle(showDefault);
});
/*
New IO Line
*/
$('#addNewIOLine').click(function() {
$('#IOLine').append(
'<div class="tableRow">' +
/* DATES */
'<div class="tableLeft">' +
'<input type="date" name="startDate" placeholder="MM/DD/YYYY" class="date">' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="date" name="endDate" placeholder="MM/DD/YYYY" class="date">' +
'</div>' +
/* PRODUCTS */
'<div class="tableMiddle">' +
'<select name="products">' +
'<option value="DEFAULT">Production</option>' +
'<option value="CPE">CPE</option>' +
'<option value="CPM">CPM</option>' +
'<option value="SWIFT">Swift</option>' +
'<option value="EGGS">Eggs2Go</option>' +
'</select>' +
'</div>' +
/* Show Default */
'<div class="showDefault">' +
'<div class="tableMiddle">' +
'<input type="text" placeholder="N/A" disabled>' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="number" placeholder="N/A" class="imps" disabled>' +
'</div>' +
'</div>' +
/* Show Impressions Only */
'<div class="showImpressions">' +
'<div class="tableMiddle">' +
'<input type="text" placeholder="N/A" disabled>' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="number" placeholder="1000" class="imps">' +
'</div>' +
'</div>' +
/* Show Targeting & Impressions */
'<div class="showTarget">' +
'<div class="tableMiddle">' +
'<input type="text" placeholder="Targeting">' +
'</div>' +
'<div class="tableMiddle">' +
'<input type="number" placeholder="1000" class="imps">' +
'</div>' +
'</div>' +
/* RATES */
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="rates">' +
'</span>' +
'</div>' +
/* GROSS COST */
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="grosscost">' +
'</span>' +
'</div>' +
/* NET COST */
'<div class="tableMiddle">' +
'<span class="input-symbol-dollar">' +
'<input type="number" placeholder="0.00" min="0.01" class="netcost">' +
'</span>' +
'</div>' +
/* NOTES */
'<div class="tableRight">' +
'<input type="text" placeholder="Notes" class="notes">' +
'</div>' +
'</div>');
});
http://jsfiddle.net/16godohe/9/.

Opencart add products in admin panel

I am developing a module for opencart 2.2.0.0 and I copied some of the basic functionality but mine isn't working.
I got to the point where the view is displaying the product form but in the original module the products which are available are shown and with my module they don't.
Original code from admin:
Controller
class ControllerModuleFeatured extends Controller {
private $error = array();
public function index() {
$this->load->language('module/featured');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('featured', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], true));
}
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_product'] = $this->language->get('entry_product');
$data['entry_limit'] = $this->language->get('entry_limit');
$data['entry_width'] = $this->language->get('entry_width');
$data['entry_height'] = $this->language->get('entry_height');
$data['entry_status'] = $this->language->get('entry_status');
$data['help_product'] = $this->language->get('help_product');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (isset($this->error['name'])) {
$data['error_name'] = $this->error['name'];
} else {
$data['error_name'] = '';
}
if (isset($this->error['width'])) {
$data['error_width'] = $this->error['width'];
} else {
$data['error_width'] = '';
}
if (isset($this->error['height'])) {
$data['error_height'] = $this->error['height'];
} else {
$data['error_height'] = '';
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], true)
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/featured', 'token=' . $this->session->data['token'], true)
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/featured', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true)
);
}
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('module/featured', 'token=' . $this->session->data['token'], true);
} else {
$data['action'] = $this->url->link('module/featured', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
}
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], true);
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
$data['token'] = $this->session->data['token'];
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
$this->load->model('catalog/product');
$data['products'] = array();
if (!empty($this->request->post['product'])) {
$products = $this->request->post['product'];
} elseif (!empty($module_info['product'])) {
$products = $module_info['product'];
} else {
$products = array();
}
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['name']
);
}
}
if (isset($this->request->post['limit'])) {
$data['limit'] = $this->request->post['limit'];
} elseif (!empty($module_info)) {
$data['limit'] = $module_info['limit'];
} else {
$data['limit'] = 5;
}
if (isset($this->request->post['width'])) {
$data['width'] = $this->request->post['width'];
} elseif (!empty($module_info)) {
$data['width'] = $module_info['width'];
} else {
$data['width'] = 200;
}
if (isset($this->request->post['height'])) {
$data['height'] = $this->request->post['height'];
} elseif (!empty($module_info)) {
$data['height'] = $module_info['height'];
} else {
$data['height'] = 200;
}
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/featured', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/featured')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!$this->request->post['width']) {
$this->error['width'] = $this->language->get('error_width');
}
if (!$this->request->post['height']) {
$this->error['height'] = $this->language->get('error_height');
}
return !$this->error;
}
}
View
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-featured" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-featured" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="<?php echo $help_product; ?>"><?php echo $entry_product; ?></span></label>
<div class="col-sm-10">
<input type="text" name="product_name" value="" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($products as $product) { ?>
<div id="featured-product<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
<input type="hidden" name="product[]" value="<?php echo $product['product_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-limit"><?php echo $entry_limit; ?></label>
<div class="col-sm-10">
<input type="text" name="limit" value="<?php echo $limit; ?>" placeholder="<?php echo $entry_limit; ?>" id="input-limit" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-width"><?php echo $entry_width; ?></label>
<div class="col-sm-10">
<input type="text" name="width" value="<?php echo $width; ?>" placeholder="<?php echo $entry_width; ?>" id="input-width" class="form-control" />
<?php if ($error_width) { ?>
<div class="text-danger"><?php echo $error_width; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-height"><?php echo $entry_height; ?></label>
<div class="col-sm-10">
<input type="text" name="height" value="<?php echo $height; ?>" placeholder="<?php echo $entry_height; ?>" id="input-height" class="form-control" />
<?php if ($error_height) { ?>
<div class="text-danger"><?php echo $error_height; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('input[name=\'product_name\']').autocomplete({
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['product_id']
}
}));
}
});
},
select: function(item) {
$('input[name=\'product_name\']').val('');
$('#featured-product' + item['value']).remove();
$('#featured-product').append('<div id="featured-product' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product[]" value="' + item['value'] + '" /></div>');
}
});
$('#featured-product').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
//--></script></div>
<?php echo $footer; ?>
Which results in:
And here is my module code:
Controller
class ControllerModulePopular extends Controller {
private $error = array();
public function index() {
$this->load->language('module/popular');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
// Validate and check for errors
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('popular', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->cache->delete('product');
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
// Set language
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_product'] = $this->language->get('entry_product');
$data['entry_limit'] = $this->language->get('entry_limit');
$data['entry_width'] = $this->language->get('entry_width');
$data['entry_height'] = $this->language->get('entry_height');
$data['entry_status'] = $this->language->get('entry_status');
$data['help_product'] = $this->language->get('help_product');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (isset($this->error['name'])) {
$data['error_name'] = $this->error['name'];
} else {
$data['error_name'] = '';
}
if (isset($this->error['width'])) {
$data['error_width'] = $this->error['width'];
} else {
$data['error_width'] = '';
}
if (isset($this->error['height'])) {
$data['error_height'] = $this->error['height'];
} else {
$data['error_height'] = '';
}
// Breadcrumbs
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/popular', 'token=' . $this->session->data['token'], 'SSL')
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/popular', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL')
);
}
// Setting the action variable depending upon the presence of module_id.
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('module/popular', 'token=' . $this->session->data['token'], 'SSL');
} else {
$data['action'] = $this->url->link('module/popular', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
}
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
// If the module_id is present in URL and the action is not POST then this will fetch the module information based on the module_id.
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
// Products
$this->load->model('catalog/product');
$data['products'] = array();
if (!empty($this->request->post['product'])) {
$products = $this->request->post['product'];
} elseif (!empty($module_info['product'])) {
$products = $module_info['product'];
} else {
$products = array();
}
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['name']
);
}
}
// End products
if (isset($this->request->post['limit'])) {
$data['limit'] = $this->request->post['limit'];
} elseif (!empty($module_info)) {
$data['limit'] = $module_info['limit'];
} else {
$data['limit'] = 5;
}
if (isset($this->request->post['width'])) {
$data['width'] = $this->request->post['width'];
} elseif (!empty($module_info)) {
$data['width'] = $module_info['width'];
} else {
$data['width'] = 200;
}
if (isset($this->request->post['height'])) {
$data['height'] = $this->request->post['height'];
} elseif (!empty($module_info)) {
$data['height'] = $module_info['height'];
} else {
$data['height'] = 200;
}
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
// Loading the header, footer and column_left controller and then setting the output with the $data. Finally HTML will be constructed from the template/data.
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/popular.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/popular')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!$this->request->post['width']) {
$this->error['width'] = $this->language->get('error_width');
}
if (!$this->request->post['height']) {
$this->error['height'] = $this->language->get('error_height');
}
return !$this->error;
}
}
View
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-popular" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-popular" class="form-horizontal">
<!-- Entry Name -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
<?php } ?>
</div>
</div>
<!-- Entry Name -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="<?php echo $help_product; ?>"><?php echo $entry_product; ?></span></label>
<div class="col-sm-10">
<input type="text" name="product_name" value="" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($products as $product) { ?>
<div id="featured-product<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
<input type="hidden" name="product[]" value="<?php echo $product['product_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
<!-- Entry Limit -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-limit"><?php echo $entry_limit; ?></label>
<div class="col-sm-10">
<input type="text" name="limit" value="<?php echo $limit; ?>" placeholder="<?php echo $entry_limit; ?>" id="input-limit" class="form-control" />
</div>
</div>
<!-- Entry Limit -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-width"><?php echo $entry_width; ?></label>
<div class="col-sm-10">
<input type="text" name="width" value="<?php echo $width; ?>" placeholder="<?php echo $entry_width; ?>" id="input-width" class="form-control" />
<?php if ($error_width) { ?>
<div class="text-danger"><?php echo $error_width; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-height"><?php echo $entry_height; ?></label>
<div class="col-sm-10">
<input type="text" name="height" value="<?php echo $height; ?>" placeholder="<?php echo $entry_height; ?>" id="input-height" class="form-control" />
<?php if ($error_height) { ?>
<div class="text-danger"><?php echo $error_height; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('input[name=\'product_name\']').autocomplete({
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['product_id']
}
}));
}
});
},
select: function(item) {
$('input[name=\'product_name\']').val('');
$('#featured-product' + item['value']).remove();
$('#featured-product').append('<div id="featured-product' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product[]" value="' + item['value'] + '" /></div>');
}
});
$('#featured-product').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
//--></script>
</div>
<?php echo $footer; ?>
Why am I not able to choose products from the available list?
P.S. sorry for the long code examples but I didn't knew what's needed to explain my problem.
You have missed a line of code in Controller file,
Add below line
$data['token'] = $this->session->data['token'];
Above $this->load->model('catalog/product'); in Controller file

Categories