Code Igniter表单不发布

时间:2012-03-16 14:51:44

标签: php forms model-view-controller codeigniter codeigniter-2

我在CodeIgniter2中创建了一个网站,但我无法让表单工作,因为我似乎无法弄清楚如何发布它!有帮助吗?这是我的代码,表单仅在推荐,联系我们和支持页面上:

表格:

<div id="mainWhiteBox">
        <h3>Tell people about us...</h3>
        <p>If you know of a company or individual who need a really great design agency to help them with a project, let them know about us and benefit too. <br /><br />
        <span class="customColour">We will give you &pound;50 of Marks &amp; Spencer vouchers for every client you recommend to us who goes on to become a client of xxxxx, it's that simple &amp; there is no limit to the amount of vouchers you can earn!</span></p>
        <div id="recommendSomeone">
            <?php echo validation_errors(); print_r($_POST);?>
            <?php echo form_open('recommend', array('id' => 'recommendForm')); ?>
                <label for="friendName">Your Friend's Name</label>
                <input type="text" id="friendName" value="<?php echo set_value('friendName'); ?>" />
                <label for="friendEmail">Your Friend's Email Address</label>
                <input type="email" id="friendEmail" value="<?php echo set_value('friendEmail'); ?>" placeholder="someone@youknow.com" />
                <label for="customerName">Your Name</label>
                <input type="text" id="customerName" value="<?php echo set_value('customerName'); ?>" />
                <label for="customerEmail">Your Email Address</label>
                <input type="email" id="customerEmail" value="<?php echo set_value('customerEmail'); ?>" placeholder="you@youremailaddress.com" />
                <label for="friendConfirm"><input type="checkbox" id="friendConfirm" value="1" <?php echo set_checkbox('friendConfirm', '1'); ?> />I confirm that I know the person I am recommending above.</label>
                <input type="submit" value="Submit Recommendation" />
            </form>
            <img src="<?=base_url(); ?>images/uploads/<?php echo $images[0]["image_filename"]; ?>" alt="<?php echo $images[0]["image_alt"]; ?>" width="180px" height="300px" class="floatRight" />
        </div>
        <p class="elevenFont">* Get &pound;50 of Marks &amp; Spencer vouchers per company or person recommended who goes on to open an account with xxxxx.</p>
    </div>
    <?php include("/home/xxxxx/libraries/application/views/widgets/newsWidget.php"); ?>
    <?php include("/home/xxxxx/libraries/application/views/widgets/twitterWidget.php"); ?>
    <?php include("/home/xxxxx/libraries/application/views/widgets/quickViewWidget.php"); ?>
    <?php include("/home/xxxxx/libraries/application/views/widgets/fbLikePageWidget.php"); ?>
    <?php include("/home/xxxxx/libraries/application/views/widgets/getQuoteBarWidget.php"); ?>
    <?php include("/home/xxxxx/libraries/application/views/widgets/newsletterSubscribeWidget.php"); ?>

控制器:

<?php

class Pages extends CI_Controller {

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


public function view($page = 'home')
{

    if ( ! file_exists('/home/urbanfea/libraries/application/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }       

    $data['title'] = $this->pages_model->getTitle($page);
    $data['showcase'] = $this->pages_model->getShowcase();
    $data['news'] = $this->pages_model->getNewsWidgetContent();
    $data['quote'] = $this->pages_model->getQuoteFromBank();
    $data['images'] = $this->pages_model->getPageImageArray($page);
    $data['PageStraplines'] = $this->pages_model->getStraplines($page);
    $data['serverStatus'] = $this->pages_model->getIssue("1");

    if($page == "support")
    {
        $this->load->view('templates/supportHead', $data);
    }
    else
    {
        $this->load->view('templates/head', $data); 
    }

    if($page == "recommend" || $page == "contact-us" || $page == "support")
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('friendName', 'Friend\'s Name', 'required');
        $this->form_validation->set_rules('friendEmail', 'Friend\'s Email Address', 'required');
        $this->form_validation->set_rules('customerName', 'Customer\'s Name', 'required');
        $this->form_validation->set_rules('customerEmail', 'Customer\'s Email Address', 'required');
        //$this->form_validation->set_rules(FriendConfirm', 'Confirm you know the person', 'required');

        if ($this->form_validation->run() === true)
        {
            $this->load->view('templates/formSuccess', $data); echo "a";
        }
        elseif($this->form_validation->run() === false && validation_errors() != "")
        {
            $this->load->view('templates/formError', $data); echo "b";
        }
        elseif($this->form_validation->run() === false)
        {
            echo "c";
        }

    }

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);

}

}

&GT;

修改

以下是路由器中的路由:

$route['404_override'] = '';

$route['user/(:any)'] = 'user/view/$1';
$route['user'] = 'user/login';
$route['our-work/(:any)'] = 'our_work/view/$1';
$route['our-work'] = 'our_work';
$route['what-we-do/(:any)'] = 'what_we_do/view/$1';
$route['what-we-do'] = 'what_we_do';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

1 个答案:

答案 0 :(得分:3)

您的form_open函数echo form_open('recommend', array('id' => 'recommendForm'));将创建以下输出:<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/recommend" />

这是在寻找一个叫做推荐的控制器,我认为这不是你想要的。更改form_open函数,以便将表单定向到正确的控制器/操作。

此外,您的代码看起来并不充分利用MVC框架。而不是处理通过相同的控制器/函数传递所有内容并让所有if语句根据$ page加载不同的视图,您应该为每个视图分别具有单独的函数。

修改 表单输入元素缺少name属性。它们必须具有可通过$ _POST访问的name属性。看看this page in the Codeigniter help。也许可以使用form_input函数来生成输入字段?