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
Related
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.
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
I have search country form with code:
<form name="form_search" id="form_search" class="form1" method="get" action="country/search"><div class="row">
<div class="col-sm-4 col-md-7">
<label>Select country</label>
<div class="select1_inner">
<div class="form-group">
<select class="select2 select" style="width: 100%" name="q" id="q">
<?php
$sql = "SELECT * FROM country";
$query = $conn->query($sql);
while ($row = $query->fetch_assoc()) :
$country_name=$row['country_name'];
?>
<option value=<?php echo $country_name?>><?php echo $country_name?></option>
<?php endwhile;?>
</select>
</div>
</div>
</div>
At the frontend url will show : http://example.com/country/search?q=parameter
but I would redirect that url to real directory at the backend http://example.com/page.php?module=search&id1=$1
with .htaccess
RewriteEngine On
Options -Indexes
rewriteRule ^country/search$ page.php?module=search&id1=$1 [L]
But this is not working I can't get value q parameter
Please help me how configure .htaccess for this form
Thanks.
You should use RewriteCond backreferences:
RewriteCond %{QUERY_STRING} q=([^&]*)
RewriteRule ^country/search$ page.php?module=search&id1=%1 [R=301,L]
Line 1: test if query string containing q={any_characters}
Line 2: if the url is country/search, redirect to page.php?module=search&id1={any_characters} permanently
I have following html form :
<div class="row form">
<div class="col-md-offset-3 col-md-6 col-md-offset-3">
<form class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
<div class="form-group col-md-10 no-p-m">
<select class="form-control" id="basic" name="location" required>
<?php
$get_location = mysqli_query($conn, "SELECT * FROM product_sub_area");
if(mysqli_num_rows($get_location) == 0 ) {
$choose = 'No City found';
} else {
$choose = 'Choose City';
}
?>
<option value=""><?php echo $choose; ?></option>
<?php
while($get_location_result = mysqli_fetch_array($get_location) ) {
$location_id = (int) $get_location_result['psub_area_id'];
$location_name = htmlspecialchars($get_location_result['psub_name']);
echo "<option value='$location_id'>$location_name</option>";
}
?>
</select>
</div>
<div class="form-group col-md-2 no-p-m" id="basic">
<input type="submit" class="btn btn-default filter-search" value="BROWSE">
</div>
</form>
</div>
</div>
When I submit this form the url is showing like bellow :
http://localhost/freelancer/happiechef/location?location=1
But I need to show this type of url :
http://localhost/freelancer/happiechef/location/carlifonia
here /location/ is a page called location.php. I am hiding page extension using .htaccess.
So from location.php page I want to get $_GET['location'] value = $location_id
for eg.
$_GET['location'] = 1 or 2 ( select tag option value)
Existing .htaccess code
ErrorDocument 404 not-found.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/([\w-]+)$ location.php?location=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Here is an example of how this can be accomplished in jQuery:
First, add an id to your <form> tag so you can access it easily:
<form id="formname" class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
You also have a duplicate id called "basic". Make sure you leave it only on the <select> tag and remove the other one.
Then in your JS:
$("#formname").submit(function(event) {
event.preventDefault();
var loc = $("#basic option:selected").text();
window.location.href = '/freelancer/happiechef/location/' + loc;
});
This will forward the user to the URL, for example http://localhost/freelancer/happiechef/location/carlifonia. Then .htaccess will rewrite this URL to location.php?location=california.
I want "nice urls" for my website. I'm looking at using .htaccess and I have tried many versions.
Now I have this:
IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
So this removes index.php from the URL. At first it was ok, but
Here is my config.php:
$config['base_url'] = 'localhost/mysite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
And my .htaccess file is in my app root.
folder img
I need a login for admin page, so localhost/mysite/admin is the my url for admin site.
public function index()
{
// $this->session->unset_userdata('logged_in'); session_destroy();
$data['sitename']="Varkocs Old Pub";
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('admin/home_view', $data);
}
else
{
//If no session, redirect to login page
//redirect('login', 'refresh');
$this->load->helper(array('form', 'url'));
$this->load->view('admin/login',$data);
}
}
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('', 'refresh');
}
This is my admin controller. I try here if logged in I load home_view, else the login view.
<div id="" class="container-fluid ">
<div id="" class="row">
<div id="" class="col-md-4 text-center admin_container">
<h2><?php echo $sitename;?><h4>belépés az admin felületre</h4></h2>
<?php echo validation_errors('<p class="bg-danger">','</p>');?>
<form action="verifylogin" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="username">Username:</label>
<input class="form-control" placeholder="" type="text" size="20" id="username" name="username"/>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input class="form-control" placeholder="password" type="password" size="20" id="passowrd" name="password"/>
</div>
<input class="btn btn-warning btn-lg btn-block" type="submit" value="Login"/>
</form>
</div>
</div>
This is the login view. I can't use here:
echo form_open('verifylogin');
Because it's returning the wrong URL.
So I write HTML:
<form action="verifylogin" method="post" accept-charset="utf-8">
The form validation with CodeIgniter is working. But the redirect afterwards doesn't work.
$this->load->helper(array('form', 'url'));
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$data['sitename']="Varkocs Old Pub";
$this->load->view('admin/login',$data);
}
else
{
//Go to private area
redirect('admin', 'refresh');
}
I have tried many ways to fix this, but I failed.
The Codeigniter documentation says the following about the redirect function:
In order for this function to work it must be used before anything is outputted to the
browser since it utilizes server headers.
This rule can be tricky to follow. A space or line end before the opening PHP tag will already break it.