Decode a search string in the url - php

I have a search text input. When i submit the searching, the text will get into the $_GET, and i will land on a search.php site.
function search_k()
{
return""!=$.trim($("#country_id").val())&&
(document.location="/kereses?k="+$("#country_id").val())
}
If i type in this: some test text
The url will be like this: kereses?k=some%20test%20text
How can i replace the %20 with a + mark? My problem is, that when i search for a product, that name has a + mark, i didnt get any result.
If i echo $_GET['k'] with php, that replaces the + mark to empty whitespaces. I have a real_escape_string function on the $_GET['k'].
UPDATE:
It still dont work, no changes. I always get the %20 in the GET. If i alert the word var, i get the right text.
function search_k()
{
if($.trim($('#country_id').val()) != "" )
{
var word = encodeURIComponent($('#country_id').val());
document.location = '/kereses?k='+word;
}
return false;
}

You need encodeURIComponent(); javascript function
var x= $("#country_id").val(); //get field value
x = encodeURIComponent(x); // encode it before passing
(document.location="/kereses?k="+x); // pass your field value
that's it

Related

Replace % with - in url PHP

Ok i have one maybe stupid question
On my website i have search options, that is input with GET metod, but when someone enter long seperated word like
I AM SOMETIMES BLANK
i got this in my url
http://www.example.com/search.php?page=1&txtPretraga=I%AM%SOMETIMES%BLANK
I dont know how to change that?
I want clean URL like this
http://www.example.com/search.php?page=1&txtPretraga=I-AM-SOMETIMES-BLANK
I want to change % with - in my ULR
Any ideas?
You can use str_replace in your php code:
http://php.net/manual/en/function.str-replace.php
$search_qry = "Whatever%They%Type";
$replace = str_replace($search_qry, "%", "-");
EDIT:
In the case of your strings - they have spaces, which show up as % in a URL, so use this before you do your $_GET
$search_qry = "Whatever They Type";
$replace = str_replace($search_qry, " ", "-");
EDIT 2
Since this is a $_GET - Javascript will have to be used to clean the string before it's sent. (using jQuery and javascript here)
<script type = "text/javascript">
$(document).ready(function(){
$('.example-input').blur(function(){
var str = $(this).val();
var clean_str = str.replace(" ", "-");
$(this).val(clean_str);
});
});
</script>
This should clean the string in the input box before it's even sent through the get.
or instead of .blur, you can use $('submit-button').click(function(){...
Or, you can use the .htaccess file to do a mod rewrite. But I don't know that well enough.
Ok, i got the fix for the problem :)
This is not question for PHP, it is for Javascript and i wrote this function
<script type="text/javascript">
function provera() {
var x=document.forms["hsearch"]["txtPretraga"].value;
var n=str.replace(" ","-");
}
</script>

Use JavaScript to prevent spacebar?

I have a live search on a site I'm developing. At the moment, it searches the MySql database after the first character is typed, and updates the search for each new character. When Space is pressed as the first character, it displays all entries in the database. I don't want it to do that. I have the following code that I found somewhere that prevents the SPACE character from being typed:
$('input[type="text"]').keydown(function(e){
var ignore_key_codes = [8,32];
if ($.inArray(e.keyCode, ignore_key_codes) >= 0){
e.preventDefault();
}
});
This does what it's meant to do, but not exactly what I want. This will prevent the space bar from working in the text input at all, what I require is that it only prevents the space bar if it's the first character being typed. For example, typing " apples" would prevent the space, but typing "apples oranges" wouldn't.
Is there anything I can try to achieve this?
You can do it easily like this example:
function keyPress(e) {
var e = window.event || e;
var key = e.keyCode;
//space pressed
if (key == 32) { //space
return;
}
searchInDataBase(); //make your ajax call to search in data base etc.
}
If the space key is pressed, you return and dont do nothing, otherwise you continue in your search...
$('input[type="text"]').keydown(function(e){
if (e.keyCode == 32 && $.trim($(this).val()) == ''){
e.preventDefault();
} else {
alert("searching..");
}
});
Demo: http://jsfiddle.net/BerkerYuceer/JJG9M/
Following on from #Florent's comment, when a user types a value into your textbox,check if the trimmed value is != '' if so it should be safe to continue
e.g
if($.trim($txtbox.val()) != ''){ // proceed to do search
}

Send Get with jQuery with spaces

I have a problem with a word count using the jQuery. The method is working as soon as I click space it will stop.
HTML:
<textarea id="essay_content_area" name="essay_content" onkeydown="words();"></textarea>
<td>Number of words: <div id="othman"></div></td>
jQuery:
function words(content)
{
var f = $("#essay_content_area").val()
$('#othman').load('wordcount.php?content='+f);
}
PHP file:
if(isset($_GET['content']))
{
echo $_GET['content']; // if it works I will send this variable to a function to calculate the words
}
the script shows the content until I click space. any suggestions ?
You need to url-encode the value before sending it over to your PHP script as the value of a GET parameter. Consider this:
function words(content)
{
var f = $("#essay_content_area").val()
$('#othman').load('wordcount.php?content=' + encodeURIComponent(f));
}
You don't need php to count the words you can use JS, something like this:
function words(content)
{
// Get number of words.
var words = content.split(" ").length;
}
You need to url encode your variable before you send it (space is not a valid url character):
function words(content)
{
var f = encodeURIComponent($("#essay_content_area").val());
$('#othman').load('wordcount.php?content='+f);
}

javascript redirect won't work?

I have a little bit of javascript code that is getting some parameters from php. the php variable $rid is an integer with a value within 1-100. what i am trying to do is change the value of the 'rid' variable in the querystring by a specified amount (offset). when i remove the last str($rid) characters from window.location.href, i am left with http://www.qwerty.asdf/uiop?rid=. When I add <?php echo $rid;?>+offsetto it I get http://www.qwerty.asdf/uiop?rid=2 for example. when I try the code, nothing happens, but I've debugged and str is the desired value by the time I say window.location.href=str;
why doesnt the page redirect? I have tried window.location as opposed to window.location.href, but it doesn't work.
p.s. there is no other javascript on the page
function moveRid(offset) {
str = window.location.href.substring(0,window.location.href.length-<?php echo strlen($rid);?>);
rid = <?php echo $rid;?>+offset;
str += rid;
window.location.href=str;
}
You need to actually set the window.location
function moveRid(offset) {
str = window.location.href.substring(0,window.location.href.length-<?php echo strlen($rid);?>);
rid = <?php echo $rid;?>+offset;
str += rid;
//window.location.href=str;
window.location = str;
}

Using hash plus querystring as an anchor for jquery fueled webpage?

Here is my situation. I have a webpage (not quite finished):
http://grapplingbasics.com/beta.php
I have the page slide to a specific div which puts a a url with a hash in the URL bar. If the user refers to this URL they can hit that specific part of the page.
However, I would like to allow them to hit that part of the page AND load a specific video with one address.
It doesn't seem that you can put a query string and hash dealy together like so: www.blah.com/index.php#BLAH?neat=one
originally i tried turning the hash into part of the querystring, and then using split in jquery to assign it into a hash on the fly. However, the problem with this, is that if I return false to the nav, it wont show the querystring on the URL bar, and if i don't return false, then it wants to navigate to something that isnt there!
What's a possible solution here?
I created a little example for you which should explain how you can get this fixed.
<script type="text/javascript">
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function () {
$('a').click(function (event) {
var thisHash = document.location.hash;
$('body').animate({ scrollTop: $(thisHash).offset().top }, function () {
if (getParameterByName('neat') != null) {
alert('I will play video ' + getParameterByName('neat'));
}
});
});
});
</script>
Html:
<a href='?neat=one#end'>Go</a>
<p id='end'>Imagine this is a panel you are going to in your example.</p>
The code above firstly has a getParameterByName function which looks into your URL to find your query string value pairs. and if its not null it returns its value.
in the section below that ...imagine that the anchor tag is your navigation and I clicked on it, then the page animates to the correct hash section. Once the animation is completed in the callback section is asks for the query string value. if it you have set a value for "neat" then your code for playing the video should sit where the alert is now.
You can put the querystring before the hash:
www.blah.com/index.php?neat=one#BLAH
Or for a more complex solution, look into the querying aspect of ben alman's jquery bbq plugin:
http://benalman.com/projects/jquery-bbq-plugin/

Categories