JQuery Ajax POST in Codeigniter

后端 未结 5 1008
情深已故
情深已故 2020-12-09 18:58

I have searched a lot of tutorials with POST methods and saw answered questions here too but my POST still doesn\'t work...I thought i should post it here if you guys see so

相关标签:
5条回答
  • 2020-12-09 19:38
        <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
        class UserController extends CI_Controller {
    
            public function verifyUser()    {
                $userName =  $_POST['userName'];
                $userPassword =  $_POST['userPassword'];
                $status = array("STATUS"=>"false");
                if($userName=='admin' && $userPassword=='admin'){
                    $status = array("STATUS"=>"true");  
                }
                echo json_encode ($status) ;    
            }
        }
    
    
    function makeAjaxCall(){
        $.ajax({
            type: "post",
            url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser",
            cache: false,               
            data: $('#userForm').serialize(),
            success: function(json){                        
            try{        
                var obj = jQuery.parseJSON(json);
                alert( obj['STATUS']);
    
    
            }catch(e) {     
                alert('Exception while request..');
            }       
            },
            error: function(){                      
                alert('Error while request..');
            }
     });
    }
    
    0 讨论(0)
  • 2020-12-09 19:38

    The question has already been answered but I thought I would also let you know that rather than using the native PHP $_POST I reccomend you use the CodeIgniter input class so your controller code would be

    function post_action()
    {   
        if($this->input->post('textbox') == "")
        {
            $message = "You can't send empty text";
        }
        else
        {
            $message = $this->input->post('textbox');
        }
        echo $message;
    }
    
    0 讨论(0)
  • 2020-12-09 19:44
    $(document).ready(function(){   
    
        $("#send").click(function()
        {       
         $.ajax({
             type: "POST",
             url: base_url + "chat/post_action", 
             data: {textbox: $("#textbox").val()},
             dataType: "text",  
             cache:false,
             success: 
                  function(data){
                    alert(data);  //as a debugging message.
                  }
              });// you have missed this bracket
         return false;
     });
     });
    
    0 讨论(0)
  • 2020-12-09 19:44

    In codeigniter there is no need to sennd "data" in ajax post method.. here is the example .

       searchThis = 'This text will be search';
        $.ajax({
          type: "POST",
          url: "<?php echo site_url();?>/software/search/"+searchThis,
          dataType: "html",
          success:function(data){
            alert(data);
          },
    
        });
    

    Note : in url , software is the controller name , search is the function name and searchThis is the variable that i m sending.

    Here is the controller.

        public function search(){
        $search = $this->uri->segment(3);
          echo '<p>'.$search.'</p>';
        }
    

    I hope you can get idea for your work .

    0 讨论(0)
  • 2020-12-09 19:47
    <script>
    $("#editTest23").click(function () {
    
            var test_date = $(this).data('id');
            // alert(status_id);    
    
            $.ajax({
                type: "POST",
                url: base_url+"Doctor/getTestData",
                data: {
                    test_data: test_date,
                },
                dataType: "text",
                success: function (data) {
                    $('#prepend_here_test1').html(data);
                }
            });
            // you have missed this bracket
            return false;
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题