Mod Rewrite and search query string - php

To search something, user need to use following URL:
http://.../results/query/something
I got my text input, in which user type the string to be posted, but it looks then following:
http://.../results/query/?query=something
I've tried to change the 'post method' type, but doesn't bring any good results. How can I do that, so it'll look like that?
http://.../results/query/something

<form id="myform" method="post" action="/">
<input type="text" id="query" name="query" />
<input type="submit" name="submit" onclick="return go();" />
</form>
<script type="text/javascript">
function go(){
var query = document.getElementById('query').value;
document.getElementById('myform').action = 'http://yourdomain/results/query/'+query;
}
</script>

Simple answer:
Use method="post"
Construct the URL with PHP after the form is posted, using urlencode() and any other appropriate sanitization on the value.
Redirect to the page you want.
$query = isset($_POST['query']) ? urlencode($_POST['query']) : '';
header("Location: results/query/$query");

Related

Add query string to URL if there is none already

I have a question. My question title doesn't really describe what I need help with, but it's the same concept. This is what I want to do:
I am creating a search function (not open for other suggestions than the one I am trying to accomplish), and it's using query string. If I search for "Hello", I want my website to redirect to website.com?s=hello, but if there already is one, add it to the url with a &, like this: website.com?page=home&s=hello. I tried to use this:
if(isset($_GET)) { // Add the string with & before
else { // Add the string with a ? before
I am willing to use jQuery if I have to
More brief explanation:
I have a form, it has an input and a submit-button.
<form action="" method="post">
<li><input type="text" placeholder="Username"></li>
<li><input type="submit" value="Search"></li>
</form>
I am aware how to store everything in variables and such, but I am not sure how to add it as a query string to the url. Thank you!
A way to do it is to handle it in php in your form:
<?php
if(isset($_GET['page'])) {
$url = '?page=' . $_GET['page'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>`
Note that if you want to submit your form AND go to the page ?s=foo as a result of submission, you must have a method GET on your form and have an input with argument name="s".
EDIT: Or a bit more exhaustive, if you have several arg that could be passed in the URL it could be :
<?php
if(!empty($_GET)) {
$url = '?' . $_SERVER['QUERY_STRING'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>

Pass unencoded url from form

I've set up a search box on the homepage, that should link into the Opencart search by passing a URL.
The problem I'm having is that the resulting URL is always unencoded:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
What I'm aiming for is
store/index.php?route=product/search&filter_name=test
My current code is:
<?php
$storesearch = $_REQUEST['store-search'] ;
$filter = 'route=product/search&filter_name';
$filtered = html_entity_decode($filter);
?>
<form id="store" method="GET" action="http://localhost/vinmobile/store/index.php?<?php echo $storesearch; ?>">
<input type="text" id="store-search" name="<?php echo $filtered; ?>" value="Store Search" onclick="this.value = '';">
<input type="submit" name="" value="Search">
</form>
<?php echo $filtered; ?> // Just to show it onscreen to make sure it's written correctly
I've tried encode, decode, html_entity_decode, urldecode, rawurldecode...
I've tried the form method as GET, POST
I've tried the variable declaration as $_REQUEST, $_GET, $_POST
Every time, I still end up with the url looking like:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
I'm sure it's something simple I've missed, but I 'think' I've tried everything and would be very grateful if someone could point me in the right direction.
Cheers
webecho
Ended up using Javascript instead and it works perfectly:
`<script>
function process()
{
var url="store/index.php?route=product/search&filter_name=" + document.getElementById("filter").value;
location.href=url;
return false;
}
</script>
<form id="store" onSubmit="return process();">
<input type="text" name="filter" id="filter" value="Store Search" onclick="this.value = '';">
<input type="submit" value="Search">
</form>`
Thanks for your suggestions though

getting value from a javascript array and put it in php variable

I use daterangepicker() to get two dates ( begginning and ending ), I'd like to put those dates in php variables.. i've succeeded to do an alert() to show those variables using a button.. but i don't know how to put them in the php.
here's my code if anyone have an idea..
<script type="text/javascript">
$(function(){
$('#rangeBa, #rangeBb').daterangepicker();
});
function test(){
var dates = new Array();
dates[0] = document.inputdate.rangeBa.value;
dates[1] = document.inputdate.rangeBb.value;
return dates;
}
</script>
<body>
<form name="inputdate" method="post">
<input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
<input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
</form>
<button onclick="alert(test())">Click on this button</button>
</body>
You have to add a submit input element in your form and add an action parameter in your form:
<form name="inputdate" method="post" action="yourfile.php">
<input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
<input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
<input type="submit" value="Click to send" />
</form>
And in yourfile.php: you get the variables by $_POST['rangeBa'] and $_POST['rangeBb']
Feel free then to use ajax method if you don't want a refresh of the page.
After you submit your form you can find the form variables by name in $_POST. for example $_POST['rangeBa']

Get Search keyword in URL

<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query

Can i Mix $_get and $_post?

in order to edit my entries i want to:
<form id="pregunta" name="pregunta" class="form_pregunta" method="post" action="pregunta.php?id=26">
<h2>Titulo de la pregunta</h2><input name="q" id="q" class="q" value="este es mi títiulo " type="text">
<h2>Describe tu pregunta</h2>
<textarea name="texto" id="texto" style="width: 98%;"><p>esta es mi descripcion</p></textarea>
<h2>Etiquetas</h2>
<input name="tags" id="tags" onmouseover="mostrar_tooltip('nube_e','','0','70','')" onmouseout="ocultar_tooltip('nube_e')" value="dos,tres,una,">
<input name="responde_a" style="display: none;" id="responde_a" value="0">
<button name="pregunta" id="pregunta" type="submit">form_edit_question_button</button>
</form>
And then in file.php
i'd like to $_get['id'] and $_post['inputs']
but when i go:
if(isset($_POST['edit_pregunta'])){
echo 'lalalalalalalalalalalalalalala';
post_edit_pregunta();
}
it won't ever enter :S. is that normal or i'm missing something... i wanted not to have a hidden input with the id of the post i want to edit..
I'm not 100% sure, but forms don't send their name when submitted, much less their id.
You could do the following instead:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
<input type="hidden" name="edit_pregunta" value="anything">
... //inputs here
</form>
and your if should now enter.
It looks like you're checking for your form's "id" attribute. This is not sent when the form is submitted, only values in <input>, <select>, <textarea> and <button> are sent.
You should check for one of those.
Edit: Your button name is "pregunta", so that is the POST variable you should be checking for, eg
if(isset($_POST['pregunta'])){
Just to comment in general on mixing params in the form's "action" and inputs, you can mix them as long as the form method is "post". You cannot set GET params in the form's action and use the "get" method
<!-- Good -->
<form action="proc.php?id=123" method="post">
<input name="foo" value="foo">
<input type="submit">
</form>
<!-- Bad -->
<form action="proc.php?id=123" method="get">
<input name="foo" value="foo">
<input type="submit">
</form>
There should be no problem at all with having get and post variables in the same request, but are you sure your syntax is correct? If this is normal php, shouldn't you write
<form id="edit_pregunta" method="post" action="file.php?id=<?php echo $this->id; ?>">
... //inputs here
</form>
[Edit]
The problem is (if I'm correct and this is standard php) that you generate a form that looks something like this:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
... //inputs here
</form>
This will make id look like this: '$this->id' (including the '-signs). When what you want is something like this:
<form id="edit_pregunta" method="post" action="file.php?id=51">
... //inputs here
</form>
Then $_GET['id'] would be 51.
[Edit2]
Also, I think you need to change
if(isset($_POST['edit_pregunta'])){
with
if(isset($_POST['pregunta'])){
If I'm not mistaken the name of a form doesn't get sent to the server, however, the name of the submit-button does, but I might be wrong about that part.
Yes you can, I've done it several times.
Probably something else is wrong with your code.
Is there any control with name="edit_pregunta" or is it just the id of the form? IDs are not sent to the server.
Simply adding the id to the form will not create the $_POST['edit_pregunta'] you verify.
Instead, inside the form tag, add an <input name="foo" />; in the php script verify $_POST['foo']
While the HTTP spec doesn't disallow query parameters in POST methods, it is somewhat unusual. You'd be better off using a hidden input field in the form to pass any non-user values up to the script.
That said, the syntax for your form is wrong. You need to use "echo" to insert the value of $this->id into the action.
Use input type="submit" in place of button tag.
You need name for form submission and activate php script!
HTML Code:
<form action="change.php" method="POST">
<input type="password" name="p1" class="change_text" placeholder="New Password"/></br>
<input type="password" name="p2" class="change_text" placeholder="Re-Password"/></br>
<input type="submit" name="change" value="Change Password" id="change" />
</form>
PHP Code:
<?php
if (isset($_POST['change']) {
$p1=$_POST['p1'];
}
?>

Categories