如何正确使用CodeIgniter和jQueryMobile提交自我提交表单?

时间:2013-10-20 12:59:04

标签: jquery ajax forms codeigniter jquery-mobile

我已经在网上搜索了这个,但仍然没有进展。我只是想将一个表单提交到同一个URL,但它只是说"错误加载页面",并且控制台显示"无法加载资源:服务器响应状态为500(内部服务器错误)"。我已经独立测试了控制器代码,没有错误。

我在互联网上获得的是设置表单属性" data-ajax = false",但这只会导致另一个问题:我提交之后页面变为完全空白形式。

以下是代码:

查看(页眉和页脚省略,只有一些包装器和js和css文件加载):

<?php if (validation_errors()): ?>
<p><?php echo validation_errors(); ?></p>
<?php endif; ?>
<form id="lecture" action="" method="POST">
    <ul data-role="listview" data-inset="true">
        <li data-role="fieldcontain">
            <label for="title">Title</label>
            <input type="text" name="title" id="title" value="<?php if ($new) echo set_value('title'); else echo $lecture->title; ?>" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
            <label for="guests">Guests</label>
            <input type="text" name="guests" id="guests" value="<?php if ($new) echo set_value('guests'); else echo $lecture->guests; ?>" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
            <label for="place">Place</label>
            <input type="text" name="place" id="place" value="<?php if ($new) echo set_value('place'); else echo $lecture->place; ?>" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
            <label for="date">Date</label>
            <input type="text" name="date" id="date" value="<?php if ($new) echo set_value('date'); else echo $lecture->date; ?>" data-clear-btn="true" data-role="datebox" data-options='{"mode":"datebox", "useNewStyle":true, "useFocus":true}'>
        </li>
        <li data-role="fieldcontain">
            <label for="time">Time</label>
            <input type="text" name="time" id="time" value="<?php if ($new) echo set_value('time'); else echo $lecture->time; ?>" data-clear-btn="true" data-role="datebox" data-options='{"mode":"timebox", "useNewStyle":true, "useFocus":true}'>
        </li>
        <li data-role="fieldcontain">
            <label for="introduction">Introduction</label>
            <textarea cols="40" rows="8" name="introduction" id="introduction"><?php if ($new) echo set_value('introduction'); else echo $lecture->introduction; ?></textarea>
        </li>
        <li data-role="fieldcontain">
            <label for="status">Status</label>
            <select name="status" id="status" data-role="slider">
                <option value="0">Off</option>
                <option value="1">On</option>
            </select>
        </li>
        <li class="ui-body ui-body-b">
            <fieldset class="ui-grid-a">
                    <div class="ui-block-a"><button type="button" data-theme="d" data-rel="back">Cancel</button></div>
                    <div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
            </fieldset>
        </li>
    </ul>
</form>

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Lectures_model');
        $this->load->library('form_validation');
    }

    public function index()
    {
        $lectures = $this->Lectures_model->get_all_lectures();
        $data['lectures'] = $lectures;
        $this->load->view('header', array('title' => 'Lectures Management'));
        $this->load->view('admin', $data);
        $this->load->view('footer');
    }

    public function add_lecture()
    {
        $data_header['title'] = 'New Lecture';
        $data_header['extra_css'] = array('jqm-datebox.min.css');
        $data_footer['extra_js'] = array('jqm-datebox.core.min.js', 'jqm-datebox.mode.datebox.min.js', 'jquery.mobile.datebox.i18n.zh-CN.utf8.js');
        $data['new'] = TRUE;
        echo $this->load->view('header', $data_header, TRUE);
        if ($this->form_validation->run('lecture') == FALSE) {
            echo $this->load->view('admin_lecture', $data, TRUE);
        } else {
            $new_lecture = array(
                'title' => $this->input->post('title'),
                'guests' => $this->input->post('guests'),
                'place' => $this->input->post('place'),
                'datetime' => $this->input->post('date') . ' ' . $this->input->post('time'),
                'status' => $this->input->post('status'),
                'introduction' => $this->input->post('introduction')
            );
            $this->Lectures_model->insert_lecture($new_lecture);
            $this->session->set_flashdata('notification', 'Successfully Adding a lecture!');
            echo "Okay";
            // $this->load->view('admin_lecture', array('lecture' => $new_lecture));
        }
        echo $this->load->view('footer', $data_footer, TRUE);
    }

    public function edit_lecture($lecture_id)
    {
        $lecture = $this->Lectures_model->get_lecture($lecture_id);
        $datetime_format = 'Y-m-d H:i:s';
        $datetime = DateTime::createFromFormat($datetime_format, $lecture->datetime);
        $lecture->date = $datetime->format('Y-m-d');
        $lecture->time = $datetime->format('H:i');
        $data_header['title'] = $lecture->title;
        $data_header['extra_css'] = array('jqm-datebox.min.css');
        $data_footer['extra_js'] = array('jqm-datebox.core.min.js', 'jqm-datebox.mode.datebox.min.js', 'jquery.mobile.datebox.i18n.zh-CN.utf8.js', 'status_flip.js');
        $data['lecture'] = $lecture;
        $data['new'] = FALSE;
        // $output = $this->load->view('header', $data_header, TRUE);
        $this->load->view('header', $data_header);
        if ($this->form_validation->run('lecture') == FALSE) {
            // $output .= $this->load->view('admin_lecture', $data, TRUE); 
            $this->load->view('admin_lecture', $data); 
        } else {
            $new_lecture = array(
                'title' => $this->input->post('title'),
                'guests' => $this->input->post('guests'),
                'place' => $this->input->post('place'),
                'datetime' => $this->input->post('date') . ' ' . $this->input->post('time'),
                'status' => $this->input->post('status'),
                'introduction' => $this->input->post('introduction')
            );
            $this->Lectures_model->update_lecture($lecture_id, $new_lecture);
            $this->session->set_flashdata('notification', 'Successfully Editing a lecture!');
            // $this->load->view('admin_lecture', array('lecture' => $new_lecture));
        }
        // $output .= $this->load->view('footer', $data_footer, TRUE);
        $this->load->view('footer', $data_footer);
        // $this->ouput->set_output($output);
    }

}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */

我已经尝试了$this->output->set_output()echo,但仍然是#34;错误加载页面&#34;。

我正在使用CodeIgniter 2.1.4,jQuery 2.0.3和jQueryMobile 1.3.2。

0 个答案:

没有答案