Images not saving to temporary folder but saved to Database - php

I have the following code with the aim of saving images/upload to a temporary folder in my project root directory. It is expected that the path of the temporary folder be saved to database and the image saved to the temporary folder
I discover the images are not saving to temporary folder rather save directly to the database.
I am using CodeIgniter
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ArticleController extends CI_Controller {
public function __construct(){
parent::__construct();
if (!$this->session->userdata('login_id'))
return redirect("LoginController");
$this->load->model('ArticleModel');
$this->load->model('CategoryModel');
$this->load->model('UserModel');
$data = array();
}
public function addarticle(){
$data['title'] = 'eBlog Admin Portal - Add Article';
$data['header'] = $this->load->view('include/header', $data, TRUE);
$data['sidebar'] = $this->load->view('include/sidebar', '', TRUE);
$data['categoryData'] = $this->CategoryModel->getCategory();
$data['userData'] = $this->UserModel->getuser();
$data['articleadd'] = $this->load->view('include/articleadd', $data, TRUE);
$data['footer'] = $this->load->view('include/footer', '', TRUE);
$this->load->view('addarticle', $data);
}
public function addArticleForm(){
//check whether user upload picture
if(!empty($_FILES['image_path']['upload_path'])){
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|';
$config['file_name'] = $_FILES['image_path']['upload_path'];
//load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('image_path')){
$uploadData = $this->upload->data();
$image_path = $uploadData['file_name'];
}else{
$image_path = '';
}
}
$data['title'] = $this->input->post('title');
$data['content'] = $this->input->post('content');
$data['category_id'] = $this->input->post('category_id');
$data['written'] = $this->input->post('written');
$data['featured'] = $this->input->post('featured');
$data['image_path'] = $this->input->post('image_path');
$title = $data['title'];
$content = $data['content'];
$category_id = $data['category_id'];
$written = $data['written'];
$title = $data['featured'];
$image_path = $data['image_path'];
if (empty($title) && empty($content) && empty($category_id) && empty($written) && empty($featured) && empty($title)) {
$sdata = array();
$sdata['msg'] = '<span style="color:red">Filed must not be empty</span>';
$this->session->set_flashdata($sdata);
redirect("ArticleController/addarticle");
}else {
$this->ArticleModel->saveArticle($data);
$sdata = array();
$sdata['msg'] = '<span style="color:green">Article added successfully!</span>';
$this->session->set_flashdata($sdata);
redirect("ArticleController/addarticle");
}
}
}
Model
<?php
class ArticleModel extends CI_Model{
public function __construct(){
parent::__construct();
}
public function saveArticle($data){
$this->db->insert('articles', $data);
}
}
View
<h2>Add Article </h2>
<hr/>
<?php
$msg = $this->session->flashdata('msg');
if(isset($msg)){
echo $msg;
}
?>
<div class="panel-body" style="width:600px;">
<form action="<?php echo base_url(); ?>ArticleController/addArticleForm" method="post" 'enctype' = 'multipart/form-data'>
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control span12" placeholder="Article Title" required="1">
</div>
<div class="form-group">
<label>Category</label>
<select name="category_id" class="form-control span12" required="1">
<option value="">Select Article Category</option>
<?php
foreach ($categoryData as $sdata) {
?>
<option value="<?php echo $sdata->id ; ?>"><?php echo $sdata->name; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label>Written By</label>
<select name="written" class="form-control span12" required="1">
<option value="">Select Article Writer</option>
<?php
foreach ($userData as $udata) {
?>
<option value="<?php echo $udata->login_id; ?>"><?php echo $udata-> fullname; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label>Featured</label>
<select name="featured" class="form-control span12" required="1">
<option value="">Select Article featured</option>
<?php
for($i = 0; $i <= 8; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label>Image</label>
<input type="file"name="image_path" class="form-control span12"placeholder="Chose Image" required="1">
</div>
<div class="form-group">
<label>Content</label>
<input type="text"name="content" class="form-control span12"placeholder="Article content" required="1">
</div>
<div class="form-group">
<input type="submit"class="btn btn-primary" value="Save">
</div>
</form>
</div>

Related

File (image) Upload issue in codeigniter

Actually, I have saved all the data in database after i have show in front end,in my side issue is i have created upload image function to save database after i fetch and display the front end,upload function is taking to save full path like :C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg. all the images saved folder also but in front end is coming like this only : C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg please i need save database like this : (images/media/rajkumar-1499778784/19510.jpg) please help me how will resolve this this is my first sit is codeigniter please help how will pass like this url.
Here my code for controller:
function addNewMedia()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name','Name','trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('event_image','Pg Image');
// $this->form_validation->set_rules('media_image','Image');
// $this->form_validation->set_rules('date_added','Date','trim|required');
if($this->form_validation->run() == FALSE)
{
$this->addNew();
}
else
{
$data = array(); $upload_data = array();
$this->load->library('upload');
$data['name'] = $this->input->post('name');
$folder_srting = $data['name']."-".time();
$data['name'] = $this->input->post('name');
// print_r($folder_srting);
$folder_string = str_replace(' ', '-', $folder_srting);// Replaces all spaces with hyphens.
$folder_string = preg_replace('/[^A-Za-z0-9\-]/', '', $folder_srting);// Removes special chars.
$folder_name = preg_replace('/-+/', '-', strtolower($folder_string));// Replaces multiple hyphens with single one.
print_r($folder_name);
//$data['name'] = $this->input->post('name');
//$pg_id = $this->input->post('pg_id');
if ($_FILES['event_image']['error'] != 4)
{
$folder = $this->checkdirectory($folder_name);
//print_r($folder_name);
$this->upload->initialize($this->set_upload_options($folder));
if ( ! $this->upload->do_upload('event_image'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error); die;
}
else
{
$upload_data['banner_data'] = $this->upload->data();
//print_r($upload_data['banner_data']);die;
$upload_data['bannerfilepath'] = $upload_data['banner_data']['full_path'];
//print_r($upload_data['bannerfilepath']);die;
}
foreach($upload_data['banner_data'] as $bannerfilepath){
$data['banner_image_path'] = str_ireplace(FCPATH,"", $upload_data['banner_data']['full_path']);
//print_r($data['banner_image_path']);die;
}
$event_image = $data['banner_image_path'];
//print_r($event_image);die;
}
// $name = ucwords(strtolower($this->input->post('name')));
$event_image = $event_image;
//print_r($event_image);die;
$name = $this->input->post('name');
$address = $this->input->post('pg_address');
$incharge_name = $this->input->post('pg_incharge_name');
$incharge_mobile = $this->input->post('pg_incharge_mobile');
$email = $this->input->post('pg_email');
$mediaInfo = array('name'=>$name,'event_image'=>$event_image,'pg_address'=>$address,'pg_incharge_name'=>$incharge_name,'pg_incharge_mobile'=> $incharge_mobile,'pg_email'=>$email,'folder_name'=>$folder);
//echo "<pre>";print_r($mediaInfo);die;
$this->load->model('media_model');
//echo "<pre>";print_r($mediaInfo);die;
$result = $this->media_model->addNewMedia($mediaInfo);
if($result > 0)
{
$this->session->set_flashdata('success', 'New Pg created successfully');
}
else
{
$this->session->set_flashdata('error', 'Pg creation failed');
}
redirect('mediaListing');
}
}
}
function editMedia()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->load->library('form_validation');
$eventId = $this->input->post('pg_id');
$this->form_validation->set_rules('name','Name','trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('event_image','Pg Image');
//$this->form_validation->set_rules('event_description','Event Description','required|max_length[200]');
// $this->form_validation->set_rules('start_date','Start Date','trim|required');
//$this->form_validation->set_rules('end_date','End Date','trim|required');
//$this->form_validation->set_rules('additional_images','Additional Images');
//$this->form_validation->set_rules('short_description','Short Description','required');
if($this->form_validation->run() == FALSE)
{
$this->editNew($eventId);
}
else
{
$data = array(); $upload_data = array();
$this->load->library('upload');
$existing_folder = $_POST['folder_name'];
//print_r($existing_folder);die;
if(isset($_POST['image_exists']) && $_POST['image_exists']!= '')
$temp_attachment = $_POST['image_exists'];
$folder = $this->checkdirectory($existing_folder);
if (isset($_FILES['event_image']['name']) && $_FILES['event_image']['error'][0] != 4 && $_FILES['event_image']['name']!='') {
$this->upload->initialize($this->set_upload_options($folder));
if ( ! $this->upload->do_upload('event_image'))
{
$error = array('error' => $this->upload->display_errors());
//print_r($error); die;
}
else
{
$upload_data['banner_data'] = $this->upload->data();
$upload_data['bannerfilepath'] = $upload_data['banner_data']['full_path'];
}
// GET REQUIRED BANNER IMAGES FILE PATH FROM FULL PATH
foreach($upload_data['banner_data'] as $bannerfilepath){
$data['banner_image_path'] = str_ireplace(FCPATH,"", $upload_data['banner_data']['full_path']);
print_r($data['banner_image_path']);die;
}
$event_image = $data['banner_image_path'];
//print_r($event_image);die;
}
else{
// echo "sfgjdf";
$event_image = $temp_attachment;
// print_r($event_image);die;
}
$event_image = $event_image;
$name = $this->input->post('name');
$pg_address = $this->input->post('pg_address');
$pg_incharge_name = $this->input->post('pg_incharge_name');
$pg_incharge_mobile = $this->input->post('pg_incharge_mobile');
$pg_email = $this->input->post('pg_email');
// $additional_images = $additional_images;
$mediaInfo = array('name'=>$name,'event_image'=>$event_image,'pg_address'=>$pg_address,'pg_incharge_name'=>$pg_incharge_name,'pg_incharge_mobile'=>$pg_incharge_mobile,'pg_email'=>$pg_email,'folder_name'=>$folder);
//echo "<pre>";print_r($mediaInfo);die;
$result = $this->media_model->editMedia($mediaInfo, $eventId);
if($result == true)
{
$this->session->set_flashdata('success', 'Pg updated successfully');
}
else
{
$this->session->set_flashdata('error', 'Pg updation failed');
}
redirect('mediaListing');
}
}
}
here my model:
function addNewMedia($mediaInfo)
{
$this->db->trans_start();
$this->db->insert('tbl_master_property', $mediaInfo);
$insert_id = $this->db->insert_id();
$this->db->trans_complete();
return $insert_id;
}
function getMediaInfo($eventId)
{
$this->db->select('pg_id, name,event_image,pg_address,pg_incharge_name,pg_incharge_mobile,pg_email,folder_name');
$this->db->from('tbl_master_property');
$this->db->where('status', 0);
$this->db->where('pg_id', $eventId);
$query = $this->db->get();
return $query->result();
}
function editMedia($mediaInfo, $eventId)
{
$this->db->where('pg_id', $eventId);
$this->db->update('tbl_master_property', $mediaInfo);
return TRUE;
}
here my view file code:
<?php
define("IMAGE_PATH", "http://localhost/rentozy/admin/");
$eventId = '';
$name = '';
$pg_address = '';
$pg_incharge_name = '';
$pg_incharge_mobile = '';
$pg_email ='';
$event_image = '';
$folder_name = '';
if(!empty($mediaInfo))
{
foreach ($mediaInfo as $ef)
{
$eventId = $ef->pg_id;
$name = $ef->name;
$pg_address = $ef->pg_address;
$pg_incharge_name = $ef->pg_incharge_name;
$pg_incharge_mobile = $ef->pg_incharge_mobile;
$pg_email = $ef->pg_email;
$event_image = $ef->event_image;
$folder_name = $ef->folder_name;
}
}
?>
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
//<![CDATA[
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
//]]>
</script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
<i class="fa fa-users"></i> Property Management
<small>Add / Edit Property</small>
</h1>
</section>
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Enter Property Details</h3>
</div><!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url() ?>editMedia" method="post" id="editEvent" role="form" enctype="multipart/form-data" files="true">
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="event_name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name" name="name" value="<?php echo $name; ?>" maxlength="128" readonly>
<input type="hidden" value="<?php echo $eventId; ?>" name="pg_id" id="eventId" />
<input type="hidden" value="<?php echo $folder_name; ?>" name="folder_name"/>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6" style="padding-bottom:15px;">
<div class="form-group">
<label for="description" class="pull-left">Pg Address</label>
<textarea rows="6" cols="50" name="pg_address" class="pull-left" style="width:100%;" value="<?php echo $pg_address;?>" id="pgaddress"><?php echo $pg_address;?></textarea>
</div>
</div>
<div class="col-md-6" style="padding-bottom:15px;">
<div class="form-group">
<label for="description" class="pull-left">Pg Incharge Name</label>
<div class="clearfix"></div>
<textarea rows="6" cols="50" name="pg_incharge_name" class="pull-left" style="width:100%;" value="<?php echo $pg_incharge_name;?>" id="pg_incharge_name" ><?php echo $pg_incharge_name;?></textarea>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="start-date">Pg Incharge Mobile</label>
<input type="text" class="form-control required pg_incharge_mobile" value="<?php echo $pg_incharge_mobile;?>" id="pg_incharge_mobile" name="pg_incharge_mobile">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="end-date">Pg Email</label>
<input type="text" class="form-control pg_email" value="<?php echo $pg_email;?>" id="pg_email" name="pg_email">
</div>
<div class="col-md-6">
<div class="col-md-6">
<div class="form-group">
<label for="event_image">Pg Image</label>
<input type="file" value="<?php echo $event_image; ?>" class="form-control file_change1" id="eventimage" name="event_image">
<img src="<?php echo IMAGE_PATH.$event_image;?>" width="100px" height="50px">
<input type="hidden" name="image_exists" value="<?php echo $event_image;?>" class="form-control" id="eventimage" placeholder="Enter Image Text" aria-describedby="fileHelp">
<div><?php echo $event_image;?></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I don't know if you've thought about it, but you could easily use PHP's str_replace to do what you need.
<?php
$path = $upload_data['banner_data']['full_path'];
$dir = 'C:/xampp/www/htdocs/rentozy/admin/';
$url = str_replace( $dir, '', $path );
echo $url;
In CodeIgniter, if you are saving to a path that is a directory off of document root, you can use the FCPATH constant to make this easy. So if your path to your upload folder is in a directory named /uploads/, and /uploads/ is at document root, then:
<?php
$path = $upload_data['banner_data']['full_path'];
$dir = FCPATH . 'uploads/';
$url = str_replace( $dir, '', $path );
echo $url;
This is just an example, but it is easy

How to avoid duplication when submitting data to json file via php?

I have a code that submits data into a cake json 'database', but when I submit using PHP. When I reload the page, the file repeats the code of the last object in the JSON file when I get it. How do I avoid this?
This is my PHP
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error = "<label class='text-danger'>Enter Name</label>";
}
else if(empty($_POST["type"]))
{
$error = "<label class='text-danger'>Enter Type</label>";
}
else if(empty($_POST["diff"]))
{
$error = "<label class='text-danger'>Enter Difficulty</label>";
}
else
{
if(file_exists('../../databases/cakes.json'))
{
$current_data = file_get_contents('../../databases/cakes.json');
$array_data = json_decode($current_data, true);
$extra = array(
'person' => array(
'name' => $_POST['name'],
'difficulty' => $_POST["diff"],
'type' => $_POST["type"],
'isNew' => 'true',
'isVeg' => 'false',
)
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('../../databases/cakes.json', $final_data))
{
//.-.
}
}
else
{
$error = 'JSON File not exits';
}
}
}
?>
<body>
<div id="layout"></div>
<div id="content">
<div id="add">
<div class="form-title"><h1>Add Cake</h1></div>
<form method="post">
<?php
if(isset($error))
{
echo $error;
}
?>
<br />
<div class="input-field">
<label for="name">Cake Name</label>
<input type="text" name="name"/>
</div>
<br />
<div class="input-field">
<label for="diff">Difficulty</label>
<div class="select">
<select name="diff" id="slct">
<option>Choose an option</option>
<option value="male">EZ</option>
<option value="female">Meh</option>
<option value="matthew">Mildy Hard</option>
</select>
</div>
</div>
<br />
<div class="input-field">
<label for="type">Type</label>
<input type="text" name="type"/><br />
Need Suggestions?<br>
</div>
<input class="addCake" type="submit" name="submit" value="Add Cake!"/><br />
See some other cakes
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
Submission Works.
Result:
[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]
But when I reload the page I see two of this things...
Result:
[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"},{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]
Use ($_SERVER['REQUEST_METHOD'] == 'POST') instead of ($_POST["submit"])
$array_data = array_merge($array_data, $extra); instead of $array_data[] = $extra;

Problems on uploading image files

This is my view file where form for image and other data exists:
<?php echo form_open_multipart('Login/client_profile'); ?>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control" name="company_name" >
</div>
<div class="form-group">
<label>Upload Profile Picture</label>
<input type="file" name="profile_pic" accept="image/*" class="form-control" required>
</div>
<div class="form-group">
<label>Mobile Number</label>
<input type="number" class="form-control" name="mobile" required>
</div>
<div class="form-group">
<label>Specialist in</label>
<input type="text" class="form-control" name="specialist_in" >
</div>
<div class="form-group">
<label>Position</label>
<input type="text" class="form-control" name="position" >
</div>
<?php
$data7 = array(
'type' => 'submit',
'value' => 'Update',
'class' => 'btn btn-primary ',
);
echo form_submit($data7);
echo form_close();
?>
This is the controller file Client.php
public function client_profile()
{
$client=$this->input->post();
$client['profile_pic']=$this->input->post('profile_pic');
$this->load->model('Clientmodel');
$email=$this->session->userdata('email_id');
$this->Clientmodel->add_client_details($email,$client);
$ppic['pic']=$this->Clientmodel->get_pic($email);
$config['upload_path'] = './profile/';
$config['allowed_types'] = 'jpg|jif|png|jpeg';
$this->load->library('upload', $config);
$field = 'pic';
if ($this->upload->do_upload($field)) {
$temp = $this->upload->data();
$pic = $temp['file_name'];
}
$this->load->view('client/pro_header',$ppic);
$this->load->view('client/client_dashboard',$client);
}
This is model file Clientmodel.php
public function add_client_details($email, Array $client)
{
return $this->db->where(['email'=>$email])
->update('clients',$client);
}
public function get_pic($login_email)
{
$q=$this->db->where(['email'=>$login_email])
->get('clients');
return $q->row()->profile_pic;
}
After entering all the data all the fields other than image can be fetched using $this->input->post when i try to fetch 'profile_pic' it returns nothing.And the image file name is also not inserted in database.Field 'profile_pic' is there in table 'clients'
This is the for uploading it's not checking any validation
public function upload_docs () {
if($this->input->post('action') == 'Upload') {
$company_name = $input->post('company_name');
$position = $input->post('position');
$mobile = $input->post('mobile');
$specialist_in = $input->post('specialist_in');
// capture all your variable like this
$file_path = './assets/images/uploads';
if ($_FILES["profile_pic"]["error"] > 0) {
$data['msg'] = 'your message';
} else {
if(!is_dir($file_path)) #mkdir($file_path, 0777, true);
if (move_uploaded_file($_FILES['profile_pic']['tmp_name'], $file_path.'/'.$_FILES['profile_pic']['name'])) {
$upload_data = array('company_name'=> $company_name,'mobile'=> $mobile,'specialist_in'=> $specialist_in,'profile_pic' => $_FILES['profile_pic']['name']);
$insert_id = $this->Your_model->addRecord($upload_data);
if ($insert_id) {
// redirect('admin/index','refresh');
}
}
}
}
$data['title'] = 'upload';
$this->load->view('admin/upload',$data);
}

PHP form not confirming errors on the form and not allowing some fields to be optional

I'm experiencing some issues with a php form i have inherited as part of a project.
The form has to be completed in its entirety to successfully submit, which it shouldn't do as there are optional fields here is no message to advise of the errors.
Any help would be most appreciated. Thanks.
Here's the code snippet.
function validateForm()
{
global $error, $data, $errorBox, $successBox, $formShown;
$formShown = true;
$error = array();
$data = array();
$count = 0;
$data = $_POST;
$blocked = array('booooo');
foreach($_POST as $key=>$value)
{
for($i=0;$i<count($blocked);$i++)
{
if(strlen(strstr($value,$blocked[$i]))>0)
{
$error[$key] = ' class="error"';
$count++;
}
}
if($value == "" || (strlen(trim($value)) == 0))
{
if($key == "submit"){
}else{
$error[$key] = ' class="error"';
$count++;
}
}
if($key == "phone" && $value<20)
{
$error[$key] = ' class="error"';
$count++;
}
}
// function to confirm form submission
if($count>0)
{
$echo "Please enter all the areas highlighted in red"
}
else
{
$this->sendForm();
$successBox = '<div class="successBox"><h3>Your e-mail has been sent.</h3></div>';
}
//Get the uploaded file information
$name_of_uploaded_file =
basename($_FILES['logo']['name']);
//get the file extension of the file
$type_of_uploaded_file =
substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file =
$_FILES["logo"]["size"]/1024;//size in KBs
//attachment settings
$max_allowed_file_size = 1024; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Size of file should be less than $max_allowed_file_size";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}
//copy the temporary uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; //NEED TO CREATE A FOLDER WITH 777 PERMISSIONS TO DROP THE FILE IN
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}
}
function sendForm()
{
global $data, $formShown;
require_once('email.class.php');
date_default_timezone_set('Europe/London');
$data['timeSent'] = date('l, jS F, Y \a\t H:i',time());
// Send email with enquiry details
$contactemail = new Email();
$contactemail->setTemplate("quickcontact");
$contactemail->setTo('jonathan#email.com'); // or mike#email.com?
$contactemail->setSubject("My enquiry");
//$contactemail->setFrom($data['name'],$data['email']);
$contactemail->setFrom("My Enquiry", "jonathan#email.com"); // double check
$contactemail->sendEmail($data);
//attach the file to the email
//$contactemail->addAttachment($path_of_uploaded_file);
// Send email to the visitor to confirm the contact form they sent
$contacteeemail = new Email();
$contacteeemail->setTemplate("quickcontactee");
$contacteeemail->setTo($data['email']);
$contacteeemail->setSubject("My");
$contacteeemail->setFrom("My", "jonathan#email.com"); // double check
$contacteeemail->sendEmail($data);
$this->unsetData();
$formShown = false;
}
Here's the HTML containing the optional fields.
<div class="row orange-underline">
<div class="addressArea hidden">
<div class="duplicateFree">
<label>Free Standing Midwifery Unit:</label>
<input <?=$error["freeStandingUnit"]; ?> type="text" name="freeStandingUnit" id="freeStandingUnit" value="<?=$_POST['freeStandingUnit'] ?>"/>
<label>Free Standing Midwifery Address:</label>
<textarea <?=$error["freeStandingAddresses"]; ?> name="freeStandingAddresses" id="freeStandingAddresses" rows="0" cols="0" ><?=$_POST['freeStandingAddresses'] ?></textarea>
<label>Free Standing Midwifery Postcode:</label>
<input <?=$error["freeStandingPost"]; ?> type="text" name="freeStandingPost" id="freeStandingPost" value="<?=$_POST['freeStandingPost'] ?>"/>
<label>Distance To Main Site:</label>
<input <?=$error["freeMainSiteDistance"]; ?> type="text" name="freeMainSiteDistance" id="freeMainSiteDistance" value="<?=$_POST['freeMainSiteDistance'] ?>"/>
<label>Non-urgent Transfer Time:</label>
<input <?=$error["freeNonUrgentTransfer"]; ?> type="text" name="freeNonUrgentTransfer" id="freeNonUrgentTransfer" value="<?=$_POST['freeNonUrgentTransfer'] ?>"/>
<label>Blue Light Transfer Time:</label>
<input <?=$error["freeBlueLightTransfer"]; ?> type="text" name="freeBlueLightTransfer" id="freeBlueLightTransfer" value="<?=$_POST['freeBlueLightTransfer'] ?>"/>
</div>
</div>
</div>
<div class="row">
<span class="leftSpan"> <span><label>Any Alongside Midwifery Units:</label>
<p>
<select class="region_units">
<option value="select">Select</option>
<?php for($i = 1; $i <= 10; $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i . ($i === 1 ? ' unit' : ' units'); ?></option>
<?php } ?>
</select>
</p>
</span> </span>
</div>
<div class="row orange-underline">
<div class="addressArea2 hidden">
<div class="duplicate">
<label>Alongside Midwifery Unit:</label>
<input <?=$error["alongsideUnit"]; ?> type="text" name="alongsideUnit" id="alongsideUnit" value="<?=$_POST['alongsideUnit'] ?>"/>
<label>Alongside Midwifery Address:</label>
<textarea <?=$error["alongsideAddresses"]; ?> name="alongsideAddresses" id="alongsideAddresses" rows="0" cols="0" ><?=$_POST['alongsideAddresses'] ?></textarea>
<label>Alongside Midwifery Postcode:</label>
<input <?=$error["alongsideUnitPost"]; ?> type="text" name="alongsideUnitPost" id="alongsideUnitPost" value="<?=$_POST['alongsideUnitPost'] ?>"/>
<label>Distance To Main Site:</label>
<input <?=$error["alongsideMainSiteDistance"]; ?> type="text" name="alongsideMainSiteDistance" id="alongsideMainSiteDistance" value="<?=$_POST['alongsideMainSiteDistance'] ?>"/>
<label>Non-urgent Transfer Time:</label>
<input <?=$error["alongsideNonUrgentTransfer"]; ?> type="text" name="alongsideNonUrgentTransfer" id="alongsideNonUrgentTransfer" value="<?=$_POST['alongsideNonUrgentTransfer'] ?>"/>
<label>Blue Light Transfer Time:</label>
<input <?=$error["alongsideBlueLightTransfer"]; ?> type="text" name="alongsideBlueLightTransfer" id="alongsideBlueLightTransfer" value="<?=$_POST['alongsideBlueLightTransfer'] ?>"/>
</div>
</div>
</div>
<div class="row">
<span class="leftSpan"> <span><label>Any Extra Addresses:</label>
<p>
<select class="extra_region_units">
<option value="select">Select</option>
<?php for($i = 1; $i <= 10; $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i . ($i === 1 ? ' unit' : ' units'); ?></option>
<?php } ?>
</select>
</p>
</span> </span>
</div>
<div class="row orange-underline">
<div class="addressArea3 hidden">
<div class="duplicateExtra">
<label>Extra Unit:</label>
<input <?=$error["extraUnit"]; ?> type="text" name="extraUnit" id="extraUnit" value="<?=$_POST['extraUnit'] ?>"/>
<label>Extra Unit Address:</label>
<textarea <?=$error["extraAddresses"]; ?> name="extraAddresses" id="extraAddresses" rows="0" cols="0" ><?=$_POST['extraAddresses'] ?></textarea>
<label>Extra Unit Postcode:</label>
<input <?=$error["extraUnitPost"]; ?> type="text" name="extraUnitPost" id="extraUnitPost" value="<?=$_POST['extraUnitPost'] ?>"/>
<label>Distance To Main Site:</label>
<input <?=$error["extraMainSiteDistance"]; ?> type="text" name="extraMainSiteDistance" id="extraMainSiteDistance" value="<?=$_POST['extraMainSiteDistance'] ?>"/>
<label>Non-urgent Transfer Time:</label>
<input <?=$error["extraNonUrgentTransfer"]; ?> type="text" name="extraNonUrgentTransfer" id="extraNonUrgentTransfer" value="<?=$_POST['extraNonUrgentTransfer'] ?>"/>
<label>Blue Light Transfer Time:</label>
<input <?=$error["extraBlueLightTransfer"]; ?> type="text" name="extraBlueLightTransfer" id="extraBlueLightTransfer" value="<?=$_POST['extraBlueLightTransfer'] ?>"/>
</div>
</div>
</div>
You are missing a ' which could cause an issue in your PHP code.
Replace this:
$successBox = <div class="successBox"><h3>E-Mail Sent.</h3></div>';
With this:
$successBox = '<div class="successBox"><h3>E-Mail Sent.</h3></div>';
Error in your code
$this->sendForm();
$successBox = <div class="successBox"><h3>E-Mail Sent.</h3></div>';
Single quote is missing it should be like this
$this->sendForm();
$successBox = '<div class="successBox"><h3>E-Mail Sent.</h3></div>';

Undefined property: stdClass codeigniter

Undefined property: stdClass alamat
in my view where I take ,
$this->data['prodi'] = $this->mprodi->get_all_prodi();
In my controller view I am getting this error. But when I am not take my select prodi is not shown.
This is my view :
<div class="field">
<label>Piih Prodi</label>
<input type="hidden" name="nama_prodi" value="<?php echo $data->nama_prodi;?>"><?php echo form_error('nama_prodi', '<div class="ui red pointing label">', '</div>'); ?>
<select name="prodi" id="select_prodi">
<?php foreach ($prodi as $prodi_data) :?>
<?php echo '<option value="'.$prodi_data->id_prodi.'">'.$prodi_data->nama_prodi.'</option>'; ?>
<?php endforeach; ?>
</select>
</div>
<div class="field">
<label>Fakultas</label>
<div class="ui small left icon input">
<input type="text" disabled="disabled" id="fakultas" class="fakultas" placeholder="fakultas" name="nama_fakultas">
<i class="text file outline icon"></i>
</div>
</div>
<div class="field">
<label>Alamat</label>
<textarea placeholder="text" name="alamat">
<?php echo $data->alamat;?>
</textarea><?php echo form_error('alamat', '<div class="ui red pointing label">', '</div>'); ?>
</div>
Controller :
function edit_anggota($nim='')
{
$this->form_validation->set_rules('nim', 'nim', 'required');
$this->form_validation->set_rules('email', 'email', 'required'););
$this->form_validation->set_rules('geup', 'Geup', 'required');
//$nim = $this->uri->segment(4);
$nim = $this->input->post('old_nim');
$this->data['prodi'] = $this->mprodi->get_all_prodi();
if (!empty($_POST))
{
$data['nim'] = $this->input->post('nim');
$data['email'] = $this->input->post('email');
$data['no_telp'] = $this->input->post('no_telp');
$data['tempat_lahir'] = $this->input->post('tempat_lahir');
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir');
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir');
$data['jenis_kelamin'] = $this->input->post('jenis_kelamin');
$data['angkatan'] = $this->input->post('angkatan');
$data['agama'] = $this->input->post('agama');
$data['id_prodi'] = $this->input->post('id_prodi');
$data['alamat'] = $this->input->post('alamat');
$data['sabuk'] = $this->input->post('sabuk');
$data['geup'] = $this->input->post('geup');
if ($this->form_validation->run() !== FALSE)
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
if ($_FILES['userfile']['size']==0)
{
// echo "<pre>";
// print_r($_POST);
// print_r($_FILES);
// print_r($this->upload->display_errors());
// exit;
$this->mdaftar->update_daftar($nim,$data);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
redirect(site_url('anggota/profil/edit_anggota'));
}
else
{
$this->data['error']= $this->upload->display_errors();
$this->data['id_user'] = $this->session->userdata('user_id');
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['data'] = $this->mdaftar->get_daftar_detail($this->data['id_user']);
$this->data['contents'] = $this->load->view('anggota/edit_profil', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_sign',$this->data);
}
}
else
{
$datafoto=$this->upload->data();
$nm_file =$datafoto['file_name'];
$data['image']= $nm_file;
$this->mdaftar->update_daftar($nim,$data);
// print_r($this->mdaftar->update_daftar($nim,$data));
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
redirect(site_url('anggota/profil/edit_anggota'));
}
}
else
{
$this->data['id_user'] = $this->session->userdata('user_id');
$this->data['detail'] = $this->mdaftar->get_daftar_detail($this->data['id_user']);
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('anggota/edit_profil', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_sign',$this->data);
}
}
else
{
$this->data['id_user'] = $this->session->userdata('user_id');
$this->data['data'] = $this->mdaftar->get_daftar_detail($this->data['id_user']);
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('anggota/edit_profil', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_sign',$this->data);
}
}
modal
function get_daftar_detail($id_user)//$limit = 10, $offset = 0)
{
//$this->db->order_by('id','asc');
//$this->db->limit($limit, $offset);
$this->db->select('*');
$this->db->join('prodi','prodi.id_prodi = mahasiswa.id_prodi','left');
$this->db->join('pendaftaran_anggota','pendaftaran_anggota.nim = mahasiswa.nim','left');
$this->db->join('anggota','anggota.id_anggota = pendaftaran_anggota.id_anggota','left');
$this->db->join('user','user.id_user = anggota.id_user','left');
$this->db->where('user.id_user',$id_user);
//$this->db->join('prodi','prodi.id_prodi = mahasiswa.id_prodi','join');
$data = $this->db->get('mahasiswa');
return $data->row();
}
function get_all_prodi()
{
$this->db->order_by('id_prodi','desc');
$data = $this->db->get($this->tbl_prodi);
return $data->result();
}
Please tell me what to do.
Thank you
It just because you have $data variable.
$this->data['data'] = $this->mdaftar->get_daftar_detail($this->data['id_user']);
And you use $data - foreach.
<?php foreach ($prodi as $data) :?>
You need change it.
<?php foreach ($prodi as $prodi_data) :?>
<div class="field">
<label>Piih Prodi</label>
<input type="hidden" name="nama_prodi" value="
<?php echo $data->nama_prodi;?>">
<?php echo form_error('nama_prodi', '
<div class="ui red pointing label">', '</div>'); ?>
<select name="prodi" id="select_prodi">
<?php
foreach ($prodi as $prodi_data)
{
echo '<option value="'.$prodi_data->id_prodi.'">'.$prodi_data->nama_prodi.'</option>';
}
?>
</select>
</div>
<div class="field">
<label>Fakultas</label>
<div class="ui small left icon input">
<input type="text" disabled="disabled" id="fakultas" class="fakultas" placeholder="fakultas" name="nama_fakultas">
<i class="text file outline icon"></i>
</div>
</div>
<div class="field">
<label>Alamat</label>
<?php
// We need to check $data.
// Please comment what the result. Paste in comment.
var_dump($data);
?>
<textarea placeholder="text" name="alamat">
<?php echo $data->alamat;?>
</textarea>
<?php echo form_error('alamat', '<div class="ui red pointing label">', '</div>'); ?>
</div>
This is because you have end your foreach loop before print that variable
try this one
<label>Piih Prodi</label>
<div>
<input type="hidden" name="nama_prodi" value="<?php echo $data->nama_prodi;?>"><?php echo form_error('nama_prodi', '<div class="ui red pointing label">', '</div>'); ?>
<select name="prodi" id="select_prodi">
<?php foreach ($prodi as $data) :?>
<?php echo '<option value="'.$data->id_prodi.'">'.$data->nama_prodi.'</option>'; ?>
// Other code ..
<div class="field">
<label>Alamat</label>
<textarea placeholder="text" name="alamat">
<?php echo $data->alamat;?>
<?php endforeach; ?>
</textarea><?php echo form_error('alamat', '<div class="ui red pointing label">', '</div>'); ?>
</div>

Categories