-
steno
Newbie
-
Posts: 6
Threads: 2
Joined: Oct 2019
Reputation:
0
How to upload photos and other data in one form? My solution below works, but not quite as I would like. It means uploading photos, but first you need to complete the entire form, select the photos to upload and press upload, which at the same time sends the entire form, if the form does not contain validation errors then the form sends and uploads the photos. I would like it to work, that I press upload, then upload photos, I have their preview and then fill out the form. Is it possible to do such a thing in one form?
Code: <form method='post' action='<?php echo base_url();?>ads/create' enctype='multipart/form-data'>
<div class="form-group row">
<label for="email" class="col-4 col-form-label text-uppercase text-right">Tytuł ogłoszenia:</label>
<div class="col-8">
<input type="text" class="form-control" name="title" value="<?php echo set_value('title'); ?>">
</div>
</div>
<div class="form-group row">
<label for="category_id" class="col-4 col-form-label text-uppercase text-right">Wybierz kategorie:</label>
<div class="col-8">
<select name="category_id" class="form-control" id="category_id">
<?php
function fetch_menu($categories){
foreach($categories as $menu){
echo "<option value='".$menu->id."'>".$menu->name."</option>";
if(!empty($menu->sub)){
echo ">";
fetch_sub_menu($menu->sub);
echo "</ul>";
}
}
}
function fetch_sub_menu($sub_menu){
foreach($sub_menu as $menu){
echo "<option value='".$menu->id."'> >".$menu->name."</option>";
if(!empty($menu->sub)){
echo "<ul>";
fetch_sub_menu($menu->sub);
echo "</ul>";
}
}
}
echo "<table>";
fetch_menu($categories);
echo "</table>";
?>
</select>
</div>
</div>
<div class="form-group row">
<label for="category_id" class="col-4 col-form-label text-uppercase text-right">Wybierz subkategorie:</label>
<div class="col-8">
<select name="subcat" id="subcat" class="form-control"></select>
</div>
</div>
<div class="form-group row">
<label for="city_id" class="col-4 col-form-label text-uppercase text-right">Wybierz miasto:</label>
<div class="col-8">
<select name="city_id" id="city_id" class="form-control">
<option value="">Wybierz miasto...</option>
<?php foreach ($cities as $city)
{
echo "<option value='".$city->id."'>".$city->name."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group row" id="sms" style="display: none">
<label for="sms" class="col-4 col-form-label text-uppercase text-right">Kod sms:</label>
<div class="col-8">
<input type="text" class="form-control" name="sms" value="<?php echo set_value('sms'); ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-4 col-form-label text-uppercase text-right">Cena:</label>
<div class="col-8">
<div class="row">
<div class="col-md-4 d-flex align-items-center">
<div class="form-group row">
<div class="col-8">
<input type="text" class="form-control" name="price" value="<?php echo set_value('price'); ?>">
</div>
<label for="email" class="col-4 col-form-label text-uppercase text-left">Zł</label>
</div>
</div>
<div class="col-md-4 mt-1">
<div class="form-check">
<label class="wl-label">
<input name="price" class="label__checkbox" type="checkbox" value="1" />
<span class="label__text">
<span class="label__check">
<i class="fa fa-check icon"></i>
</span>
</span>
</label>
<span class="d-inline-block ml-3">Za darmo</span>
</div>
</div>
<div class="col-md-4 mt-1">
<div class="form-check">
<label class="wl-label">
<input name="price" class="label__checkbox" type="checkbox" value="2" />
<span class="label__text">
<span class="label__check">
<i class="fa fa-check icon"></i>
</span>
</span>
</label>
<span class="d-inline-block ml-3">Wymienię</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row">
<label for="message" class="col-4 col-form-label text-uppercase text-right">Opis ogłoszenia:</label>
<div class="col-8">
<textarea name="description" id="message" cols="70" rows="5"
class="form-control" ><?php echo set_value('description'); ?></textarea>
</div>
</div>
<div class="form-group row">
<label for="contact" class="col-4 col-form-label text-uppercase text-right">Osoba do kontaktu:</label>
<div class="col-8">
<input type="text" class="form-control" name="contact" value="<?php echo set_value('contact'); ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-4 col-form-label text-uppercase text-right">Adres email:</label>
<div class="col-8">
<input type="text" class="form-control" name="email" <?php if ( logged_in() == true )
{ echo "value='".$user->email."' "; } ?> >
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-4 col-form-label text-uppercase text-right">Numer telefonu:</label>
<div class="col-8">
<input type="text" class="form-control" name="phone" value="<?php echo set_value('phone'); ?>">
</div>
</div>
<div class="form-group row">
<label for="message" class="col-4 col-form-label text-uppercase text-right">Zdjęcia:</label>
</div>
<hr>
<input type='file' name='files[]' multiple="multiple"> <br/><br/>
<input id="submit" type='submit' value='Upload' name='upload' />
<div class="form-group row">
<div class="col-12 text-center">
<button class="btn btn-primary w-50 mt-3" type="submit">Dodaj ogłoszenie</button>
</div>
</div>
<?php echo form_close(); ?>
and my create function:
Code: public function create()
{
$user_id = $this->session->userdata( 'id' );
$where = array( 'id' => $user_id);
$user = $this->Site_model->get_single('users', $where);
$data['user'] = $user;
if ( !empty( $_POST ) )
{
if ( $this->form_validation->run( 'site_ads_create' ) == true )
{
$activate_code = random_string();
if ( logged_in() == true )
{
$data = array(
'title' => $this->input->post( 'title' , true ),
'description' => $this->input->post( 'description' , true ),
'category_id' => $this->input->post( 'subcat' , true ),
'city_id' => $this->input->post( 'city_id' , true ),
'price' => $this->input->post( 'price' , true ),
'contact' => $this->input->post( 'contact' , true ),
'email' => $this->session->userdata( 'email' ),
'phone' => $this->input->post( 'phone' , true ),
'user_ip' => getUserIpAddr(),
'created' => time(),
'active' => 1,
);
}
else
{
$data = array(
'title' => $this->input->post( 'title' , true ),
'description' => $this->input->post( 'description' , true ),
'category_id' => $this->input->post( 'subcat' , true ),
'city_id' => $this->input->post( 'city_id' , true ),
'price' => $this->input->post( 'price' , true ),
'contact' => $this->input->post( 'contact' , true ),
'email' => $this->input->post( 'email' , true ),
'phone' => $this->input->post( 'phone' , true ),
'user_ip' => getUserIpAddr(),
'created' => time(),
'active' => 0,
'activate_code' => $activate_code,
);
}
$count = count($_FILES['files']['name']);
for($i=0;$i<$count;$i++){
if(!empty($_FILES['files']['name'][$i])){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
$path = BASEPATH . '../images/'. date('y').'_'.date('m').'/';
if ( !file_exists( $path ) )
{
mkdir( $path , 0777 );
}
$config['upload_path'] = 'images/'. date('y').'_'.date('m').'/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5000';
$config['file_name'] = $_FILES['file']['name'];
$this->load->library('upload',$config);
if($this->upload->do_upload('file')){
$uploadData[] = $this->upload->data();
}
}
-
InsiteFX
Super Moderator
-
Posts: 6,301
Threads: 288
Joined: Oct 2014
Reputation:
234
-
steno
Newbie
-
Posts: 6
Threads: 2
Joined: Oct 2019
Reputation:
0
Thanks, it works, but I don't know how to transfer the uploaded data to the create function and save the photos to the database? I tried to pass data as
Code: $this->create($uploadedphotos)
and receive public function create ($uploadedphotos) but when the main form is sent, the $ uploadedphotos variable is as if empty. How to save uploaded files to database?
-
steno
Newbie
-
Posts: 6
Threads: 2
Joined: Oct 2019
Reputation:
0
I thought it was obvious ... I upload the files to the server, but I want to save them (file names) to the database and I don't know how to pass it from upload function to create function, where is the main form.
-
InsiteFX
Super Moderator
-
Posts: 6,301
Threads: 288
Joined: Oct 2014
Reputation:
234
-
steno
Newbie
-
Posts: 6
Threads: 2
Joined: Oct 2019
Reputation:
0
The point is that the main form adds an advertisement to the database and then its id is created. The problem is that I would like to add photos to the id that is not yet available at the time of uploading the image, but only created when the main form is sent. How to do it technically? Any advice?
-
InsiteFX
Super Moderator
-
Posts: 6,301
Threads: 288
Joined: Oct 2014
Reputation:
234
You could create a place holder id which you would know the value of it then look up that id
and insert your image etc;
Auto increment the id each time you use it.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|