Welcome Guest, Not a member yet? Register   Sign In
"Commit" topic in Codeigniter by using Ajax
#1

[eluser]haily_php[/eluser]
I have module "Commit" which is used to feedback News. I want to use ajax to execute process but my code hasn't run exact.

View---

Code:
<div id="detail_comment1">
                    <div id="ketqua"></div>
                    <h3>Send Feedback</h3>
                    <p> &lt;input id="comment_title" type="text" maxlength="250" class="comment_input text" value="Title *"/&gt; &lt;input id="comment_telephone" type="text" maxlength="250" class="comment_input" value="Telephone *"/&gt;&lt;/p>
                    <p> &lt;input id="comment_fullname" type="text" maxlength="250" class="comment_input text2" value="Full Name *"/&gt;
                        &lt;input id="comment_email" type="text" maxlength="250" class="comment_input text2" value="Your Email *"/&gt;
                    </p>
                    <p>&lt;input id="comment_address" type="text" maxlength="250" class="comment_input text" value="Your Address"/&gt;&lt;/p>
                    <p>
                        &lt;textarea rows="10" cols="130" class="comment_textarea" id="comment_content"&gt;&lt;/textarea>
                    </p>
</div>
                &lt;/form&gt;


Controller-----

Code:
public function checkcomment(){
        $this->load->helper(array("url","form"));
        
        $name=$this->input->post("c_name");
        $title=$this->input->post("c_title");    
        $telephone=$this->input->post("c_telephone");
        $email=$this->input->post("c_email");
        $adress=$this->input->post("c_adress");
        $content=$this->input->post("c_content");
        
        $this->load->library("form_validation");
            $this->form_validation->set_rules($title,"Comment title","required");
            $this->form_validation->set_rules($name,"Full name","required");
            $this->form_validation->set_rules($telephone,"Telephone","required|is_natural");
            $this->form_validation->set_rules($email,"Email","required|valid_email");
            $this->form_validation->set_rules($content,"Content","required");
            
        if($this->form_validation->run()==FALSE){
            $this->load->view("comment");
        }else{
            echo "Correct information"; // debug code
        }

View "Comment" --- to show error validation

Code:
&lt;?php echo validation_errors();?&gt;

File ajax ---


Code:
function comment(){
    document.getElementById("ketqua")[removed]="Loading data ...  ";
    title=encodeURI(document.getElementById('comment_title').value);
    telephone=encodeURI(document.getElementById('comment_telephone').value);
    email=encodeURI(document.getElementById('comment_email').value);
    adress=encodeURI(document.getElementById('comment_address').value);
    content=encodeURI(document.getElementById('comment_content').value);
    name=encodeURI(document.getElementById('comment_fullname').value);
    
    http.open('post','http://localhost/ci/index.php/index/checkcomment',true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    http.onreadystatechange=process;
    http.send('c_title='+title+"&c_telephone="+telephone+"&c_email="+email+"&c_adress="+adress+"&c_content="+content+"&c_name="+name);
}

function process(){
    if(http.readyState == 4 && http.status == 200){
        kq=http.responseText;
            document.getElementById('ketqua')[removed]= kq;
        
    }
}


Error: if I enter correct information, program has also shown about Form validation

Please help me
#2

[eluser]toopay[/eluser]
this is how you should use validation set_rules function :
Code:
// Here you passed the value of $_POST['c_title']. This wrong
$this->form_validation->set_rules($title,"Comment title","required");
// If, you have $_POST['c_title'] as param, you use set_rules like this
$this->form_validation->set_rules('c_title',"Comment title","required");




Theme © iAndrew 2016 - Forum software by © MyBB