大家好我正在创建一个带有codeigniter和bootstrap的新项目。
我已经配置了第一页,CI正在加载所有内容,但是我遇到了联系表单的问题。 如果我尝试使用它而没有codeigniter工作正常,所以我认为问题出在控制器中。
这是我的代码:(那是视图)landing.php
<div class="col-md-8 col-md-offset-2">
<form action="%3C?php%20echo%20site_url('landing/index');%20?%3E" class=
"wow bounceInUp" data-wow-delay="0.2s" data-wow-offset="10" id=
"contact-form" method="post" name="contact-form" onsubmit=
"return checkform();">
<input name="act" type="hidden" value="act">
<div class="row">
<div class="col-md-6" style="margin-bottom:5px">
<input class="form-control input-lg" id="name" name="name"
placeholder="Name" required="required" type="text" value="name">
</div>
<div class="col-md-6" style="margin-bottom:5px">
<input class="form-control input-lg" id="email" name="email"
placeholder="E-mail" required="required" type="email" value="email">
</div>
</div>
<div class="row">
<div class="col-md-12" style="margin-bottom:5px">
<div class="form-group">
<input class="form-control input-lg" id="object" name="object"
placeholder="Object" required="required" type="text" value=
"object">
</div>
<div class="form-group" id="interested">
<select class="form-control input-lg" id="contact-form" name=
"interested">
<option value="select">
--Select--
</option>
<option id="info" value="info">
Info
</option>
<option id="careers" value="careers">
Careers
</option>
</select>
</div>
<div class="form-group" style="margin-bottom:5px">
<textarea class="form-control" name="message" rows="3">
</textarea>
</div><button class="btn btn-primary" style="width:100%;" type=
"submit">Send</button>
</div>
</div>
</form>
</div>
这是控制器landing.php;
class Landing extends CI_Controller {
public function index(){
$this->load->view('header/landing.php');
$this->load->view('landing.php');
$this->load->view('footer/landing.php');
if(isset($_POST['act']) && $_POST['act'] =="act"){
$name = $_POST['name'];
$object = $_POST['object'];
$email = $_POST['email'];
$message = $_POST['message'];
$interested = $_POST['interested'];
if ($interested == 'info') {
$to = 'info@mymail.it';
}
else if ($interested == 'careers') {
$to = 'careers@mymail.it';
}
$this->load->library('email');
$this->email->from($email, $name);
$this->email->to($to);
$this->email->subject($object);
$this->email->message($message);
$this->email->send();
}
}
这是我尝试发送邮件时显示的错误:
遇到错误
不允许您请求的操作。
修复!代码没问题,但我传递了错误的变量 $这 - &GT;的电子邮件 - &gt;至($感兴趣的); &lt; - $感兴趣进入一个if else并成为$ to!这就是为什么不起作用的原因! :d 修复!
答案 0 :(得分:0)
我之前遇到过这个问题,它与CSRF安全性有关。
您应该使用表单助手而不是对表单进行硬编码。
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
一旦加载了帮助器,您的表单标签就会发生变化;
<form id="contact-form" class="wow bounceInUp" data-wow-offset="10" data-wow-delay="0.2s" method="POST" action="<?php echo site_url('landing/index'); ?>" onsubmit="return checkform();" />
对此;
<?php echo form_open('landing', 'class="wow bounceInUp" id="contact-form" data-wow-offset="10" data-wow-delay="0.2s" onsubmit="return checkform();"'); ?>
编辑...
<?php echo form_open('landing', 'id="contact-form" class="wow bounceInUp" data-wow-offset="10" data-wow-delay="0.2s" onsubmit="return checkform();"'); ?>
答案 1 :(得分:-1)
在config.php文件中设置此配置:
$config['csrf_protection'] = FALSE;