CodeIgniter - Image submit button not working - codeigniter

I am using CI to create a project for a client, I have a submit button as an image but it doesn't seem to be submitting the form, the code I have at the moment is.
<input type="image" name="trialSubmit" id="trialSubmit" src="<?php echo base_url().'images/subscribe_free.jpg'; ?>" style="height:29px;width:207px;border-width:0px;" />
The code I have to use at the moment is as follows
<input type="submit" name="trialSubmit" value=" Subscribe Free " id="" class="button" />
If anyone could shed some light on why it's not working with the image, that would be tight.
Cheers,

Is it across all browsers? Or IE specific? What about if you add an onclick event just for testing? onclick="javascript:document.formidhere.submit()"
I'd recommend using either <input type="submit" /> or my preference would be to use <button type="submit> due to browser inconsistencies with <input type="image" /> and just style the button with something like:
button{
background:url('/images/subscribe_free.jpg') no-repeat;
height:29px;
width:207px;
border:0;
}

Try adding value="trialSubmit" to get the image to submit. Seemed to work for me.
You could also see if this answer helps:
image submit button

**inside the view(as comerciales in my case)**,
<form action= "<?php echo site_url()?>/admin/doupload" method="post" enctype="multipart/form-data" >
<b> Select the image to upload( Maximum size 500 kb, jpg, jpeg): </b>
<input style="color:#00A76F" type="file" name="fileToUpload" id="fileToUpload">
<div class="input-group" style="left:10%;width:85%;">
<input class="btn btn-success pull-right" style="background:#00A76F" type="submit" value="Upload Image" name="submit">
</div>
</form>
<div class="modal-body">
<div id="user_data">
<?php
if (!empty($massage)){
echo $massage ;
}
?>
</div>
**inside the controller define a method**
public function doupload(){
$root1 = $_SERVER['DOCUMENT_ROOT'];;
$target_dir = $root1."/nawaloka/uploads/";
// $target_dir = $root1."/nawaloka/application/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$data['massage']= "Sorry, your file is too large.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
$uploadOk = 0;
}
if (is_dir($target_dir) && is_writable($target_dir)) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
$data['massage']= "Sorry, only JPG, JPEG files are allowed.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$data['massage']= "Sorry, your file was not uploaded.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
// if everything is ok, try to upload file
} else {
array_map('unlink', glob("/var/www/html/nawaloka/uploads/*"));
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"/var/www/html/nawaloka/uploads/shamith.jpg")) {
$data['massage']= "The image ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
$data['massage']= "Sorry, there was an error while uploading your file.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
}
}
}

Related

codeigniter upload 2 files (images and video ) in separate field

Hi i am new in codeigniter , I want to upload image and video in same form with different field. i did but it store either one. last one is stored like video.
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
<input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
</div>
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
<input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
</div>
And my controller is
public function videoupdate() {
$data['user_data'] = $this->session->userdata('user_logged_in');
if (($this->session->flashdata('success')))
$data['success'] = $this->session->flashdata('success');
else
$data['error'] = $this->session->flashdata('error');
if (!empty($data['user_data']['user_id'])) {
$title = $_POST['title'];
$description = htmlentities($_POST['description']);
$target_dir = './cms/uploads/blog/video3/';
$temp = explode('.', $_FILES['fileToUpload']['name']);
$video = explode('.', $_FILES['videoToUpload']['name']);
if (!empty($_FILES['fileToUpload']['name'])) {
$newfilename = round(microtime(true)) . '.' . end($temp);
} else {
$newfilename = "";
}
if (!empty($_FILES['videoToUpload']['name'])) {
$videofilename = round(microtime(true)) . '.' . end($video);
} else {
$videofilename = "";
}
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $newfilename);
move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $videofilename);
$createddate = date('Y-m-d H:i:s');
$ipaddress = $_SERVER['REMOTE_ADDR'];
$status = $this->microblog_model->insertBlogvideo($title, $description, $newfilename, $videofilename, $data['user_data']['user_id'], $createddate, $ipaddress);
I found your code for upload files is working fine. As I check it in separate php file:
<pre>
<?php
if(isset($_POST)){
$temp = explode('.', $_FILES['fileToUpload']['name']);
$video = explode('.', $_FILES['videoToUpload']['name']);
try{
//upload image
if (!empty($_FILES['fileToUpload']['name'])) {
$newfilename = round(microtime(true)) . '.' . end($temp);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './uploads/' . $newfilename)){
echo "Image Uploaded<br/>";
}
}
//upload video
if (!empty($_FILES['videoToUpload']['name'])) {
$videofilename = round(microtime(true)) . '.' . end($video);
if(move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './uploads/' . $videofilename)){
echo "Video Uploaded<br/>";
}
}
}catch(Exception $e){
print_r($e);
}
}
?>
</pre>
<form method="post" enctype="multipart/form-data">
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
<input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
</div>
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
<input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
</div>
<input type="submit" name="submit">
</form>
You should check the size of video you are trying to upload and the maximum upload size allowed. I hope this will help. Because I checked it multiple times with many images and videos and found it is working fine.

Prevent duplicate entry in database Codeigniter

I would like to know how to prevent my upload function to insert duplicate entries into the database by using the uid.
public function upload(){
if(!empty($_FILES['uploaded_file']))
{
$path = FCPATH . "/file_attachments/signature_file/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
$base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path))
{
$data = array (
'uid' => $this->user->get_uid(),
'image' => $base64,
'name' => basename( $_FILES['uploaded_file']['name']),
);
$this->load->database('employee');
$this->db->insert('signatures', $data);
// echo "The file ". basename( $_FILES['uploaded_file']['name']).
// " has been uploaded";
$alert = "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
else
{
// echo "There was an error uploading the file, please try again!";
$alert ="There was an error uploading the file, please try again!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
}
}
Here's my view file. I haven't got the time to edit the unnecessary things in here. The problem is still that if it got duplicate entries the database error still shows up and that is what I'm preventing to do so. Cheers!
<div class="bod">
<div class="insta">
<form enctype="multipart/form-data" style="padding-top: 10px" name="exit_form" method="post" action="<?= base_url() ?>signature_uploader/upload">
<input type="hidden" name="uid" value="<?= $agent->get_uid() ?>" />
<input type="hidden" name="gid" value="<?= $agent->get_gid() ?>" />
<input type="hidden" name="fname" value="<?= $agent->get_fname() ?>" />
<input type="hidden" name="lname" value="<?= $agent->get_lname() ?>" />
<div style="text-align: center; font-size: 25pt; margin-bottom: 15px">Signature Uploader</div>
<table width="105%">
<tr>
<td width="40%"><strong>Name: </strong><input class="textinput" disabled="disabled" type="text" name="full_name" id="textfield3" size="35" value="<?= $agent->get_fullName() ?>" /></td>
<tr/>
<tr>
<td><label>Upload Image File:</label><br /> <input name="uploaded_file" type="file" accept=".png" required/>
</tr>
<table/>
<br />
<input type="submit" value="Submit" class="button1" />
</form>
<br/>
</div>
You can use from Codeigniter form validation to prevent from duplicate entry:
at first you must get the uid in the form view like this:
<input type="hidden" name="uid" value="<?php echo $this->user->get_uid() ?>">
and then in your controller:
public function upload(){
if(!empty($_FILES['uploaded_file']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('uid', 'UID', 'is_unique[signatures.uid]');
if ($this->form_validation->run() == FALSE)
{
// Your Code if the uid is duplicate
$alert ="Could't upload because of duplicate entry!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
else
{
// Your Code if the uid is unique
$path = FCPATH . "/file_attachments/signature_file/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
$base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
$data = array (
'uid' => $this->input->post('uid'),
'image' => $base64,
'name' => basename( $_FILES['uploaded_file']['name']),
);
$this->load->database('employee');
$this->db->insert('signatures', $data);
// echo "The file ". basename( $_FILES['uploaded_file']['name']).
// " has been uploaded";
$alert = "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
} else{
// echo "There was an error uploading the file, please try again!";
$alert ="There was an error uploading the file, please try again!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
}
}
}
Either you can check weather such file exists or not, but normally while creating a code which uploads file, has a function which will generate unique name for each file that we upload. So in such cases it will have duplicate entries with different name.
Only way i see is to create a function that will check weather the file name already exists in database.

AJAX submit both ENTER and CLICK

So I have a form to submit user login data. But currently the default of data submitting is by clicking a button. This script can work both click the button or pressing ENTER on Mozilla, but not in Chrome. How to resolve my problem?
JAVASCRIPT:
$(document).ready(function(){
$('#buttonSignIn').click(function(s){
s.preventDefault();
var error = false;
var _wait_ = $(this).attr('data-wait');
var user = $('#user').val();
var pass = $('#password_signin').val();
var waitHtml = $(this).html();
var _wait = '...';
var _waitHtml = waitHtml + _wait;
if( user == '' || user == 0 || trim( user ).length == 0 ) {
var error = true;
$('#user').focus();
}
if( error == false ){
$('#buttonSignIn').attr({'disabled' : 'true'}).html(_waitHtml).css({'opacity':'0.5'});
$.post("asset/ajax/sign_in.php", $("#signin_form").serialize(),function(result){
if( result == 'True' ){
$('#signin_form input, .forgot, #buttonSignIn, #recover_pass_form').remove();
$('#success_signin').fadeIn(500).html(_wait_);
$('#error').fadeOut(500);
$("#signin_form").reset();
$('body,html').animate({
scrollTop: 0
}, 500);
setTimeout(function()
{
window.location.reload();
},500 );
}
else{
$('#error').fadeIn(500);
$('#error').html( result );
$("#signin_form").reset();
$('#buttonSignIn').removeAttr('disabled').html(waitHtml).css({'opacity': 1});
}//<-- ELSE
});//<-- END $POST AJAX
}//<-- END ERROR == FALSE
});//<-- END FUNCTION CLICK
}
<form action="" method="post" name="form" id="signin_form" class="form_login signInForm">
<input type="text" name="user" id="user" placeholder="<?php echo $_SESSION['LANG']['placeholder_email_username']; ?>" title="<?php echo $_SESSION['LANG']['placeholder_email_username']; ?>" />
<input type="password" name="password" id="password_signin" placeholder="<?php echo $_SESSION['LANG']['placeholder_pass']; ?>" title="<?php echo $_SESSION['LANG']['placeholder_pass']; ?>" />
<span id="error"></span>
<span id="success_signin"></span>
<a style="cursor: pointer;" class="recover_pass buttonInside forgot buttonForgot"><?php echo $_SESSION['LANG']['placeholder_forgot_pass']; ?></a>
<button type="submit" id="buttonSignIn" class="button_class" data-wait="<?php echo $_SESSION['LANG']['please_wait']; ?>"><span><?php echo $_SESSION['LANG']['sign_in']; ?></span></button>
<div class="clear"></div>
</form>

File (picture) Upload not working

Okay so I just basically got this code online.
It works locally (to upload a file), but it doesn't wanna work on the website.
(I've already put the permissions at 0777, for the folder, but it still won't upload.
<header>
<?php
function UploadOne($fname)
{
$uploaddir = 'pictures/';
if (is_uploaded_file($fname['tmp_name']))
{
$filname = basename($fname['name']);
$uploadfile = $uploaddir . basename($fname['name']);
if (move_uploaded_file ($fname['tmp_name'], $uploadfile))
$res = "File " . $filname . " was successfully uploaded and stored.<br>";
else
$res = "Could not move ".$fname['tmp_name']." to ".$uploadfile."<br>";
}
else
$res = "File ".$fname['name']." failed to upload.";
return ($res);
}
?> </header>
<body>
<?php
if ($_FILES['picture']['name'] != "")
{
$res = UploadOne($_FILES['picture']);
$filname = $_FILES['picture']['name'];
echo ($res);
}
?>
<h1>UPLOADING FILES</h1>
<form name="fupload" enctype="multipart/form-data" action="upfiles.php" method="post">
<input type="file" name="picture" />
<input type="submit" value="Submit" />
</form>
</body>

How do i get the actual image to show on my page rather than the text?

<?php
require 'db.php';
include_once("header.php");
include_once("functions.php");
if(isset($_POST['search_term'])){
$search_term = mysql_real_escape_string(htmlentities ($_POST['search_term']));
if(!empty($search_term)){
$search = mysql_query("SELECT users.username, users.id, tbl_image.photo, tbl_image.userid FROM users LEFT OUTER JOIN tbl_image ON users.id=tbl_image.userid WHERE users.username LIKE '%$search_term%' and users.business <> 'business'");
$result_count = mysql_num_rows($search);
$suffix = ($result_count != 1) ? 's' : '';
echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>';
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong>',$results_row['photo'], $results_row['username'], '</strong></div>';
$following = following($_SESSION['userid']);
if (in_array($key,$following)){
echo ' <div action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</div>';
}else{
echo " <div action='action.php' method='GET' data-theme='a'>
<input type='hidden' name='id' value='$key'/>
<input type='submit' name='do' value='follow' data-theme='a'/>
</div>";
}
}
}
}
?>
How do i get the actual image to show on the page rather than the name of the image. I have the images in the file system under a folder called image. How do I echo that image on screen or if echo is not the right way to do it how would you do it?
You mean like this?
echo '<img src="'.$results_row['photo'].'" width="80">';

Resources