getting form data values as null in %fdat varible in EmbPerl - enctype

I am getting form data values as NULL in %fdat varible in EmbPerl, if we go for file upload and form property enctype="multipart/form-data".
Here is the EmbPerl code :
if(( defined $fdat{file} && $fdat{file} ) && $fdat{upload} )
{
$filename = "/tmp/sample.txt";<br/>
open (FILE, "> $filename") || die("open failed: $!");
while (read($fdat{file}, $buffer, 32768)) {
print FILE $buffer || die("print test: $!");
close FILE || die("close test: $!");
}
And HTML Code is :
<form name="form1" method="post" enctype="multipart/form-data" onsubmit="return validation();>
<table>
<tr> <td>Select EXCEL sheet</td>
<td><input type="file" name="file" value=""></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="upload" value="upload"></td>
</tr>
</table>
</form>
I am using EmbPerl/2.3.0, CGI/3.63, Perl/5.10.1, Apache/2.2.22 and Linux OS.

If you downgrade the CGI version to 3.37, then it will work.

Related

Laravel : How download custom blade table as excel

I have this table in blade file in laravel app:
<form method="POST" action="{{url('/download')}}" id="download_form">
#csrf <!-- {{ csrf_field() }} -->
<table class="table font-weight-bold w-100" id="excel_table">
<thead class="table-primary">
<tr>
<th class="text-center">#</th>
<th class="text-center">ناڤ</th>
<th class="text-center">كوم</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center">{{$counter}}</td>
<td class="text-right table-primary">{{$student->name}}</td>
<td class="text-center table-primary">{{$sub_total}}</td>
</tr>
</tbody>
</table>
<div>
<input type="hidden" name="file_contente" id="file_contente">
<input type="hidden" value="{{$stage}}" name="stage" id="">
<input type="hidden" value="{{$group}}" name="group" id="">
<button type="submit" id="download" class="btn btn-success">Download to excel</button>
</div>
</form>
</div>
<script>
$(document).ready(function() {
$('#download').click(function() {
var table_content = '<table>';
table_content += $('#excel_table').html();
table_content+= '</table>';
$('#file_content').val(table_content);
$('#download_form').html();
})
})
</script>
the controller is :
public function download(Request $req)
{
$temporary_html_file = './tmp_html/' . time() . '.html';
file_put_contents($temporary_html_file, $req->file_contente);
$reader = IOFactory::createReader('Html');
$spreadsheet = $reader->load($temporary_html_file);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $req->stage." ".$req->group . '.xlsx';
$writer->save($filename);
header('Content-Type: application/x-www-form-urlencoded');
header('Content-Transfer-Encoding: Binary');
header("Content-disposition: attachment; filename=\"".$filename."\"");
readfile($filename);
unlink($temporary_html_file);
unlink($filename);
}
I get error :
file_put_contents(./tmp_html/1634558222.html): failed to open stream:
No such file or directory
How can I solve it ? what is the mistake i did?
Edit
after I created the temp_html folder in public folder I get this error :
./tmp_html/1634560521.html is an Invalid HTML file.

When editing item from table, it pulls/saves the wrong information

So, I am working on a CRUD application and I am trying to make it so that I can edit the project when I click edit. I can successfully get the project ID with the req.params.id and it is successfully printing the correct information into the console but when I go to send it to my front end it is still updating the information for the first entry in the collection. Below is the code that I used for this.
Backend:
router.get('/edit/(:id)', function (req, res, next) {
var o_id = new ObjectId(req.params.id).toString();
db.collection('projects').find({
"_id": ObjectId(o_id).toString
}).toArray(function (err, result) {
if (err) return console.log(err)
// if user not found
if (!result) {
req.flash('error', 'Project not found with id = ' + req.params.id)
res.redirect('/projects')
} else { // if user found
console.log(result);
// render to views/user/edit.ejs template file
res.render('edit.ejs', {
user: req.user,
title: 'Edit User',
//data: rows[0],
projID: result[0]._id,
projName: result[0].projectName,
projStat: result[0].status,
projEngineer: result[0].engineer,
projCost: result[0].finalCost
});
}
});
});
Frontend for listing the projects:
<tbody>
<form action="/edit/<%=projID%>" method="post" class="d-flex align-self-center mx-auto" style="width:500px;height:500px;padding:0px;margin:0px;background-color:rgb(255,255,255);">
<tr>
<td>
<input type="text" class="form-control" name="projName" value="<%=projName %>">
</td>
<td>
<input type="text" class="form-control" name="projStat" value="<%=projStat %>">
</td>
<td>
<input type="text" class="form-control" name="projEngineer" value="<%=projEngineer %>">
</td>
<td>
<input type="text" class="form-control" name="projCost" value="<%=projCost %>">
</td>
</tr>
<button class="btn btn-primary btn-block" type="submit" style="background-color:rgb(4,148,74);">Submit</button>
</form>
</tbody>
Front end for editing
<tbody>
<form action="/edit/<%=projID%>" method="post" class="d-flex align-self-center mx-auto" style="width:500px;height:500px;padding:0px;margin:0px;background-color:rgb(255,255,255);">
<tr>
<td>
<input type="text" class="form-control" name="projName" value="<%=projName %>">
</td>
<td>
<input type="text" class="form-control" name="projStat" value="<%=projStat %>">
</td>
<td>
<input type="text" class="form-control" name="projEngineer" value="<%=projEngineer %>">
</td>
<td>
<input type="text" class="form-control" name="projCost" value="<%=projCost %>">
</td>
</tr>
<button class="btn btn-primary btn-block" type="submit" style="background-color:rgb(4,148,74);">Submit</button>
</form>
</tbody>
So I figured out my issue, the way I was querying the db returned all of the objects, I just had to loop over it and find where the id matches up. Thanks to #andreiNikolaenko for pointing out to check that.

Codeigniter - upload xls or xlsx and send the data to current view

i been stuck at this problem for a long time. I have a page with list of student and student's score. I want to add a button to upload xls/xlsx file at the current page, and after i uploaded the file, the column data fill the student's score input. How can i do that? Please i need help on this.
View
<form action="<?php echo base_url()?>index.php/nilai/ubahNilaiItem/<?php echo $idKelas ?>/<?php echo $id ?>" method="post" class="form-horizontal">
<button name="tombol" type="submit" value="import" class="btn pull-right btn-primary">Import</button>
<input type="file" name="inputfile" size="20" class="pull-right"/>
</div>
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th class="col-md-1 active">No</th>
<th class="col-md-2 active">NIM</th>
<th class="col-md-5 active">Nama</th>
<th class="col-md-1 active">Nilai</th>
</tr>
</thead>
<tbody>
<?php
$no = 0;
foreach($input as $i){
$no++
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $i->nim ?></td>
<td><?php echo $i->nama ?></td>
<td>
<div class="input-group">
<input hidden value="<?php echo $i->id ?>" name="id[]">
<input class="text-center form-control" value="<?php echo $i->angka_nilai ?>" name="angka[]"> //this is for inputing student score
<span class="input-group-addon"><i class="glyphicon glyphicon-question-sign" data-toggle="tooltip" title="Gunakan koma (,) untuk nilai desimal"></i></span>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="tombol" value="simpan" type="submit" class="btn pull-right btn btn-primary">Simpan</button>
</form>
Controller for the view
public function itemView($idKelas,$id){
//load model
$this->load->model('nilais');
//take data from database
$data2['input'] = $this->nilais->input_item($idKelas,$id)->result();
$data3['detail'] = $this->nilais->tampil_detail($idKelas)->result();
//merge array
$data4 = array_merge($data2, $data3);
$data4['item'] = $this->nilais->item_detail($id)->result();
$data4['idKelas'] = $idKelas;
$data4['id'] = $id;
//take data for header and notification
$data6['notifikasi'] = $this->nilais->notifikasi($this->session->userdata('id_user'))->result();
$data6['count'] = count($data6['notifikasi']);
$data6['matakuliah'] = $idKelas;
$data6['active'] = 'nilai';
//load view nilai and header
$this->load->view('nilai/item_view',$data4);
$this->load->view('header',$data6);
}
Update
Now i used PHPExcel, but i cant get the null cell. How can i get all cell even if its null?
Controller for excel
public function importAction($idKelas,$id){
//load model
$this->load->model('nilais');
$this->load->library('excel');
$config['upload_path'] = './file/excel/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ($this->upload->do_upload('inputfile')) {
$upload_data = $this->upload->data();
$objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;
}
}

Uncaught ReferenceError: JST is not defined on Sails 0.11

while using Sails I get this error Uncaught ReferenceError: JST is not defined on the chrome console everytime this line of code:
JST['SailsApp/assets/templates/addUser.ejs']( obj )
The content of the addUser.ejs file is:
<tr data-id="<%= user.id %>" data-model="user">
<% if (user.online) { %>
<td><span class="fa fa-circle fa-lg fa-circle-green"></span></td>
<% } else { %>
<td><span class="fa fa-circle fa-lg fa-circle-red"></span></td>
<% } %>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.title %></td>
<td><%= user.email %></td>
<% if (user.admin) { %>
<td> <span class="glyphicon glyphicon-king" > </span></td>
<% } else { %>
<td> <span class="glyphicon glyphicon-user" > </span></td>
<% } %>
<td>Show </td>
<td>Edit </td>
<td>
<form action="/user/destroy/<%= user.id %>" method="POST">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" class="btn btn-sm btn-danger" value="Delete"/>
<input type="hidden" name="_csrf" value="<%= _csrf%>" />
</form>
</td>
</tr>
The compiled .jst file is not appearing in .tmp/public folder, and if I run sudo grunt jst manually then it's created but the chrome console continues giving the same error.
Any help would be much appreciated.
Thanks in advance!
I had the same problem recently. Under 'tasks/register/compileAssets.js', try moving jst:dev below copy:dev.
module.exports = function (grunt) {
grunt.registerTask('compileAssets', [
'clean:dev',
'less:dev',
'copy:dev',
'jst:dev',
'coffee:dev'
]);
};
It seems that the jst:dev task is looking for the template files in the .tmp/ dir, but they aren't moved from assets/ until the copy task executes. You'll see this if you run grunt and get an error like:
Running "jst:dev" (jst) task
>> Destination not written because compiled files were empty.

How can I differentiate between two POST calls in expressjs?

I currently have two different forms on one inven.ejs file:
One for simple description:
///inven.ejs
<form method="POST" value="inven">
<div id="some-form" style="display: none;">
<table>
<tr>
<td><label for="item">Item</label></td>
<td><input type="text" name="item" required/></td>
</tr>
<tr>
<td><label for="text-box-value">Value</label></td>
</tr>
<tr>
<td><label for="comments">Comments</label></td>
<td><textarea rows="4" cols="50" required></textarea></td>
</tr>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
</div>
</form>
and another for file upload:
///inven.ejs
<div id="fileUp">
<form id="fileUpload" name="fileUpload" enctype="multipart/form-data" method="post">
<fieldset>
<input type="file"id="fileSelect">
<input type="submit" name="upload" value="upload">
</fieldset>
</form>
</div>
In express, how can I differentiate between these two posts in my list.js file?
router.post('/list', function(req,res){
// ???
});
Do I need two routers? Am I completely doing this incorrectly?
Thank you!
EDIT:
Included an image, if it helps?
It makes sense to have forms post to a different address.
That is, <form id="fileUpload" action="file_upload" ... >
Then, if the form is found at http://server/my_form, it will post to http://server/file_upload
In your Node.js router you need to catch that and done.
If you still desire to send both forms to the same address, you can then use hidden fields.
Example: <input type="hidden" name="form_type" value="file_up_form">. Then in your Node.js you check for the argument form_type and check its value.

Resources