An Error Was Encountered in CI forms - php

Iam a newbie in Codeigniter , Iam learning it from watching videos , the instructor did the same what I did , But it gives me an error like this "The action you have requested is not allowed." ,and it worked with him, I don't know why, any help ! .
this is my Controller code
public function index(){
if($this->input->post('submit')){
echo $this->input->post('first_name');
}
$this->load->view('forms');
}
this is my View code
<form method="POST">
<input type="text" name="first_name" />
<input type="submit" name=submit" />
</form>

Use form_open() helper which automatically adds hidden input with CSRF value.
So your view should be:
<?php echo form_open('action_url'); ?>
<input type="text" name="first_name" />
<input type="submit" name=submit" />
<?php echo form_close(); ?>
Disabling CSRF protection also works but it's a bad idea.

you almost right, you need to add some parts like action in your form, and isset or empty in your controller like
class Test_form extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('form_test');
}
//using your example. good
public function check_form(){
if( isset($this->input->post('first_name', TRUE))){
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
else{
echo "error";
}
}
//using form_validation. best
public function check_form_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'first Name', 'trim|required|xss_clean');
if( ! $this->form_validation->run()){
echo "error <br>" . validation_errors();
}
else{
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
}
}
form_test.php
first method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>
<hr>
second method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form_validation">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>

Related

form_open() method not working that no form has been shown

This is my view/login/loginform.php
<?php echo form_open('scheduler/login');
?>
<label for="username">username</label>
<input type="text" name="username"/><br/>
<label for="password">password</label>
<input type="password" name="password"></input><br/>
<input type="submit" name="submit" value="log in" />
</form>
This is my controllers/Scheduler.php
<?php
class Scheduler extends CI_Controller {
public function login()
{
$this->load->helper('form');
echo"not logged in";
$username=$this->input->post("username");
$password=$this->input->post("password");
if($username=="123" && $password="abc"){
$data['username']=$username;
$this->load->view('login/login_success_message',$data);
//$this->input->set_cookie('123','abc','15');
// redirect('home');
}
else {
$this->load->view('login/login_failure_message');
//$this->load->view('login/loginform');
}
}
}
?>
However, there is no form shown when I go the http://localhost/project/scheduler/login
//$this->load->view('login/loginform');
You are not loading the login form. So, Uncomment the above line
$this->load->view('login/loginform');
if the problem is still same then try to call $this->load->view('login/loginform');out side if else statement

Form submit in opencart

I'm new to opencart. I have to write a custom Log-in form for users. Then i design a small code for log-in form in opencart like below. path is (MyTheme/temlate/auth/Sign.tpl)
<form action="<?php echo $Sub; ?>" method="GET" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
and controller is like (Path is controller/auth/Sign.php)
<?php
class ControllerAuthSign extends Controller{
public function index() {
$data['Sub']=$this->url->link('auth/result','','SSL');
if(file_exists(DIR_TEMPLATE . $this->config->get('config_template'). '/template/auth/sign.tpl')){
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/sign.tpl',$data));
}
else{
$this->response->setOutput($this->load->view('default/template/account/login.tpl'));
}
}
}
?>
when a user submit the form have to navigate to Result page (Path is /auth/result.tpl)
<?php
echo "Welcome : Mr./Mrs. ".$User;
?>
<br><p>Your are Loged-In</p>
and the controller for Result is.. (Path is /auth/result.php)
<?php
class ControllerAuthResult extends Controller{
public function index() {
$data['User']=$_REQUEST['txtUser'];
$data['Password']=$_REQUEST['txtPassword'];
if(isset($data)){
$this->response->redirect($this->url->link('auth/sign', '', 'SSL'))
}
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/result.tp',$data));
}
}
?>
but the problem is when i click on submit , page navigate to
http://localhost/opencart/index.php?txtUser=Narayana&txtPassword=narayana
and displayed index page. Can any one help how to navigate to result page...?
Thanks in Advance.
Use this
<form action="<?php echo $Sub; ?>" method="POST" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">

PHP My code doesn't send to the right page after using edit button

I have the following problem. I created a .php file that creates(extends) two new pages. In the first page there is a button called submit which after click on it send me to the next page called editname. When I made click on the button (edit)of the second page, it send me to the third page called recorndname. In the third page, there are two buttons called update and delete which send me to the second page. But, actually it is working as it should. Here is part of my code.
//here is my main class
class main {
public function __construct() {
$page_request = 'homepage';
if(isset($_REQUEST['page'])) {
$page_request = $_REQUEST['page'];
}
$page = new $page_request;
if($_SERVER['REQUEST_METHOD'] == 'GET') {
$page->get();
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST'){
$page->post();
} else{
$page->delete();
}
}
}
Here is my page class. In this part seems neither one of the functions are called.
//Page class
class page {
public function get() {
echo 'I am a get method page';
}
public function post() {
echo 'I am a post method';
$user=new users();
//$user->addUser($_POST["firstname"],$_POST["lastname"],$_POST["email"]);
if($_POST['botton'] == 'Submit'){
$user->addUser($_POST["firstname"],$_POST["lastname"],$_POST["email"]);
} else if($_POST['botton'] == 'Update'){
$user->update($_POST["email"],$_POST["firstname"],$_POST["lastname"]);
} else if($_POST['botton'] == 'Delete'){
$user->deleteUser($_POST["email"]);
}
}
public function delete(){
echo 'I am a delete method';
}
}
Here are my extended pages.
//Page classes
class homepage extends page {
public function get() {
echo 'homepage';
echo'<form action="projectBackup.php?page=editname" method="post">
First name:<br>
<input type="text" name="firstname" value="">
<br>
Last name:<br>
<input type="text" name="lastname" value="">
<br>
Email Address:<br>
<input type="text" name="email" value="">
<br><br>
<input type="submit" name="button" value="Submit">
</form>';
}
}
class editname extends page {
public function post(){
echo 'editname';
$users=new users();
$userlist=$users->listUsers();
echo '<table><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>';
foreach($userlist as $Kuser=>$user){
echo '<tr><td>'.$user['fname'].'</td><td>'.$user['lname'].'</td><td>'.$Kuser.'</td><td>';
echo '<form action="projectBackup.php?page=recordname" method="get">
<input type="hidden" name="email" value="$Kuser">
<input type="submit" value="Edit"></form>';
}
echo '</table>';
}
}
class recordname extends page {
public function get() {
echo 'recordname';
$users=new users();
$userlist=$users->listUsers();
echo'<form action="projectBackup.php?page=editname" method="post">
First name:<br>
<input type="text" name="firstname" value="$userlist[$_GET["email"]["fname"]">
<br>
Last name:<br>
<input type="text name="lastname" value="$userlist[$_GET["email"]["lname"]">
<br>
Email Address:<br>
<input type="text" name="email" value="$_GET["email"]">
<br><br>
<input type="submit" name="button" value="Update">
<input type="submit" name="button" value="Delete">
</form>';
}
}
Can someone help me with this? Thank you in advance.

Unable to insert data into database (PHP MVC)

I want to allow people to submit their contacts by entering it into a field and it would eventually send it to the database but been trying it for quite some time and cant find out whats wrong with it, if possible do let me know how to return a echo saying "SENT" when its in database. Redirecting to the same page, if possible , dont even need a refresh to get that SENT shown to people.
This is my Controller
$this->config->db_config_fetch();
$this->load->model('skills_model');
//Get Form Data
$this->input->post('submitsms') ;
//GEt phone number from field
$type = $this->input->post('phonenumber');
//Call model
$this->skills_model->addsms($type);
}
This is my View # home page
<form method="post" action="<?php echo site_url(''); ?>" name="form" enctype="multipart/form-data">
<label>SMS Subscription</label>
<input type="text" name="phonenumber" placeholder="Phone Number here">
<button class="btn btn-info" name="submitsms" type="submit">Subscribe</button>
</form>
This is my Model
function addsms($type){
$this->db->set('number', $type);
$this->db->insert('subscribers');
}
I also tried the following
function addsms($type){
$sql = "INSERT INTO 'subscribers' ('number') VALUES ($type)";
$this->db->query($sql);
echo $this->db->affected_rows();
}
You advice would be of a great help! thank you!
A small example ...........
View file
<form action="<?php echo ROOT_FOLDER ?>/add_price" method="post">
<input type="text" class="text" name="amount" value="<?php echo set_value('amount'); ?>" />
<input type="submit" class="submit" value="Approve" />
</form>
Controller...........
public function post_add_price()
{
$data = array(
'amount'=>$this->input->post('amount'),
);
$this->model->add_amount($data); //sending amount to model to insert in dataabse
echo "Amount added to database";
}
Model................
public function add_amount($data)
{
$this->insert_helper('order_amount_table',$data);
}
public function insert_helper($table_name, $data_array){
$this->db->insert($table_name,$data_array);
return $this->db->insert_id();
}
I hope this example will help you .........if you have any doubts ask

View rendering as blank in Zend Framework action

<?php
class SignupController extends Zend_Controller_Action
{
function indexAction()
{
if ($this->_request->isPost())
{
echo "Your email address is: " . $this->_request->getPost('email');
}
$this->render("signup");
}
}
This is my controller
this is my view
<html>
<body>
<form action="/signup" method="post">
Email: <input name="email" type="text" />
<input name="btnSubmit" value="Click me" type="submit" />
</form>
</body>
</html>
when I tried to run the view
I am getting result
blank/signup
why this happens
please rectify that error, and tell me the reason for that error
If you are using signup view then replace your render code from this :-
$this->_helper->viewRenderer('signup');
Try it with else:
if ($this->_request->isPost()) {
echo "Your email address is: " . $this->_request->getPost('email');
} else {
$this->render("signup");
}

Categories