gpt4 book ai didi

php - fatal error : Call to undefined function form_open() line 104

转载 作者:行者123 更新时间:2023-11-29 11:49:21 26 4
gpt4 key购买 nike

我知道你们大多数人都会意识到这是一个愚蠢的问题,但我不知道我的代码有什么问题。我正在尝试创建一个将发送到数据库的联系表单。但是当我尝试访问 contact_form_view.php 时,它显示 fatal error :在第 104 行调用未定义的函数 form_open()

这是表单代码

<?php $attributes = array("name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>

<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>

<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>

<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>

<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>

这是PHP代码

<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}

function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);

if ($this->db->insert('contacts', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}

//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>

最佳答案

如果您尝试像这样在 autoload.php 中加载表单助手

$autoload['helper'] = array('url', 'form');

我认为您在 Controller 中加载了帮助程序,该 Controller 具有表单的操作,而不是调用表单的 Controller 。

所以只需将助手添加到 autoload.php 即可工作

关于php - fatal error : Call to undefined function form_open() line 104,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34398627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com