2012-03-16 94 views
1

我在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="[email protected]" /> 
       <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="[email protected]" /> 
       <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); 

} 

}

>

編輯

下面是我的路由器的路由:

$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'; 

回答

4

你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語句基於$頁面加載不同的視圖,您應該爲每個視圖分別設置不同的函數。

編輯: 您的表單輸入元素缺少name屬性。它們必須具有可通過$ _POST訪問的name屬性。看看this page in the Codeigniter help。也許可以使用form_input函數來生成輸入字段?

+0

嗨AndrewR,這是我的第一個MVC網站,我一直在編程編碼到現在,所以MVC代碼對我來說是新的,所以這就是爲什麼我沒有做出不同的看法。不,我不希望它去推薦的控制器,它被髮送到頁面控制器。我很困惑,因爲它會'刷新'頁面並回顯c以顯示沒有提交表單(在我設置規則的底下),但是'print_r($ _ POST);'只顯示一個空數組:S – 2012-03-16 15:47:27

+0

你沒有在你的routes.php文件中做一些類型的重定向,讓你把所有的東西都放到相同的頁面/視圖控制器中? – AndrewR 2012-03-16 16:23:50

+0

您缺少表單輸入中的'name'屬性。查看更新的答案。 – AndrewR 2012-03-16 16:28:47