2014-04-29 66 views
0

我在codeigniter中的表單有問題。我嘗試過不同的事情,但我找不到解決方案。form action codeigniter找不到帖子數據

這是以下形式:

<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser"> 
<div class="row"> 
    <div class="col-md-3"> 
     Naam: 
    </div> 
    <div class="col-md-9"> 
     <input type="text" id="naam" name="username" /> 
    </div> 
</div> 

<div class="row"> 
    <div class="col-md-3"> 
     E-mail: 
    </div> 
    <div class="col-md-9"> 
     <input type="email" name="mail" id="email" /> 
    </div> 
</div> 

<div class="row"> 
    <div class="col-md-3"> 
     Passwoord: 
    </div> 
    <div class="col-md-9"> 
     <input type="password" name="pass" id="password" /> 
    </div> 
</div> 

<div class="row"> 
    <div class="col-md-8 col-md-offset-2"> 
     <input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" /> 
    </div> 
</div> 

在我的控制器I過程從表單來的的數據。 控制器位於控制器/ CMS/cms.php

class Cms extends MY_Controller 
{ 
    public function newUser() 
    { 
     if(isset($_POST)) 
     { 
      echo "verwerk deze data <br>"; 

      if(isset($_POST['username'])) 
      { 
       echo 'naam is set!'; 
      } 
      else { 
       echo 'naam is not set!'; 
      } 

      $naam = $_POST['username']; 
      $mail = $_POST['email']; 
      $pass = $_POST['password']; 

      echo "naam: " . $naam . "<br>"; 
      echo "mail: " . $mail . "<br>"; 
      echo "pass: " . $pass . "<br>"; 

      $time = new DateTime(); 
      $time->setTimezone(new DateTimeZone ('Europe/Brussels')); 
      $tijd = $time->format('Y-m-d H:i:s'); 

      include_once('application/libraries/PasswordHash.php'); 

      $hasher = new PasswordHash(12,false); 
      $passwoord = $hasher->HashPassword($pass); 

      $gebruiker = new Gebruiker(); 
      $gebruiker->naam = $naam; 
      $gebruiker->email = $mail; 
      $gebruiker->paswoord = $passwoord; 
      $gebruiker->laatsteLogin = $tijd; 
      $gebruiker->active = 0; 

      echo "coded pas" . $passwoord . "<br>"; 
     } 
     else 
     { 
      echo "no post"; 
     } 
    } 

當我檢查我的網站,並填寫表格,我得到3 PHP的錯誤,我把在變量從帖子中的值不存在。 if(isset)函數也表示該變量未設置。我不知道爲什麼表單不起作用。

我甚至試過codeigniter的方式,但它給出了相同的結果。

$name = $this->input->post('username'); 

感謝您的幫助。

+0

Put:var_Dump($ _ POST);在if之前,看看裏面有什麼.. – Naruto

+0

什麼是你的base_url() – Jenz

回答

0
HTML: 

enter code here 

<form method="post" action="<?php echo site_url(); ?>/cms/cms/newUser"> 
<div class="row"> 
<div class="col-md-3"> 
    Naam: 
</div> 
<div class="col-md-9"> 
    <input type="text" id="naam" name="username" /> 
</div> 

電子郵件:

<div class="row"> 
<div class="col-md-3"> 
    Passwoord: 
</div> 
<div class="col-md-9"> 
    <input type="password" name="pass" id="password" /> 
</div> 
</div> 

<div class="row"> 
<div class="col-md-8 col-md-offset-2"> 
    <input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" /> 
</div> 
</div> 

PHP: 
class Cms extends MY_Controller 
{ 
public function newUser() 
{ 
    if(isset($_POST)) 
    { 
     echo "verwerk deze data <br>"; 

     if($this->input->post('username')) 
     { 
      echo 'naam is set!'; 
     } 
     else { 
      echo 'naam is not set!'; 
     } 

     $naam = $this->input->post('username'); 
     $mail = $this->input->post('mail'); 
     $pass = $this->input->post('pass'); 

     echo "naam: " . $naam . "<br>"; 
     echo "mail: " . $mail . "<br>"; 
     echo "pass: " . $pass . "<br>"; 

     $time = new DateTime(); 
     $time->setTimezone(new DateTimeZone ('Europe/Brussels')); 
     $tijd = $time->format('Y-m-d H:i:s'); 

     include_once('application/libraries/PasswordHash.php'); 

     $hasher = new PasswordHash(12,false); 
     $passwoord = $hasher->HashPassword($pass); 

     $gebruiker = new Gebruiker(); 
     $gebruiker->naam = $naam; 
     $gebruiker->email = $mail; 
     $gebruiker->paswoord = $passwoord; 
     $gebruiker->laatsteLogin = $tijd; 
     $gebruiker->active = 0; 

     echo "coded pas" . $passwoord . "<br>"; 
    } 
    else 
    { 
     echo "no post"; 
    } 
} 

$這個 - >輸入 - >後( '')會自動檢查不需要isset()。 $這個 - >輸入 - >後( '')將名稱爲ID

+0

您能解釋爲什麼site_url()工作並且base_url()不起作用嗎? – WouterV

0

你需要傳遞只controllername/FUNC_NAME並保持你的控制器文件controller目錄 然後在CI的方式嘗試

<?php echo form_open("cms/newUser");?> 
<?php echo form_close();?> 

<form method="post" action="<?php echo base_url(); ?>cms/newUser"> 

否則你需要使用routing對於這一點,告訴選擇這個路線 這個控制器去config/routes.php並添加像這樣的路由: -

$route['cms'] = "cms/cms"; 
+0

,根據它的設置,他可能需要在'html'表格版本中包含'index.php'。 – Albzi

0

在表單標籤

<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser"> 

cms是控制器的名字,你已經使用了兩次。試着用

<form method="post" action="<?php echo base_url(); ?>cms/newUser"> 
+0

名爲Cms的控制器位於名爲cms的映射中,而映射cms位於controllers - >所以這就是爲什麼我使用它兩次 – WouterV

0

嘗試加載形式助手,並使用此代碼

控制器:

$這個 - > viewContent [ 'form_action' ] = base_url()。「CMS/CMS/NEWUSER」; $ this-> load-> view('your_view_page',$ this-> viewContent);

view: echo form_open($ form_action);