我正在使用codeigniter php框架来构建包括联系表的标准网站,该网页的用户应该能够输入基本的联系表信息。
我已经设置并创建了联系表单的视图文件,并创建了一个相应的控制器-这包括使用codeigniter验证库进行验证,然后使用私有功能将有关查询的电子邮件发送给网站管理员等。
我在通过表单验证方案时遇到了问题,但是在提交表单时,我知道一个事实,即数据输入应通过验证测试,然后将数据传递给私有函数以发送电子邮件。
下面是我的文件,如果有人可以发现问题,并以正确的方向提示我,我将非常感激!
<form method="POST" action="/contact/contactvalidate" name="contactform" id="contactform">
<?php echo validation_errors(); ?>
<label>Full Name:</label><br />
<?php echo form_error('fullname'); ?>
<input type="text" name="fullname" id="fullname" maxlength="100" size="50" />
<br />
<label>Email:</label><br />
<?php echo form_error('email'); ?>
<input type="text" name="email" id="email" maxlength="100" size="50" />
<br />
<label>Telephone:</label> <br />
<?php echo form_error('telephone'); ?>
<input type="text" name="telephone" id="telephone" maxlength="100" size="50" />
<br />
<label>What is your enquiry regarding</label> <br />
<?php echo form_error('regarding'); ?>
<select name="regarding" id="regarding">
<option value="General Enquiry">General Enquiry</option>
<option value="HR Consultancy Service">HR Consultancy Service</option>
<option value="Business Startup Service">Business Startup Service</option>
<option value="Solutions for Individuals">Solutions for Individuals</option>
<option value="Other">Other</option>
</select>
<br />
<label>Your Enquiry</label> <br />
<?php echo form_error('enquiry'); ?>
<textarea name="enquiry" id="enquiry"></textarea>
<br />
<label>What is 4 + 1?</label> <br />
<?php echo form_error('robot'); ?>
<input type="text" name="robot" id="robot" />
<br />
<input type="submit" value="Submit Post" class="button" />
</form>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Controller {
public function index()
{
$this->load->view('general/view_header');
$this->load->view('page/view_contact');
$this->load->view('general/view_footer');
}
public function contactvalidate()
{
$this->load->library('form_validation');
if ($this->input->post('form') == 'contactform'){
//Set the validation rules based on the page
$this->form_validation->set_rules('fullname', 'Name', 'required|max_length[50]|xss_clean|prep_for_form');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|max_length[255]|xss_clean|valid_email|prep_for_form');
$this->form_validation->set_rules('telephone', 'Telephone', 'required|max_length[20]|xss_clean|prep_for_form');
$this->form_validation->set_rules('regarding', 'Regarding');
$this->form_validation->set_rules('enquiry', 'Enquiry', 'required|max_length[800]|xss_clean|prep_for_form');
$this->form_validation->set_rules('robot', 'Sum', 'required');
}
if ($this->form_validation->run() === true)
{
//Send the email
if($this->sendemail($_POST))
{
//If successful load the appropriate view
redirect('/thank-you');
}
}
else{
//If page exists load all necessary views
$this->load->view('general/view_header');
$this->load->view('page/view_contact');
$this->load->view('general/view_footer');
}
}
private function sendemail($content)
{
//Load the email library
$this->load->library('email');
//Initialise the email helper and set the "from"
$this->email->initialize(array("mailtype" => "html"));
$this->email->from("[email protected]", "Lesley Nowell HR Consultancy");
//Set the recipient, subject and message based on the page
//$this->email->to('[email protected]');
$this->email->to('[email protected]');
$this->email->subject('Website Enquirie');
$this->email->message("My name is: {$content["fullname"]}<br /><br />My email address is: {$content["email"]}<br /><br />My telephone number is: {$content["telephone"]}<br /><br />The enquiry is regarding: {$content["regarding"]}<br /><br />Enquiry: {$content["enquiry"]}");
//If the email is sent
if($this->email->send())
{
return true;
}
else
{
return false;
}
}
}
希望有足够的信息来了解我在做什么,我相信我一直在做的事情是正确的,我不确定到底该说些什么。
可悲的是,我最终决定解决自己的问题时,我本该推迟发布该帖子。
问题出在接触控制器内
if ($this->input->post('form') == 'contactform')
if ($this->input->post()){}
经过更多研究后发现,如果您将post()函数留空,它会在我只是简单地创建自己的问题之前从联系表单中选择所有已发布项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句