I know most of you guys will realize this is a dumb question but I don't have any idea what's wrong with my code. I'm trying to create a contact form that will be sent into the database. But when I try to go to the contact_form_view.php it says Fatal error: Call to undefined function form_open() on Line 104
Here's the form code
<?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'); ?>
Here's the php code
<?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;
}
}
}
?>
if you please try to load form helper in your autoload.php like this
$autoload['helper'] = array('url', 'form');
i think you load the helper in the controller which have the action of your form not the controller that call the form.
so just add the helper to autoload.php and it will work
Related
I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo $author->first_name;?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo $author->last_name;?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo $author->email;?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo $author->bio; ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
It's corresponding controller logic is this:
public function edit($id) {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
}
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE)
{
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The problem I have not been able to solve is related to the validation of this form: if I leave a required filed empty, and try to submit the form, the form is loaded, with the proper validation error message, only the invalid field is populated with the data from the database and so, valid data is displayed along with the error message, as seen in the form below:
However, as long as the field's current value is not replaced with an invalid one, I want it (the field value) to come from the database.
How can I solve this issue?
If you want to keep the submitted data on an input value, you could modify the email codes like this :
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
This will set the submitted data as the email value (if available), or email data from the database if there is no submitted email data.
I have a problem with uploading images in the database in codeigniter. When I upload image in the database, it shows this type of error like:
The upload path does not appear to be valid. So what's the problem in this code. I cannot figure out, so please help me.
I tried base_url() method also but error is not solved...!
My Htmlcode:-
<?php echo form_open_multipart('student/insertstudent') ?>
<form>
<div class="panel panel-primary" >
<div class="panel-heading" >
<p class="label" style="font-size: 15px">Student Registration Form</p>
</div>
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter First Name" name="fname" value="<?php echo set_value('fname'); ?>">
<div class="row-lg-6">
<?php echo form_error('fname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Last Name</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Last Name" name="lname" value="<?php echo set_value('lname'); ?>">
<div class="row-lg-6">
<?php echo form_error('lname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleTextarea">Address</label>
<textarea class="form-control" id="exampleTextarea" rows="2" placeholder="Enter Address" name="add"><?php echo set_value('add'); ?></textarea>
<div class="row-lg-6">
<?php echo form_error('add'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Contact Number</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Contact Number" name="cno" value="<?php echo set_value('cno'); ?>">
<div class="row-lg-6">
<?php echo form_error('cno'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Emaid ID</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter your email" name="email" value="<?php echo set_value('email'); ?>">
<div class="row-lg-6">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Enter Course</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="cname" value="<?php echo set_value('cname'); ?>">
<div class="row-lg-6">
<?php echo form_error('cname'); ?>
</div>
</div>
<!-- <div class="form-group">
<label for="exampleInputPassword1">Select Student Photo</label>
<input type="file" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="photo">
<div class="row-lg-6">
<?php echo form_error('photo'); ?>
</div>
</div> -->
<div class="form-group">
<label for="exampleInputPassword1">Select Image</label>
<?php echo form_upload(['name' => 'userfile','class'=>'form-control']); ?>
<div class="row-lg-6">
<?php if(isset($upload_error))
{
echo $upload_error;
} ?>
</div>
</div>
<h4 align="center" style="margin-left: -150px"><button type="submit" class="btn btn-primary">Register</button></h4>
<h4 align="center" style="margin-top: -43px;margin-left: 200px"><button type="reset" class="btn btn-primary">Reset</button></h4>
</fieldset>
</div>
</div>
my controller file:-
public function insertstudent()
{
$this->form_validation->set_rules('fname', 'FirstName', 'required|alpha');
$this->form_validation->set_rules('lname', 'lastName', 'required|alpha');
$this->form_validation->set_rules('add', 'Address', 'required');
$this->form_validation->set_rules('cno', 'Contact Number', 'required|numeric');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('cname', 'Course Name', 'required');
// $this->form_validation->set_rules('userfile', 'Student Photo', 'required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");
$config['upload_path'] = base_url('asset/uploads/');
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
$this->load->library('upload',$config);
if ($this->form_validation->run() == TRUE && $this->upload->do_upload('userfile')) {
$post = $this->input->post();
$data = $this->upload->data();
$image_path = base_url("upload/".$data['raw_name'].$data['file_ext']);
$post['image_path'] = $image_path;
$this->load->model('useradd');
if($this->useradd->addstudent($post)){
$this->session->set_flashdata('success', 'Record Successfully Inserted');
return redirect('admin');
}else {
$this->load->view('addstudent');
}
// $data = array(
// 'firstname' => $this->input->post('fname'),
// 'lastname' => $this->input->post('lname'),
// 'address' => $this->input->post('add'),
// 'phone' => $this->input->post('cno'),
// 'email' => $this->input->post('email'),
// 'course' => $this->input->post('cname'),
// 'photo' => $image_path,
// );
// // $this->load->library('upload', $data);
// if ($result == true) {
// $this->session->set_flashdata('success', 'Record Successfully Inserted');
// return redirect('admin');
// } else {
// $this->load->view('addstudent');
// }
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('addstudent',compact('upload_error'));
}
}
my Model:-
class useradd extends CI_Model
{
function addstudent($data)
{
$result = $this->db->insert('add_student', $data);
}
}
My error is:
The upload path does not appear to be valid.
Try This Code To Upload Image.and remove form tag in your view file and you didn't define form_close method at the end of the form of your view file
$file = $_FILES['filename']['name'];
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload();
$allData = $this->upload->data();
Hi i have request form if i enter wrong captcha and click on submit button all the form fields which i have entered are getting cleared.If there is any error in the captcha form only the captcha has to be refreshed and the form values should not be cleared.
View:
<form name="contact" id="contactform" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>welcome/request">
<div class="name"><span class="mandatory"></span>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" value="<?php echo set_value('name');?>" required>
<?php echo form_error('name', '<div class="error">', '</div>'); ?>
</div>
<div class="name">
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo set_value('phone');?>" placeholder="Phone" MinLength ="10" MaxLength="10" onkeypress="return isNumber(event)" required>
<?php echo form_error('phone', '<div class="error">', '</div>'); ?>
</div>
<div class="email">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required value="<?php echo set_value('email');?>"/>
<?php echo form_error('email', '<div class="error">', '</div>'); ?>
</div>
<div class="description">
<textarea name="description" style="width:100%;overflow:auto;" id="description" placeholder="Description" maxlength="3000" required value="<?php echo set_value('description');?>"/></textarea>
</div>
<p class="entertext">Enter Image Text</p>
<div class="">
<div class="captchas">
<div class="captchatexts" ><?php echo $captchaImg;?></div>
<p id = 'ref_symbol' class='refresh clickto'>Click to change</p>
</div>
<div class="captcha">
<input type="text" class="form-control" id="captcha" name="captcha" style="background-color: #EAD5D2;border: none;" required>
<?php echo form_error('captcha', '<div class="error">', '</div>'); ?>
</div>
</div>
<input type="hidden" value="true" name="isSubmitted">
<button type="submit" id="submit" class="btn btn-success" onclick="return validate();" >Submit</button><!-- id="demo"!-->
</form>
Controller:
function request()
{
$this->load->library('form_validation');
if ($this->input->post('email'))
{
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('name','First Name' , 'required');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('phone','Mobile Number','required|numeric');
$this->form_validation->set_rules('description','Description','required');
$this->form_validation->set_rules('captcha', 'Captcha', 'required');
if($this->form_validation->run()== FALSE)
{
/*echo "valid";
print_r($this->input->post());
exit;*/
$data['mainpage']='index';
$this->load->view('templates/template',$data);
}
else
{
/*echo "invalid";
print_r($this->input->post());
exit;*/
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if ($inputCaptcha === $sessCaptcha)
{
$result = $this->index_model->send_mail($this->input->post('email'));
if ($result)
{
$this->flash->success('<h2 style="color:green">Thank You! Your Message has been sent</h2>');
redirect('welcome');
}
else
{
$this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
redirect('welcome');
}
}
else
{
$this->flash->success('<h2 style="color:red;font-size:15px;margin-top:6px;">Please enter correct captcha.</h2>');
redirect('welcome');
}
}
}
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url() . 'captcha_images/',
'img_width' => '125',
'img_height' => 35,
'word_length' => 6,
'font_size' => 30
);
$captcha = create_captcha($config);
$word = $captcha['word'];
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode', $word);
$data['captchaImg'] = $captcha['image'];
//$data['captchaImg'] = $captcha['image'];
$data['mainpage'] = "index";
$this->load->view('templates/template',$data);
}
Can anyone please help me out this
Don't load view twice, try to remove the loading of view from if($this->form_validation->run()== FALSE) because when the view loads then set_value inside it is triggered and you have called loading view after creating captcha. So remove the load view when validation errors are there like,
....
if($this->form_validation->run()== FALSE) {
/*echo "valid";
print_r($this->input->post());
exit;*/
$data['mainpage']='index';
// comment the below line, otherwise there are no loophole in your code
//$this->load->view('templates/template',$data);
}
....
Hope the above will work for you.
Hi when I use required in my rules in ci form_validation, I know the value of this required input post and don't show any message .but this if ($this->form_validation->run()) is always false.
thanks for your help.
my view:
<form>
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
</form>
my controller:
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
}
else redirect();
}
In your view file, you had wrote nmae except name
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
correct it.
please load again view file after false form_validation
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
$this->load->view('view_file','',true);
}
else redirect();
}
perhaps the video will easily explain the problem. here's the link to my video .
here's the view code
<div class="col-sm-4">
<?php echo form_open('user/updateuser');
?>
<legend>Update User</legend>
<div class="form-group">
<label for="id">ID</label>
<input name="id" type="text" class="form-control" id="id" placeholder="Input id" value="<?php echo $id; ?>" disabled>
<?php echo form_error('id'); ?>
</div>
<div class="form-group">
<label for="username">Username</label>
<input name="username" type="input" class="form-control" id="username" placeholder="Input Username" value="<?php echo $username;?>">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="password">Old Password:</label>
<input name="old_password" type="password" class="form-control" id="password" placeholder="Input Old Password"" value ="<?php set_value('old_password');?>">
<?php echo form_error('old_password')?>
</div>
<div class="form-group">
<label for="password">New Password:</label>
<input name="password" type="password" class="form-control" id="password" placeholder="Input Password" ">
<?php echo form_error('password')?>
</div>
<div class="form-group">
<label for="password">New Password Confirmation:</label>
<input name="password_conf" type="password" class="form-control" id="password" placeholder="Input Password Confirmation">
<?php echo form_error('password_conf')?>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" value="<?php echo $email; ?>">
<?php echo form_error('email')?>
</div>
<div class="form-group" align="center">
<button type="submit" class="btn btn-success">Submit</button>
<button type="reset" class="btn btn-danger">Clear</button>
</div>
</div>
<?php
echo form_close();
?>
and here's the controller user/updateuser
function index()
{
//This method will have the credentials validation
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean|callback_check_password');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|matches[password_conf]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
if($this->isloggedin('logged_in'))
{
if($this->form_validation->run() == FALSE)
{
$data = array(
'sess_username' => $this->isloggedin('logged_in'),
'id' => $this->input->post('id'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email')
);
$this->load->view('header');
$this->load->view('main/menu_super_admin',$data);
$this->load->view('user/modifuser');
$this->load->view('footer');
}
else
{
$query = $this->m_user->updateuser($this->input->post('id'),$this->input->post('username'),md5($this->input->post('password')),$this->input->post('email'));
if($query)
{
echo "<script>window.onload = function() { return alert(\" Update User Success ! \"); }</script>";
}
else
{
return false;
}
redirect('user/user', 'refresh');
}
}
else
{
redirect('login', 'refresh');
}
}
the problem is, i want to make the disabled input stay disabled and the values remains the same,
is there any mistakes on my code ?
Disabled inputs are not posted to the server: http://www.w3.org/TR/html401/interact/forms.html#disabled
... [a disabled input] cannot receive user input nor will its value be submitted with the form.
I suggest getting the ID from the session and not relying on the posted information in any way (This is a security concern because posted information can be manipulated by the end user). You already check to see if the user is logged in. Just get the ID from the the session while you're at it.
Session ID can be retrieved like so:
$data = array(
'sess_username' => $this->isloggedin('logged_in'),
'id' => $this->session->userdata('session_id'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email')
);
You may also need to load the library first
$this->load->library('session');
I also suggest using sess_use_database as mentioned in the docs for added session security.