I am trying to handle the Post request in the same page. But i am facing some issues when handling post parameters.I am always getting 'value as empty'.
Below is my code.
<body>
<?php include 'Header.html' ?>
<div class="container">
<div style="margin:10% 30% 0% 30%;">
<div class="panel panel-default">
<div class="panel-heading">
<h4 align="center" style="font-family: monospace;"><p>Upload External Device Report</p></h4>
</div>
<div class="panel-body">
<div class="container col-lg-12">
<form class="form-horizontal" action="UploadExternalReport.php" role="form" method='post' enctype="multipart/form-data">
<div class="input-group">
<input type="file" class="filestyle" data-buttonName="btn-primary" name="fileToUpload" accept=".xls ,.xlsx" required="required" >
</div>
<br/>
<button type="submit" class="btn btn-default">Upload</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
// require_once '../PHPExcel/IOFactory.php';
if(isset($_POST["fileToUpload"]) && trim($_POST["fileToUpload"]) != ""){
echo $_POST['fileToUpload'];
}
else
{
echo'value is empty';
}?>
I tried !empty($_POST["fileToUpload"]) in if condition. But still its getting as 'value is empty'.
You are supposed to use the $_FILES array, not $_POST.
So make use of $_FILES['fileToUpload'].
http://php.net/manual/en/reserved.variables.files.php
Related
I wanted to learn how to make a cms. I know crud operations but when I want to take some value from ckeditor and put it in MySQL database is not working. Is making me nervous I know I did a mistake but I don't know where.
<?php
if (isset($_POST['submit'])){
if(isset($_POST['editor']) && !empty($_POST['editor'])){
$content = $_POST['editor'];
}else{
$empty_error = "didnt send to database";
}
if (isset($content) && !empty($content)) {
$insert_q = "INSERT INTO `contact`(continut) VALUES ('$content')";
if (mysqli_query($conn,$insert_q)) {
// code...
}else{
$submit_error ="submit didnt work";
}
}
}
?>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-title">Add</div>
<hr>
<form action=" " method="post" enctype="multipart/form-data">
<textarea name="editor" class="ckeditor" rows="8" cols="80"></textarea>
<button type="submit">Save</button>
</form>
</div>
</div>
</div>
</div>
I would like to know how to run 2 or more input files in the same form, I have to upload some documents by using php, I made separate forms and they work, but I need to all together however I dont know how. I need to put only two forms as example actually I need to put 3 but the 3rd is larger so it would be much code to read with an example putting only two I would be able to do the rest.
Note: Form 1 and Form to upload data to different tables.
Form 1
<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
$fileName=$_FILES['myFile']['name'];
$fileTmpName=$_FILES['myFile']['tmp_name'];
$fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){?>
<div class="alert alert-danger">
INVALID FILE
</div>
<?php }else{
$handle = fopen($fileTmpName, 'r');
$k = 0;
$energies = array ();
while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
$k++;
if ( $k > 1 ) {
$energies[] = $myData[3];
}
}
list($e1, $e2, $e3) = $energies;
$query = "INSERT INTO metlab.resultados_impacto_junta (energy1, energy2, energy3) VALUES ($e1, $e2, $e3)";
$run = mysql_query($query);
if(!$run){
die("error in uploading file".mysql_error());
}else{ ?>
<div class="alert alert-success">
SUCCESS
</div>
<?php }
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center">
RESULTS
</h3></hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" name ="uploadBtn" class="btn btn-info">
</div>
</div>
</div>
</form>
Form 2
<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
$fileName=$_FILES['myFile']['name'];
$fileTmpName=$_FILES['myFile']['tmp_name'];
//RUTA DEL ARCHIVO
$fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
//FORMATOS DE ARCHIVO PERMITIDOS
$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){?>
<div class="alert alert-danger">
INVALID FILE
</div>
<?php }else{
$handle = fopen($fileTmpName, 'r');
$k = 0;
while (($myData = fgetcsv($handle,1000,','))!== FALSE){
$k++;
if ( $k > 4 ) {
$valor_dureza = $myData[3];
$query = "INSERT INTO metlab.resultados_tension_junta (size,yield,tensile,ra,elongacion)
VALUES ('".$valor_dureza."')";
$run = mysql_query($query);
}
}
if(!$run){
die("error in uploading file".mysql_error());
}else{ ?>
<div class="alert alert-success">
SUCCESS
</div>
<?php }
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center">
RESULTS
</h3></hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" name ="uploadBtn" class="btn btn-info">
</div>
</div>
</div>
</form>
I would like a form like this:
With the Fk I would know which number the 3 docs belong to.
I solved the problem myself I just changed the vars of the forms and thats all, Its a dirty and bad solution but it works for now.
I have this simple code:
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="titleDiv">
<h1>Image canvas</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="previewDiv">
<img src="#" id="imgPreview">
</div>
<div class="buttonsDiv">
<button type="button" class="rotateButtons" id="rotateLeft">
<i class="fa fa-arrow-circle-left" aria-hidden="true"></i> Rotate left
</button>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="uploadDiv">
<form action="upload.php" method="POST" id="#uploadForm">
<!--<input type="file" name="img" accept="image/*" id="imgUpload">-->
<input type="number" name="number" value="0">
<input type="submit" value="Submit">
</form>
</div>
</div>
</div>
</div>
And on the upload.php file I have only 2 lines of code:
var_dump($_POST);
var_dump($_GET);
When I try to send this data over POST I get an output array(0) { } .
Though when I change the form method to method="GET" everything works normal and I receive the the data great on the upload.php.
I looked the php.ini file (I am using xampp as a local server) and the line ;enable_post_data_reading=Off is commented. Can someone help sort out this problem?
Check if you're actually using your webserver. What's the address on your navigation bar? It should be localhost without some strange ports. If it's pointing to 63342 that could be your problem.
edit: that could happen if you're using an IDE
It's work fine when I am inputing data using select option on CI but when I try to edit or update my data again my form became weird like this (See picture edit form)
Here my controller :
public function updateobat ($kode_obat){
if($_POST==null){
$this->load->model('a_model');
$data['hasil'] = $this->a_model->select2($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
$this->template->admin('admin/edit-obat',$data);
}else{
$this->load->model('a_model');
$this->a_model->update_obat($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['obat'] = $this->a_model->ambil_obat();
$this->template->admin('admin/data-obat',$data);
}
}
Model
public function update_obat($kode_obat){
$nama_obat = $this->input->post('nama_obat');
$kode_jenis_obat = $this->input->post('kode_jenis_obat');
$stok= $this->input->post('stok');
$data = array('nama_obat' => $nama_obat,
'kode_jenis_obat' => $kode_jenis_obat,
'stok' => $stok,
);
$this->db->where('kode_obat',$kode_obat);
$this->db->update('tb_obat',$data);
}
public function select2($kode_obat){
return $this->db->get_where('tb_obat', array('kode_obat' => $kode_obat))->row();
}
View
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-8">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Silahkan melakukan edit stok obat</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/updateobat/'.$hasil->kode_obat; ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-medkit"></i></span>
<input type="text" class="form-control" name="nama_obat" id="exampleInputNama" placeholder="Nama Obat" action="<?php echo form_input('nama_obat',$hasil->nama_obat);?>">
</div>
</div>
<div class="form-group">
<label for="exampleInputJenis">Jenis Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<select name="kode_jenis_obat" class="form-control" action="<?php echo form_dropdown('kode_jenis_obat',$hasil->kode_jenis_obat);?>">
<option value="none" selected="selected">Pilih Jenis Obat</option>
<!-----Displaying fetched cities in options using foreach loop ---->
<?php foreach($tb_jenisobat as $jenisobat):?>
<option value="<?php echo $jenisobat->kode_jenis_obat?>"><?php echo $jenisobat->nama_jenis_obat?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Stok Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<input type="text" class="form-control" name="stok" id="exampleInputUsername" placeholder="Stok" action="<?php echo form_input('stok',$hasil->stok);?>">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
I am using form_dropdown for my select option
I think there is something wrong with my controller syntax?
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
But I am not sure enough, can someone help me?
Picture : edit form
I need to know how can I implement a user level access in my site. I need to hide forms and images when users with level are member...
in my DB I have a USERS TABLE with:
id
nombre
apellido
username
password
level
level is ENUM and have this two selects: administrator and member
I can hide scripts in any page with this type of PHP code:
<?php if(basename($_SERVER['PHP_SELF']) == 'contact.php') { ?>
<script src="js/jquery.js"></script>
<?php } ?>
and works very well, but I don't know how can hide per example this:
<div class="box span6">
<div class="box-header well" data-original-title>
<h2><i class="icon-picture"></i> <?php $translate->__('Save images'); ?></h2>
<div class="box-icon">
<i class="icon-chevron-up"></i>
<i class="icon-remove"></i>
</div>
</div>
<div class="box-content">
<form action="upload.php" method="post" name="image_upload" id="image_upload" enctype="multipart/form-data">
<label><?php $translate->__('Images type'); ?> (gif, jpg, png)</label><br />
<input type="file" size="45" name="uploadfile" id="uploadfile" class="file margin_5_0" onchange="ajaxUpload(this.form);" />
<div id="upload_area" class="corners align_center">
<?php $translate->__('Please select one image'); ?>.
</div>
</form>
</div>
</div>
In google I saw this code but don't work in my partycular case:
<?php if($USERS->level == "administrator"): ?>
<?php endif; ?>
can you help me with my problem?
Hiding js files will not do you much good because an attacker can intercept the javascript file being served to your admin user, build his own mock-up page of your site and have it use that javascript. So make sure to put security and validation in your php code, especially inside your upload.php
Obviously this $USERS->level can be replaced by any variable that you have set.
<?php if($USERS->level == "administrator")
{
?>
<div class="box span6">
<div class="box-header well" data-original-title>
<h2><i class="icon-picture"></i> <?php $translate->__('Save images'); ?></h2>
<div class="box-icon">
<i class="icon-chevron-up"></i>
<i class="icon-remove"></i>
</div>
</div>
<div class="box-content">
<form action="upload.php" method="post" name="image_upload" id="image_upload" enctype="multipart/form-data">
<label><?php $translate->__('Images type'); ?> (gif, jpg, png)</label><br />
<input type="file" size="45" name="uploadfile" id="uploadfile" class="file margin_5_0" onchange="ajaxUpload(this.form);" />
<div id="upload_area" class="corners align_center">
<?php $translate->__('Please select one image'); ?>.
</div>
</form>
</div>
</div>
<?php
}
?>