Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法从CodeIgniter内的表单提交运行同步jQuery ajax请求_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Php 无法从CodeIgniter内的表单提交运行同步jQuery ajax请求

Php 无法从CodeIgniter内的表单提交运行同步jQuery ajax请求,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我有一个js函数,表单上的操作会调用它两次,一次是用户通过jQuery.change在字段中输入文本,第二次是表单提交时。提交表单时,它调用函数执行ajax请求,然后根据返回值停止或继续提交表单。我似乎得到的只是“未定义”的返回值 正在调用的函数如下所示 function cf_check_course_slug() { // check the slug name is unique var course_id = $('#Course_Id').val(); var

我有一个js函数,表单上的操作会调用它两次,一次是用户通过jQuery.change在字段中输入文本,第二次是表单提交时。提交表单时,它调用函数执行ajax请求,然后根据返回值停止或继续提交表单。我似乎得到的只是“未定义”的返回值

正在调用的函数如下所示

function cf_check_course_slug() {
    // check the slug name is unique
    var course_id = $('#Course_Id').val();
    var course_slug = $('#Slug').val();
    if(course_slug!='') {
        // call script via ajax and ensure it is not already assigned
        $.ajax({
            type:   "GET"
        ,   url:    "/courses/course_slug"
        ,   data:   { cid: course_id, slug: course_slug }
        ,   async:  false
        }).done(function(response) {
            // if we have a duplicate course slug then notify user
            if(response=='duplicate') {
                showErrorMessage('The SLUG created from the LINK NAME you have entered is already being used by another course, please edit the LINK NAME.')
                return false;
            } else {
                return true;
            }
        });
    }
}
提交表单时,它在javascript中激发以下代码

$(document).ready(function () {

    // check we are in the correct module before firing this script
    if($('#course-form').length > 0) {

        // check the link name has created a unique slug
        $('#LinkName').change(function() {
            cf_check_course_slug();
        });


        // check the course record form prior to saving
        $('form').submit(function() {
            if(!cf_check_course_slug()) {
                return false;
            }
        });

    }

});
此cf_u函数调用CI中的控制器,该控制器对数据库执行检查,以检查slug是否重复,如果重复,则返回字符串“duplicate”。如果我更新了链接名,并对其进行了检查,那么它将非常有效

我假设函数的异步设置有问题,就好像我手动将其编码到submit js中一样,它可以按预期工作-尽管我希望尽可能保持干燥

任何指点都很好

更新#1 下面是控制器中并从javascript函数调用的以下代码

function course_slug() {

        // first check this request has come via ajax
        if($this->input->is_ajax_request()) {

            $iCourse_Id = $this->input->get('cid', true);
            $sSlug = $this->input->get('slug', true);

            // load the model and data
            $this->load->model('Course_Model');
            $aCourses = $this->Course_Model->get_courses_by_category();

            // check each course and perform a check on the slug
            $output = '';
            foreach($aCourses as $aCourse) {
                // if we are dealing with an existing course, omit the Course_Id from the course dataset
                if($iCourse_Id && is_numeric($iCourse_Id)) {
                    if($aCourse['Course_Id']!=$iCourse_Id && $aCourse['Active']==1 && $aCourse['Slug']==$sSlug) {
                        $output = 'duplicate';
                    }
                } else {
                    // we are dealing with a new course, check through ALL courses in the dataset
                    if($aCourse['Slug']==$sSlug) {
                        $output = 'duplicate';
                    }
                }

            }

            // spit out the result
            $this->output->set_content_type('application/json')->set_output(json_encode($output));

        } else {
            // redirect to course homepage
            redirect('courses'); 
        }

    }

您的url部分应该是

url:    "< ?php echo site_url("/courses/course_slug"); ?>"
而不是

url:    "/courses/course_slug"
如果您使用的是标准htaccess,则相对url在codeigniter中并不总是有效,因为codeigniter使用index.php创建nice_url选项,因此您的url应该类似于localhost/index.php/courses/courses_slug,而您添加的是localhost/courses/courses_slug,如果没有重写引擎,它将无法工作


虽然如果您也可以发布php脚本,那么对于为什么会得到未定义的响应会有更多帮助。

Howdy Hun73r,我在原始帖子中添加了ajax调用的php控制器。我将url值更改为控制器的完整url路径,但仍然没有更改。
url:    "/courses/course_slug"