I am quite new to PHP and CodeIgniter itself. I know that this error will be encountered when you don't load the form helper.
However, I have added in and still face this error.
Please take a look at my codings.
This is my View: Create
<?php echo form_open('studCred/Create'); ?>
<?php echo validation_errors(); ?>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Admin No</label>
<input type="text" class="form-control" name="adminNo" placeholder="AdminNo">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Phone number</label>
<input type="number" class="form-control" name="phone" placeholder="Phone">
</div>
</div>
</div>
<input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">
<p class="lead-footer">* We'll contact you by phone & email later</p>
</form>
This is my Controller: studCred
class studCred extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
this->load->model('studCredModeller');
}
//Register students
public function create() {
//load registration view form
$this->load->view('studCred/Create')
;
//set validation rules
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username_exists');
$this->form_validation->set_rules('adminNo', 'AdminNo', 'required|callback_check_adminNo_exists');
$this->form_validation->set_rules('email', 'Email', 'required|callback_check_email_exists');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
if ($this->form_validation->run() === FALSE){
$this->load->view('studCred/Create');
}
else
{
this->studCredModeller->saveRecords();
this->load->view('nypportal');
}
}
}
Model: studCredModeller
if (!defined('BASEPATH'))
exit ('No direct script access allowed!');
class studCredModeller extends CI_Model
{
public function saveRecords();
{
$this->load->helper('url');
$data = array(
'username' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'));
return $this->db->insert('stud_login', $data);
}
}
Thank you. Putting it into the config/autoload.php didn't work either.
Hope this will help you :
You have series of typo error in your code, so remove them one by one and then check again
create method should be Create, add $ to here this->studCredModeller->saveRecords(); and here this->load->view('nypportal');
You are also missing url helper in controller so use url helper in controller instead of using in model
Note : controller name should start with capital letter and must match with file name, and better use url helper and form helper in autoload.php
Your controller __construct() method should be like this :
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
this->load->model('studCredModeller');
}
And Remove ; after the method saveRecords(); in your model , should be like this:
public function saveRecords()
{
$this->load->helper('url');
$data = array(
'name' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone')
);
return $this->db->insert('stud_login', $data);
}
for more : https://www.codeigniter.com/user_guide/general/index.html
Related
I have a PHP Laravel CRUD application I made where I am using MVC style. I have controllers views and models. My database migration is made and my table in the database is made with php artisan migrate. I am using php 7.3 and laravel 5.8.
On my create view I go to create a single object in my database and my errors are thrown saying nothing in text box (no input) If I comment out the errors then just I click my submit button and nothing happens nothing is entered into my db. I have looked at many different crud examples and I am not sure why my object isn’t being created. Here is what I have
My env is setup correctly I just don’t get the not creating object.
//view create
#section('main')
<section id="section-content" class="text-center">
<div class="container contentdiv rounded">
<div class="row">
<div class="col-md-12">
<div class="pb-2 mt-4 mb-2 border-bottom clearfix">
<h2>Create Contact</h2>
</div>
<div >
<a class="btn btn-success" href="{{route('contacts.index')}}">Back</a>
</div>
</div>
<!-- <div class="col-md-10 mx-auto">
#if($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><br />
#endif
</div> -->
<div class="row">
<div class="col-md-10 mx-auto mt-3">
<form method="POST" action="{{ route('contacts.store') }}">
#csrf
<div class="form-group row">
<label for="txtfn" class="col-sm-3"><b>First Name:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtfn" id="txtfn"/>
</div>
</div>
<div class="form-group row">
<label for="txtln" class="col-sm-3"><b>Last Name:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtln" id="txtln"/>
</div>
</div>
<div class="form-group row">
<label for="txtem" class="col-sm-3"><b>Email:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtem" id="txtem"/>
</div>
</div>
<button type="submit" class="btn btn-primary">Create Contact</button>
</form>
</div>
</div>
</div>
</section>
//controller
namespace App\Http\Controllers;
use App\Contact;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function store(Request $request)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required'
]);
$contact = new Contact([
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'email' => $request->get('email'),
'job_title' => $request->get('job_title'),
'city' => $request->get('city'),
'country' => $request->get('country')
]);
$contact->save();
return redirect('/contacts')->with('success', 'Contact saved!');
}
public function index()
{
$contacts = Contact::all();
return view('contacts.index', compact('contacts'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('contacts.create');
}
// model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $fillable = [
'first_name',
'last_name',
'email',
'city',
'country',
'job_title'
];
}
Your problem is that your input names do not correspond to the keys you are referencing in your store() method:
for example:
<input type="text" class="form-control" name="txtfn" id="txtfn"/>
here, your input name is txtfn, but in your store method, you are looking for first_name.
'first_name' => $request->get('first_name')
so, $request->get('first_name') returns null as if you didn't pass any value.
You must make your input names match with the keys you are using in your store method, either by changing input names, or by changing key names.
example:
<input type="text" class="form-control" name="first_name" />
<input type="text" class="form-control" name="last_name" />
<input type="text" class="form-control" name="email" />
Form validation and record insert not working in codeigniter
model
class Mymodel extends CI_Model
{
function insert($data)
{
$this->db->insert("users", $data);
}
}
controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_controller
{
function index()
{
$this->load->view('myfolder/my_page');
}
function login()
{
$this->load->view('myfolder/login');
}
function signup()
{
$this->load->view('myfolder/signup');
}
function signupmethod()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("firstname","First Name",'requird|alpha');
$this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
$this->form_validation->set_rules("email","Email",'requird|alpha');
$this->form_validation->set_rules("password","Password",'requird|alpha');
$this->form_validation->set_rules("mobile","Mobile",'requird|alpha');
if ($this->form_validation->run())
{
$this->load->model("mymodel");
$data = array(
"firstname" => $this->input->post("firstname"),
"lastname" => $this->input->post("lastname"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"mobile" => $this->input->post("mobile"),
"curtime" => "NOW()"
);
if($this->input->post("Signup"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
}
}
public function Inserted()
{
$this->index();
}
}
?>
view(html code)
<?php include('header.php'); ?>
<form method="post" action="<?php echo base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h3>Login</h3>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>First Name</label>
<input type="text" name="firstname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("firstname"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("lastname"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Email</label>
<input type="text" name="email" value="" class="form-control">
<span class="text-danger"><?php echo form_error("email"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Password</label>
<input type="password" name="password" value="" class="form-control">
<span class="text-danger"><?php echo form_error("password"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" name="mobile" value="" class="form-control">
<span class="text-danger"><?php echo form_error("mobile"); ?></span>
</div>
<div class="col-md-6 form-group" style="margin-top: 23px;">
<input type="submit" name="submit" value="Signup" class="btn btn-info">
</div>
</div>
I have read every line carefully, code run but no errors showing and validation and record insert are not working. and no errors showing.
please help.
Change controller this block of code when you fetch form elements in controller always use the element name
if($this->input->post("submit"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
Correct spelling of required
$this->form_validation->set_rules("firstname","First Name",'required|alpha');
Your set_rules statements are incorrect. Use required instead of requird otherwise set_rules will fail.
When you are not sure why form_validation is failing, try getting the errors with:
If($this->form_validation->run() == false)
{
echo validation_error();
}
I have email form in my codeigniter, it is sending e-mails successfully i just cannot make it refresh page when the e-mail will be sent, if I send email and then refresh page manually it writes your e-mail has been sent successfully which is written in flashdata in my language, which means that everything is okay I just can not refresh page automatically, please help.
This is my contact Controller :
class Contact extends CI_Controller {
public function index()
{
$this->load->library('session');
$this->load->library('email');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title'] = "Contact";
$this->load->view('templates/header', $data);
$this->load->view('contact', $data);
$this->load->view('templates/footer', $data);
}
public function sendmail1(){
$this->load->library('email');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$name = $this->input->post('contact-name');
$email = $this->input->post('contact-email');
$subject = $this->input->post('contact-subject');
$phone = $this->input->post('contact-phone');
$message = $this->input->post('contact-message');
$this->form_validation->set_rules('contact-name', 'სახელი', 'trim|required');
$this->form_validation->set_rules('contact-email', 'ელ-ფოსტა', 'trim|required');
$this->form_validation->set_rules('contact-phone', 'ტელეფონი', 'trim|required');
$this->form_validation->set_rules('contact-subject', 'წერილის თემა', 'trim');
$this->form_validation->set_rules('contact-email', 'ელ-ფოსტა', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('warmatebulia', '<h5 style="color: red;">თქვენი წერილის გაგზავნა ვერ მოხერხდა.</h5>');
// after storing i redirect it to the controller
redirect(base_url().'contact', 'refresh');
return FALSE;
}
else {
$this->email->from($email, $name);
$this->email->to('info#mymail.com');
$this->email->subject($subject);
$this->email->message('ტელეფონის ნომერი:'.$phone.'<br />'.$message);
$this->email->send();
$this->session->set_flashdata('warmatebulia', '<h5 style="color: green;">თქვენი წერილი წარმატებით გაიგზავნა, მადლობა.</h5>');
// after storing i redirect it to the controller
redirect('', 'refresh');
}
}
}
This is my form on contact view page :
<?php echo $this->session->flashdata('warmatebulia'); ?>
<?php
$attributes = array('class' => 'form-message', 'id' => 'quote-contact-request');
echo form_open('contact/sendmail1', $attributes);
?>
<div class="form-results"></div>
<div class="form-group row">
<div class="form-field col-md-6 form-m-bttm">
<input name="contact-name" type="text" placeholder="სახელი *" class="form-control required">
</div>
<div class="form-field col-md-6">
<input name="contact-email" type="email" placeholder="ელ-ფოსტა *" class="form-control required">
</div>
</div>
<div class="form-group row">
<div class="form-field col-md-6 form-m-bttm">
<input name="contact-phone" type="text" placeholder="ტელეფონის ნომერი*" class="form-control required">
</div>
<div class="form-field col-md-6">
<input name="contact-service" type="text" placeholder="წერილის თემა" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="form-field col-md-12">
<textarea name="contact-message" placeholder="წერილი *" class="txtarea form-control required"></textarea>
</div>
</div>
<input type="submit" name="submit" value="გაგზავნა" class="btn solid-btn sb-h">
<?php echo form_close(); ?>
If you need to flash a success message in your contact page itself, you just redirect to index() function of your 'Contact' controller. So please do like this
$this->session->set_flashdata('warmatebulia', '<h5 style="color: green;">თქვენი წერილი წარმატებით გაიგზავნა, მადლობა.</h5>');
// after storing i redirect it to the controller
redirect('contact', 'refresh');
I need to get the data that has been stored in my database to print in my website.I want to input the user entered word and then store it in a table after that i want it to show on a page.I have already saved created a form that takes the user entered data and stores it in a table but have not been able to display the data.I want the data entered by the user be available in any file
Here is my View
<form class="form-horizontal"
action="<?php echo base_url() ?>index.php/submit/new_form_submit" method="post">
<fieldset>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="Title">Title</label>
<div class="col-md-4">
<input id="Title" name="Title" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Price">Price</label>
<div class="col-md-4">
<input id="Price" name="Price" type="number" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Describe your product</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Link">Link to Live preview</label>
<div class="col-md-4">
<input id="Link" name="Link" type="url" placeholder="e.g http://www.example.com" class="form-control input-md" required="">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Screenshot of your theme</label>
<div class="col-md-4">
<label for="file-input">
<!-- <div class="thumbnail">
<img src="<?/*= base_url() */?>Images/placeholder.jpg"/>
</div>-->
</label>
<input id="file-input" type="file"/>
</div>
</div>
<br>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="button1id"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-success">Save</button>
<a id="cancel" name="cancel" class="btn btn-danger" href="<?php echo base_url(); ?>index.php/home">
Cancel</a>
</div>
</fieldset>
Here is my Controller
<?php
session_start();
class Submit extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('security');
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Submit_Database');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
public function new_form_submit()
{
$this->load->helper('url');
$this->form_validation->set_rules('Title', 'Title', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
} else {
$data = array(
'Title' => $this->input->post('Title'),
'Price' => $this->input->post('Price'),
'textarea' => $this->input->post('textarea'),
'Link' => $this->input->post('Link')
);
$result = $this->Submit_Database->submit_insert($data);
if ($result == TRUE) {
$this->load->view('templates/header');
$this->load->view('pages/about');
$this->load->view('templates/footer');
} else {
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
}
redirect('');
}
}
Here is my model
<?php
Class Submit_Database extends CI_Model
{
function __construct()
{
parent::__construct(); // construct the Model class
$this->load->database();
}
// Insert registration data in database
public function submit_insert($data)
{
// Query to insert data in database
$this->db->insert('submit', $data);
}
}
Any help would be much appreciated!
The logic is this:
You need a function in model to select from the database.
A function in controller to call the model function and load the view with the data.
And a view file to actually display the data.
You can use sessions in order to store the data in a session variable and access it in any file. You have to load the session library.
I'm trying to send an email filled with a form from a Laravel app.
When you hit submit it throws the above error:
Fatal error: Class 'App\Http\Controllers\Input' not found
Not sure why as I don't have, nor knew I needed to have an Input controller, or what I would put in it.
Below is the content of the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class contact extends Controller
{
// This function will show the view
public function showForm()
{
return view('pages.contact');
}
public function handleFormPost()
{
$input = Input::only('name', 'email', 'msg');
$validator = Validator::make($input,
array(
'name' => 'required',
'email' => 'required|email',
'msg' => 'required',
)
);
if ($validator->fails())
{
return Redirect::to('contact')->with('errors', $validator->messages());
} else { // the validation has not failed, it has passed
// Send the email with the contactemail view, the user input
Mail::send('contactemail', $input, function($message)
{
$message->from('idocompscihw#gmail.com', 'Your Name');
$message->to('idocompscihw#gmail.com');
});
// Specify a route to go to after the message is sent to provide the user feedback
return Redirect::to('thanks');
}
}
}
Below is the view of the forum (based on bootstrap):
<div class="container">
<h1>A basic contact form</h1>
<form id="contact" method="post" class="form" role="form">
#if(Session::has('errors'))
<div class="alert alert-warning">
#foreach(Session::get('errors')->all() as $error_message)
<p>{{ $error_message }}</p>
#endforeach
</div>
#endif
<div class="row">
<div class="col-xs-6 col-md-6 form-group">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="form-control" id="name" name="name" placeholder="Name" type="text"autofocus="">
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="text">
</div>
</div>
<textarea class="form-control" id="message" name="msg" placeholder="Message" rows="5"></textarea>
<br>
<div class="row">
<div class="col-xs-12 col-md-12 form-group">
<button class="btn btn-primary pull-right" type="submit">Submit</button>
</div>
</div>
</form>
</div>
Used this in your contact.php Controllers-
use Illuminate\Support\Facades\Input;
your error will get fixed. Thanks.
Input:: is replaced with Request::. Instead of
$input = Input::only('name', 'email', 'msg');
use this:
$input = Request::only('name', 'email', 'msg');
And if you get error something about 'should not use statically' just add this at the top of your file
use Request;
If you already have this line:
use Illuminate\Http\Request;
delete it because you can't have two classes with the same name in one file
public function handleFormPost(Request $request)
{
$name = $request->get('name');
$email = $request->get('email');
$msg = $request->get('msg');
}
OR
public function handleFormPost(Request $request)
{
$input = $request->all();
}
you not use input try this :
use Input;
Put it after the namespace declaration like this
<?php
namespace App\Http\Controllers;
use Input;
...
?>