Welcome Guest, Not a member yet? Register   Sign In
Updating my news post (with jquery/Ajax)
#11

[eluser]DynamiteN[/eluser]
[quote author="cahva" date="1271038875"]So you have this:
Code:
if($this->input->post('submit') == TRUE){
And its saying that $_POST['submit'] is not TRUE. Try to change that to
Code:
if($this->input->post('submit')){
CI's input lib will return false if its not set so you dont have to use == TRUE(which itself is faulty as submits value is "Edit Post").

And even better, dont use post('submit') to check if the form is submitted. Some browsers(for example older IE) dont send the input value if you have not pressed the button, but pressed enter on some of the fields which also sends the form.

And check from the firebug that it indeed have sent the right information in POST.[/quote]

i will check that right away
in firebug everytime i press submit it shows that it gets all of the data from the inputs
#12

[eluser]DynamiteN[/eluser]
hmmm okey ..
i actually removed this completly
Code:
$this->input->post('submit');
and now the UpdateNews() looks like this
Code:
function UpdateNews() {
        
        if(IS_AJAX){
            $nid = $this->uri->segment(4);
            
            $this->form_validation->set_rules('newstitle', 'News Title', 'required|trim');
            $this->form_validation->set_rules('newsauthor', 'News Author', 'required|trim');
            $this->form_validation->set_rules('newscontent', 'News Content', 'required|trim');
            $this->form_validation->set_rules('news_date', 'News Date', 'required');
    
            $newstitle = $this->input->post('newstitle');
            $newsauthor =  $this->input->post('newsauthor');
            $newscontent = $this->input->post('newscontent');
            $news_date =  $this->input->post('news_date');
                
            $data = array(
                'title'    => $newstitle,
                'content' => $newscontent,
                'author' => $newsauthor,
                'news_date' => $news_date
                );
                
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            }else{
                if($this->admin_news_model->UpdateNews($data, $nid)){
                    echo "News Updated";
                }
            }
        }else{
        echo "Direct Access Not allowed";
        }
    }

when i to submit i actually get "News Updated" from the line that checks to model
BUT nothing gets updated.
I actually think that there is something with the javascript that is messy it doesnt seem to actually send the actual values to the model, thats what i think anyway,
because to the controller the values to come as the validation errors appear if don't fill in a field
adn when it comes to ajax/jquery or whatever it is called my mind draws a complete blank .. :/

P.S Would like to thank you guys for sticking with my problem <3 Smile
#13

[eluser]pickupman[/eluser]
What does $this->admin_news_model->UpdateNews($data,$nid) look like? Are you returning false if $this->db->update fails?
#14

[eluser]DynamiteN[/eluser]
[quote author="pickupman" date="1271050447"]What does $this->admin_news_model->UpdateNews($data,$nid) look like? Are you returning false if $this->db->update fails?[/quote]
If i remove the jquery/ajax the update works dont get any errors ...
Code:
function UpdateNews($data, $nid) {
        //Update a news post
    
        $this->db->set($data);
        $this->db->where('id', $nid);
        if($update = $this->db->update('news')){
            //return TRUE;
            return $update;
        }else{
            return FALSE;
        }
        
    }
that is the model function
im sure it could use some cleaning up, maybe some more security checks and whatnot
the code does work when updating correct and incorrect
i am quite sure that there is something in the jquery that needs looking over
but as i dont know that language that well im just stuck .. :/
#15

[eluser]pickupman[/eluser]
Try this in your model.
Code:
function UpdateNews($data, $nid) {
        //Update a news post
    
        if(is_array($data))
        {
          foreach($data as $field=>$value){
             $this->db->set($field,$value);
          }
          
          $this->db->where('id', $nid);
          
          if($this->db->update('news')){
              
            return TRUE;
            
          }
        }
       return FALSE;        
    }
#16

[eluser]DynamiteN[/eluser]
That didnt work any better with jquery/ajax on
and still worked whithout it
#17

[eluser]pickupman[/eluser]
Okay, I was looking way to deep into the CI logic. The issue is using jQuery .serialize(). It is passing a URL string of the values. It's not going to be available via $this->input->post(); You will need to parse the url string into your variables, probably using explode().
#18

[eluser]DynamiteN[/eluser]
hmm ....
i think i have to go get some sleep and look into that in the morning to see how to go about doing this .. Smile

P.S Thanks alot for helping me with my code Smile , gn now im gonna go glue my eyes shut so i dont sit here all night Wink
#19

[eluser]pickupman[/eluser]
I was being lazy. I was using jQuery.post instead of jQuery.ajax. It is passing the parameters as you would expect. $.ajax when serializing the fields is passing each of them to the server. I am starting to run out of ideas.
#20

[eluser]DynamiteN[/eluser]
[quote author="pickupman" date="1271057601"]I was being lazy. I was using jQuery.post instead of jQuery.ajax. It is passing the parameters as you would expect. $.ajax when serializing the fields is passing each of them to the server. I am starting to run out of ideas.[/quote]

hmm ... what about this block of code in the jquery what does that "if(parseFloat)" do anyway , maybe something like if it's true do this, or .. ? i dont know ...
Code:
if(parseFloat(html)){
                 $('#news_form').after('Thank you for submitting');
                 $('#news_form').hide('slow');
             }else{
                 $('#errors').html(html).show('slow');
             }




Theme © iAndrew 2016 - Forum software by © MyBB