我在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>
在我的控制器中我处理来自表单的数据。 控制器位于controllers / 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 :(得分: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";
答案 1 :(得分:0)
在表单标记
中<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser">
cms
是控制器的名称,您已经使用过两次。试试
<form method="post" action="<?php echo base_url(); ?>cms/newUser">
答案 2 :(得分: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";
}
}
$ this-&gt; input-&gt; post(&#39;&#39;)将自动检查不需要isset()。 $ this-&gt; input-&gt; post(&#39;&#39;)将取名而不是id
答案 3 :(得分:0)
尝试加载表单助手并使用此代码
控制器:
$ this-&gt; viewContent [&#39; form_action&#39;] = base_url()。&#34; cms / cms / newUser&#34 ;; $这 - &GT;负载&gt;查看(&#39; your_view_page&#39;,$这 - &GT; viewContent);
视图: echo form_open($ form_action);