Welcome Guest, Not a member yet? Register   Sign In
How to avoid $_POST resending data?
#1

[eluser]webbymonk[/eluser]
After being web programmer for sometime found some problem on inserting data to database especially using $_POST...if we accidentally pressing F5 data will be resend...

I found a tutorial here http://www.ajaxray.com/blog/2008/01/12/h...d-warning/

But I am not sure it is the only way by storing $_POST data into session...

Any other solutions?
#2

[eluser]LuckyFella73[/eluser]
In case you want to show the user some details he just submitted
in a form, using sessions (native or CI-session) would be nessesary
or recommended.

You could add the db insert-id info as an url segment and start a query at
the redirect-page but that would open your database as you could
just edit the url segment containing the id and get infos about all your
other db entries!

You also could build the redirect url depending on the submitted data
but that could become "fiddly" (don't know if you can use this
word - I just googled to find a similar world I would use in my
language). If it's just a small set of data it would be ok this way.

But I would prefer using sessions.
#3

[eluser]webbymonk[/eluser]
I have implement session already in the situation...

Here is my steps
1. The Post data I copied it into session
2. Displaying the session data into a redirected page
3. Insert session data into DB
4. Unset/Clear the session...


btw I google for fiddly -> requiring close attention to detail : fussy ; especially : requiring an annoying amount of close attention

Smile)
#4

[eluser]Michael Wales[/eluser]
Eliminating refresh reposting is very easy in CodeIgniter:

Code:
if ($this->validation->run()) {
  // Our code validatates
  // Add to database
  redirect('somewhere');
  return;
}

$this->load->view('my_form');

Since you are redirecting after the submission of data, it's impossible for the refresh to resubmit.
#5

[eluser]webbymonk[/eluser]
Similiar to what i did...

but my case is for temporary data,,,
After the user confirm, the data will be sent to database,,,so I will have to use session...

I think redirecting is good especially for upload file case.

But I don't know how to write the upload data into session because i have multiple upload field

$this->upload->data() is multi dimension array...

Here is my code i wrote in the helper

Code:
<?php



function generate_code($length = 10){

    
    if ($length <= 0)
    {
           return false;
    }
            
    $code = "";
    $chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
    srand((double)microtime() * 1000000);
    for ($i = 0; $i < $length; $i++)
    {
        $code = $code . substr($chars, rand() % strlen($chars), 1);
    }
    return $code;

}


function generatePhotoThumbnail($source, $newimage)
{
    $CI = & get_instance();
    
            $config['image_library'] = 'GD2';
            $config['source_image'] = $source;
            $config['create_thumb'] = TRUE;
            $config['thumb_marker'] = '_thumb';
            $config['master_dim'] = 'width';
            $config['quality'] = 75;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 175;
            $config['height'] = 175;
            $config['new_image'] = './assets/public/placesPhotos/thumb/'.$newimage;

            //$this->image_lib->clear();
            $CI->image_lib->initialize($config);
            //$this->load->library('image_lib', $config);
            $CI->image_lib->resize();
            
            
            
            rename($source,'./assets/public/placesPhotos/'.$newimage);
            
}


function processPhoto($uploaddata, $reviewID)
{
        $CI = & get_instance();
           $gmt=false;
        $time = ($gmt)  ? gmstrftime("%Y-%m-%d %H:%M:%S", time()) : strftime("%Y-%m-%d %H:%M:%S", time());
        //Move Files To User Folder
        foreach($uploaddata as $key => $value)
        {
            //Gen Random code for new file name
            $randomcode = generate_code(12);
            
            $newimagename = $randomcode.$value['file_ext'];
            
        
            generatePhotoThumbnail($value['full_path'],$newimagename);
            
            //Make Some Variables for Database
            $imagename = $newimagename;
            $thumbnail = $randomcode.'_thumb'.$value['file_ext'];
      
          
            
           $CI->load->model('photo_model');
            
            $photoInfo = array(
                'place_id' => $reviewID,
                'imagename' => $imagename,
                'thumbnail' => $thumbnail,
                'title' => $value['raw_name'],
                'width' => $value['image_width'],
                'height' => $value['image_height'],
                'filesize' => $value['file_size'],
                'description' => '',
                'timestamp' => $time,
                'up_status' => 1
            );
            
            $CI->photo_model->insertPhoto($photoInfo);
      /*      //Add Pic Info To Database
            $this->db->set('tour_id', $picInfo['tour_id']);
            $this->db->set('imagename', $imagename);
            $this->db->set('thumbnail', $thumbnail);
            $this->db->set('filesize', $filesize);
            $this->db->set('width', $width);
            $this->db->set('height', $height);
            $this->db->set('up_status', '1');
            $this->db->set('timestamp', $picInfo['timestamp']);
            
            //Insert Info Into Database
            return $this->db->insert('pictures');
*/
        }
        
}




?&gt;

and this is my controller
Code:
$this->load->helper(array('file','photo_upload_helper'));

    $config['upload_path'] = './img/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '2048'; //2 meg
    $config['max_width']  = '1024';
    $config['max_height']  = '1024';
                        
         $this->load->library('upload',$config);
    $this->load->library('image_lib');    
        foreach($_FILES as $key => $value)
    {
         if( ! empty($value['name']))
          {
                  if(!$this->upload->do_upload($key))
               {                                
            $data['error_upload'][] = $this->upload->display_errors();
            $sessUploadError = $this->upload->display_errors();
            $this->session->set_flashdata($sessUploadError);          
            //$errors = TRUE;
         }
           else
          {
            // Build a file array from all uploaded files
            
             $sessUploadData = $this->upload->data();                                  
                      $this->session->set_flashdata($sessUploadData);
             processPhoto(array($this->upload->data()), $placeID);
         }
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB