When I try to send my form with the base_url() method (so I click on the submit button), instead of the result of my request, I have a 403 error on the other page (forbidden acces), on the page generated by my controller.
I use a Wampp Server, the version 3.1.8 of CodeIgniter. I've also add url in the autoload file.
I have .htacess file with that :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
View page :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="<? echo base_url();?>form_validation">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Can you help me ? Thanks a lot
Hope this will help you :
NOTE : Assuming that default controller is welcome in route.php if not replace welcome with yours
In controller : you have method form_validation and you give in form action only validation :
public function form_validation()
{
echo 'OK';
}
Your form view is just like this :
REPLACE :
/* note missing php error in action*/
<form method="post" action="<? echo base_url();?>form_validation">
With :
/*add controller name also in action,
assuming your default controller is Welcome*/
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
View should be like this :
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
route.php should be like this :
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
NOTE ALSO : .htaccess should be in project folder , in your case CI folder
Related
I am working on a search display, but got some problems with the htaccess, not working the way I want it to. I got multiple rewrites on the same url and I assume thats why. But i am not sure..
UPDATE: From what I can see by testing, all the Rewrite urls work. But the form force the page over to search.php?search=test and not search/test as i want.
So the real question is:
How do I get form action="" to submit to search/test/ and not search.php?search=test
What i get:
search.php?page=1
search.php?type=DEFINED_TYPE&search=test
search.php?type=DEFINED_TYPE&search=test&page=1
What i want:
search/1
search/DEFINED_TYPE/test
search/DEFINED_TYPE/test/1
Form:
<form action="" method="get" id="searchForm">
<label for="DEFINED_TYPE_radio">
<input type="radio" name="type" id="DEFINED_TYPE_radio" value="DEFINED_TYPE"<?php if($_GET['type'] == 'DEFINED_TYPE') echo ' checked' ?> />
<em<?php if($_GET['type'] == 'DEFINED_TYPE') echo ' class="selected"' ?>><i class="fas fa-tags"></i>Type</em>
</label>
<div class="input">
<input type="text" placeholder="Search here..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>" name="search" id="searchField" autocomplete="off" autofocus>
<i class="fa fa-search submit" id="submit"></i>
</div>
</form>
Htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^search$ search.php [NC]
RewriteRule ^search/([^/]+)$ search.php?page=$1 [NC]
RewriteRule ^search/([^/]+)/([^/]+)$ search.php?type=$1&search=$2 [E=ORIG_URI:/$1]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)?$ search.php?type=$1&search=$2&page=$3 [E=ORIG_URI:/$1]
Anyone who can spot what I do wrong?
Note that as you are using get method on the form, every field is added with its name to the url as a param.
I suggest you submit the form with method post and add the url in the action attribute (or the whole url):
<form action="search.php?search=test" method="post" id="searchForm">
or
<form action="/search/test" method="post" id="searchForm">
That involves changing how you read those values
I have a HTML form containing a textbox and a submit button. When I submit the form, I want textbox value appending to the URL.
For example, if my domain is http://www.example.com/ and form lies on index.php. Suppose textbox value is "test" then when the form is submitted the URL should change to http://www.example.com/test. How can we achieve it?
HTML Code:
<form method="post" action="">
<input type="text" name="txtname" value="<?php echo $name; ?>" />
<input type="submit" name="btnsubmit" value="Submit" />
</form>
PHP Code:
<?php
$name = NULL;
if(isset($_POST['btnsubmit']))
{
$name = $_POST["txtname"];
}
?>
.htaccess Code:
# .htaccess mod_rewrite
# example.com
#Enable mod rewrite
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
How about a jQuery solution? Something like...
$(document).ready(function(){
var txtname = $('#txtname').val();
var url = window.location.href;
var new_url = url + "/" + txtname;
$('#formid').on('submit', function(e){
e.preventDefault();
// Do what you need to do here.
console.log(new_url);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formid" method="post" action="">
<input id="txtname" type="text" name="txtname" value="testname" />
<input type="submit" name="btnsubmit" value="Submit" />
</form>
You could use header:
$name = $_POST['txtname'];
header('Location: http://www.example.com/$name');
If you want to get the value without the error you can set the variable to empty like this:
$name = isset($_POST['txtname']) ? $_POST['txtname'] : '';
Place that after your opening php tag.
I am running into this problem with codeigniter, where it loads the index() page perfectly, but when it comes to any other function, it just throw me an 404 error. Here is a skeleton code :
<?php if( ! defined('BASEPATH')) exit('No direct script access alloowed');
Class User extends CI_Controller{
var $err;
public function construct(){
parent::_construct();
session_start();
$this->load->database();
$this->load->model('User_Model');
}
public function index(){
if(isset($this->session->userdata['userID'])){
$this->welcome();
}else{
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('login',$data);
}else{
$this->load->view('login');
}
$this->load->view('footer');
}
}
public function test(){
echo "Hello World";
}
public function welcome(){
$this->load->view('test');
}
public function register(){
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('register',$data);
}else{
$this->load->view('register');
}
$this->load->view('footer');
}
public function registration(){
//registration ops
}
public function login(){
//login ops
}
}
?>
So, when I am trying to navigate to www.example.com/index.php/user , it runs perfect, but when I try to let it run register, or do a login operation, it just throws an 404 error. Is there any solution to this?
Here is a html code generated using with index():
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="http://localhost/baseball/css/login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<p><img src="http://thumbs.dreamstime.com/z/cartoon-baseball-player-vector-illustration-30999087.jpg" height="250" alt=""/>Baseball Card Shop</p>
<hr />
<p class="loginInfo">
Shopping Cart
</p>
<hr/>
</div>
<div class="central_login">
<div class="main">
<form action="http://localhost/baseball/index.php/user/login" method="post" accept-charset="utf-8"> <div class="left">
<p>
<label for="login"> Login: </label>
<input type="text" name="login" id="login" pattern="^[a-zA-Z0-9]+$" required/>
</p>
<p>
<label for="pwd">Password: </label>
<input type="password" name="pwd" id="pwd" required/>
</p>
<p><label id='pwdErr'></label>
<input type="submit" value="Log in" id="create-account" class="button"/>
</p>
</div>
</form> <div class="right">
<form action="http://localhost/baseball/index.php/user/register" method="post" accept-charset="utf-8"> <p>Not our customer, worry not ! Register here, it is free!</p>
<input type="submit" value="Register HERE!" id="register" class="button"/>
</form> </div>
</div>
</div>
<footer>
<p>CSC309 Assignment</p>
<p>Baseball Web Application</p>
</footer>
</body>
</html>
Thanks a bunch!
You need to change your .htaccess. The following .htaccess will help you:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And need to change the following configure in config.php:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
There are a couple of things that are drawing attention in your code:
First, the use of var key word. It was used to declare class members in php4.
It is deprecated in php5 and greater version.
It will work. But raise a warning and is a bad practice.
Second. From the code you have posted, I believe, you have not auto loaded Session library.
Change the session_start() code in your constructor to :
$this->library->load('session');
Third thing I noted is this line of code:
$this->load->view('banner','Login');
The view functions of the loader class takes parameters in the below format.
$this->load->view('file_name', $data, true/false);
The syntax that you are might not be correct.
If you are planning to load banner and login file, you should do this:
$this->load->view('banner');
$this->load->view('Login');
On a side note, using native php sessions and CI sessions might be confusing and also may raise conflict in some situations.
I'm trying to validate a form that's being submitted through a PHP router.
All my traffic is sent to index.php via htaccess. to index.php:
RewriteEngine On
RewriteBase /router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /divbase/index.php?/$1 [L]
index.php then provides the appropriate content:
<?php
Router::get('/form', function() {
include 'content/form.php';
});
?>
so when you go to example.com/form, the form page is loaded:
<?php
$errorMessage = false;
if (isset($_POST['submit'])) {
if (!isset($_POST['name']) || $_POST['name']=='') {
$errorMessage = 'Please enter your name';
}
else {
// do something with the data
echo "Success!!";
}
}
?>
<form method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>
However, when I try to submit the form on that page it 404s. After doing some troubleshooting, it looks like the PHP thinks that the form is being submitted to index.php rather than form.php.
I've tried to set the form action to:
<?php "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
But it still 404s. Does anyone have any suggestions for this?
You need to add another route for POST, for example...
Router::get('/form', function() {
include 'content/form.php';
});
Router::post('/form', function() {
echo 'Hello world';
});
Your form is using method="post" so you need to add a POST route
I have a simple form built in a CodeIgniter view:
<html>
<head>
<title></title>
<base href="<?php echo site_url(); ?>" />
</head>
<body>
<?php echo form_open('main/testformout'); ?>
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
EDIT: Here is the HTML output of the form above:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="http://www.mypage.com/main/testformout">
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I have tried it with and without the form helpers but in this example I am using the form_open helper.
When hitting the submit button no POST data makes it to the Controller.
Controller Code:
public function helperin()
{
$this->load->view('test/helperin_v', NULL);
}
public function testformout()
{
print_r($_POST);
echo "<br />";
echo $_SERVER['REQUEST_METHOD'];
echo "<br />";
var_dump($this->input->post());
$this->load->view('test/out_v', $viewData[$_POST]);
}
When the system and application folders are loaded at the same level as the public_html folder I get this result:
GET
bool(false)
When the system and application folders are loaded inside the public_html folder (I know this is not secure) I get this result:
Array ( )
GET
bool(false)
Also, the code works perfect on my local machine but does not work on when uploaded to Bluehost server.
Here is my htaccess file (because I've done enough research to know this may be of value).
RewriteEngine On
RewriteCond $1 !^(index\.php|public|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Bluehost service people are saying it is because of a redirect issue and they consider the matter closed but as I am not doing a redirect that leaves either the CodeIgniter framework or their server.