Welcome Guest, Not a member yet? Register   Sign In
weird: $this->input->post() value can not be retrieved after added to the database by jeditable
#1

[eluser]onuryasar[/eluser]
this is really one of the weirdest coding struggles i've had in my whole life.

today i added jeditable to my codeigniter site and made it work very easily. however, after finishing the database update, i cannot get the value back on the page. but it's not js related, looks like it's totally something about php side.

look at the codes:

view (script):
Code:
...
...
<td><div id="&lt;?php echo $product->products_ext_id ?&gt;" class="product_title">&lt;?php echo $product->title ?&gt;</div></td>
...
...
script
// &lt;![CDATA[
$(document).ready(function() {
  $(".product_title").editable("&lt;?php echo base_url(); ?&gt;/administrator/jeditable", {
      submit    : 'OK'
  });
});
// ]]>
script

controller:
Code:
function index() {
$this->load->model('jeditable_model');
$data['feedback'] = $this->jeditable_model->insert_value($this->input->post('id'),$this->input->post('value'));
$this->load->view("admin/jeditable_feedback_view",$data);
}

view (output):
Code:
$this->output->set_output($feedback);

the model is the part where it gets weird. the variables $id and $value are succesfully passed to the function and the database is being succesfully updated. then all i want to do is to return $element_value, which was added to the database, so that it will be printed on the page. but returns nothing:

model:
Code:
function insert_value($element_id,$element_value) {
    
        $data_db = array(
                'title' => $element_value
                );
        $this->db->where('id',$element_id);
        $this->db->update('products_ext',$data_db);

        return $element_value;
        
    }

if i try to add any dumb letter to the return value like

Code:
return $element_value.'asdf';

it returns succesfully as "asdf", so this shows that the problem is at the model but there's nothing between the database operation and the output, so how come the value cannot be echoed? is there such thing as "using the input value once"?? also, i tried to directly output the "value" without loading the model or doing the database update but no chance. i also tried the $this->output->set_output() method which was mentioned at another thread related to jeditable, but no chance on this one, either.

does anyone have any idea on this ?

thanks in advance Smile
#2

[eluser]tonanbarbarian[/eluser]
something to be aware of
i believe that $this->input->post('value') will return false if there is no POST variable found
so you say that the parameters element_id and element_value are being passed to the insert_value method correctly, but what are the actual values of the parameters
and does the database update work correctly? i.e. is the record updated and what is the title updated to?
#3

[eluser]onuryasar[/eluser]
the post variables are there for sure because the database is being updated correctly. when i change a title of the product, it saves the right title to the right product, where the new "title" value and the "id" of the record are both from the post variables. those post variables are sent via jeditable's jquery function (in my first view).

when i refresh the page i succesfully see the new value of the title. but the point of using jeditable is to see that change without refreshing, and that is made by the output of the "save" operation.
#4

[eluser]tonanbarbarian[/eluser]
have you determined if the data coming back from the model correctly or if the issue is in the view?
if you print_r($data); before the load->view in the controller do you get the value?
#5

[eluser]onuryasar[/eluser]
i tried and i got:

Code:
Array ( [feedback] => )


but more interestingly, as i mentioned at my first post, i changed the controller to this:

Code:
//$this->load->model('jeditable_model');
        //$data['feedback'] = $this->jeditable_model->insert_value($this->input->post('id'),$this->input->post('value'));
        //print_r($data);
        print_r($this->input->post('value'));
        //$this->load->view("admin/jeditable_feedback_view",$data);

the only thing it should do is to print out posted value, which if i load into the model is being succesfully saved to the database. but no, it turns empty. somehow the input variable can be put into the database but not echoed back.
#6

[eluser]tonanbarbarian[/eluser]
after the call to insert_value, in the controller, add the following
echo $this->db->last_query();

this will show you the query that was last run
i suspect that the query is not really updating
#7

[eluser]onuryasar[/eluser]
you were right, it returned

Code:
UPDATE `products_ext` SET `title` = 0 WHERE `id` = 0


but then, how come the database field is actually being updated with the new value? is it possible that the controller is calling itself once again without the post data (a kind of redirect) right after the database update?
#8

[eluser]tonanbarbarian[/eluser]
you should check the weblogs to see if the code is being run twice
#9

[eluser]Christophe28[/eluser]
Hi,

I use jeditable together with CodeIgniter and it works like a charm. Debug the script from step one. In the controller check if both values are actually submitted by echoing them right back to the script and so on ...

Here is my code for what it is worth:

JS
Code:
$('.fileDescription').editable('/update_file_description/', {
            type      : 'textarea',
            cols      : 40,
            rows      : 5,
            indicator : "<img src='/images/ajax-loader.gif' />",
            submit    : 'OK',
            cancel    : 'Cancel',
            onblur    : 'ignore'
        });

CONTROLLER:
Code:
function update_file_description() {
    
        // User language
        $lang = $this->sitewide->user_language();
    
        // Get the POST values
        $file_id = $this->input->post('id');
        $description = $this->input->post('value');
        
        // Get the user id out session table
        $user_id = $this->session->userdata('user_id');
        
        // Check if user is authenticated to edit this file
        $user_auth = $this->sitewide->ajax_file_auth($user_id, $file_id);
        
        if($user_auth == TRUE) {
        
            // If the user is authenticated, update the database
            $this->files_model->update_file_description($file_id, $description);
        
        } else {
            echo $lang->You_are_unauthorized;
            exit;
        }
    
        // Simply give back the $_POST title
        echo $description;
    
    }
And finally the files model:
Code:
// Update file description (= using jquery plugin jeditable)
    function update_file_description($file_id, $description) {
        
        $q = "UPDATE files SET description = ? WHERE id = ?";

        $this->db->query($q, array($description, $file_id));
    
    }

Please tell me if you manage it to work now.

Christophe
PS: I wouldn't create a special model just for jeditable, but rather one model for each table ;-)
#10

[eluser]onuryasar[/eluser]
thanks Christophe28, i actually didn't build the whole script from scratch but the first line of your script opened my mind about the problem.

i'm just stating it so maybe this will help somone someday Smile

remember my first line of JS code, i wrote the target URL as:

Code:
$(".product_title").editable("&lt;?php echo base_url(); ?&gt;/administrator/jeditable", {

and actually never thought that this would be the cause of the problem.

the thing is: I use a MY_Language.php to redirect every page to the language page, so "/administrator/jeditable" redirects to "/en/administrator/jeditable". and this redirect causes the request to be sent twice.

if i use this way of writing the target URL, the problem is solved:

Code:
$(".product_title").editable("&lt;?php echo site_url('administrator/jeditable'); ?&gt;", {

because site_url method creates the target as "/en/administrator/jeditable" so the request isn't being sent twice.




Theme © iAndrew 2016 - Forum software by © MyBB