Send Get with jQuery with spaces - php

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);
}

Related

Decode a search string in the url

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

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>

Get the result from data.php then convert it to javascript string

I don't really know what to call this. Here is my files
data.php => calculate some random numbers
jscript.js => this echos the result from data.php to a div id
but what i wanted to do on some part is get the value from data.php and then place it to
the style of the div.
$.get('jQuery/data.php?Con&userHP&perc' , { userID: xUserID } ,
function(output) {
$('#userHP').html(output).show();
var o = document.getElementById("userHealthbar");
o.style.width= result_here ;
}
);
instead of placing the result inside the div /div i want it to the style portion of the div.
P.S: i want the result from data.php to be a variable in javascript.
but i can't seem to make it work.
i hope my explanation is clear.
You should use jQuery selectors instead of document.getelementById.
Common way:
jQuery('#userHealthbar').attr('style', whateverYouGotFromPHP);
Better in this case:
jQuery('#userHealthbar').width(whateverYouGotFromPHP);
D.
If you want to return multiple things from php to javascript, you should use json.
So if you have 2 values you want to return in php, you can do:
$html = "your html string";
$value = 4; // the value you want to return
$array_to_return = array("html" => $html, "value" => $value);
echo json_encode($array_to_return); // the only thing you echo out in your php!
Then, in javascript, you can use $.getJSON instead of $.get:
$.getJSON('jQuery/data.php?Con&userHP&perc' , { userID: xUserID } ,
function(output) {
$('#userHP').html(output.html).show();
var o = document.getElementById("userHealthbar");
o.style.width= output.value ;
}
);

Jquery set PHP $_GET array

Greetings Stackoverflow
How do I set the php $_GET[] array from Jquery? I have a string looking simmilar to this: $sample_string = "Hi there, this text contains space and the character: &";. I also have a variable containing the name of the destination: $saveAs = "SomeVariable";. In PHP it would look following: $_GET["$SomeVariable"] = $sample_string;.
How do I do this in Jquery?
Thanks in advance, Rasmus
If you're using jQuery, you'll have to set up an AJAX request on the client side that sends a GET request to the server. You can then pull the data you supplied in the request from the $_GET[] array on the server side.
$(function() {
var data = {
sample_string: "hi",
saveAs: "something"
};
$.get('/path/to/script.php', data, function(response) {
alert(response); // should alert "some response"
});
});
And in script.php:
<?php
$sample = $_GET['sample_string']; // == "hi"
$saveAs = $_GET['saveAs']; // == "something"
// do work
echo "some response";
?>
Can't tell if you're looking to grab a GET param from javascript or set a GET param from jQuery. If it's the former, I like to use this code (stolen a while back from I can't remember where):
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
Then you can call
var cake = urlParams['cake'];
To get the $_GET param specified by http://someurl.com?cake=delicious
If you want to send a $_GET parameter, you can use either jQuery's $.get() or $.ajax() functions. The $.get function is more straightforward and there's documentation on it here http://api.jquery.com/jQuery.get/
For $.ajax you would do something like this:
var trickystring = "Hi there, this text contains space and the character: &";
$.ajax({
url:'path/to/your/php/script.php',
data: {
'getParam1':trickystring,
'getParam2':'pie!'
},
type:'GET'
});
Now in PHP you should be able to get these by:
$trickystring = $_GET['getParam1'];
$pie = $_GET['getParam2'];
Hope these examples GET what you're looking for. (Get it?)
if $sample_string is what you want in jquery, you can do
var sample_str = '<?php echo $sample_string; ?>'; and then use the js variable sample_str wherever you want.
Now, if you want to do set $_GET in jquery, an ajax function would be way to go.
$.ajax({
url:'path/to/your/php_script.php',
data: {
'param1': 'pqr',
'param2': 'abc'
},
type:'GET'
});
Do you mean that would look like $_GET[$saveAs] = $sample_string y think.
$_GET is a variable for sending information from a page to another by URL. Its nosense to do it in jQuery that is not server side. If you want to dynamically set the $_GET variable to send it to another page you must include it in the URL like:
/index.php?'+javascriptVariable_name+'='+javascriptVariable_value+';
$_GET is just a URL parameter. So you can access get like /index.php?id=1:
echo $_GET['id'];
Look at this article, it shows all the ways to load stuff with ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

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;
}

Categories