I am using this ajax code to send data to server:
$.ajax({
data: postData,
type: method,
url: url,
timeout: 20000,
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
error: function(jqXHR,textStatus,err){alert("Error returned from ajax call "+err);},
success: function(data,status,jqXHR){
// process response...
}
});
postData is a query string with many values while method is GET or POST
The problem is that, when I send a query string that contains a value like Älypuhelimen lisävarusteet, the result in database is �lypuhelimen lis�varusteet
The database connection collation is utf-8, and this works fine when I do not use ajax to post and save to database... It is definitely ajax that messes up the encoding...
I have also tried using encodeURIComponent() function on the data, and it becomes %C4lypuhelimen%20lis%E4varusteet if I use it... same goes for escape() function...
any help will be appreciated...
You should try jQuery Base64 encode.
JavaScript:
<script src="jquery.min.js"></script>
<script src="jquery.base64.min.js"></script>
<script>
enctext = $.base64.encode("yourtext");
//your ajax code goes here.
</script>
PHP :
<?php
$org_text = base64_decode($_POST['your_variable']);
?>
jQuery Base64 plugin.
download from here.
https://github.com/carlo/jquery-base64
Try changing the column in the database to utf16_bin Collation
Post you php database connection code.
Just for information and to help others that might fall in the same situation...
The problem was with the postData itself... it was parsed such that every post variable was applied with escape()... Using encodeURIComponent() instead of escape() worked!
Summary:
Donot use escape() function to url-escape query components... use encodeURIComponent() instead...
Related
I submit a form using AJAX and JQuery.
serializedData = $form.serialize();
request = $.ajax({
url: "processData.php",
type: "post",
data: serializedData
});
The problem is when I use the data on processData.php the text is urlencoded.
cuando=Una+fecha+%C3%BAnica
I tried using php's urldecode() but it is not working. it's still urlencoded..
$cuando = $_POST['cuando'];
$cuando = urldecode($cuando);
Any suggestion? Thanks a lot!
Can't reproduce it..I made this:
<?php
echo urldecode('Una+fecha+%C3%BAnica');
Output is this:
Una fecha única
My only guess is that in your question you mean that your output is still what my input is, in that case you are somehow double urlencoding it on the other end
The form data is encoded in the same way as if it was a normal HTML form submission without Ajax, so there is nothing special to do, just use:
echo $_POST['cuando'];
PHP has already decoded and parsed the request body (post data).
First I tried changing the contentType attribute on JQuery $.ajax function, but it didn't work out:
$.ajax({
url: "insertar_datos.php",
type: "post",
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
data: serializedData
So, I found that there some issues at working with other charset encodings that are not UTF8 on JQuery-ajax.
http://forum.jquery.com/topic/serialize-problem-with-latin-1-iso-8859-1-and-solution
Then, I tried with no hope the php function utf8_decode():
utf8_decode($_POST['cuando']);
And it worked. what I found on this link is that:
"utf8_decode simply converts a string encoded in UTF-8 to ISO-8859-1. A more appropriate name for it would be utf8_to_iso88591. If your text is already encoded in ISO-8859-1, you do not need this function. If you don't want to use ISO-8859-1, you do not need this function."
So, if someone is using iso-8859-1 and is having some problems with ajax posted data. You can decode it for sure with utf8_decode(). Maybe there is an easier way to decode all data before post it, but as you can see it didn't worked to me.
If someone knows a better/more efficient way I'll choose your answer happily.
Hope it helps someone,
Regards!
Absolutely new to PHP and so far it isn't pretty.
Anyway, I'm trying to pass a variable over to a PHP script, do a couple things with it, and pass it back to my Javascipt code.
Here's where I pass it off to PHP:
var src=encodeURIComponent("http://www.someonlinesite.com/file.swf");
$.ajax({
url:'test.php?src='+src,
dataType:'json',
success:function(response){
alert(response)
}
});
and here's the script:
<?php
$src=isset($_GET['src'])?$_GET['src']:'';
$size=getimagesize($src);
echo json_encode(array('size'=>$size));
?>
I'm trying to pass the URL of a .SWF video file over to a small PHP script that will use getImagesize() to figure it's dimensions and pass them back.... but I'm not seeing anything in the response and the alert isn't firing.
What's going wrong?
UPDATE:
I've updated the code with the most recent - according to the advice from some nice SO members. When I hardcode the $src variable and navigate directly to the test.php it echoes everything perfectly. So, it looks like the PHP is working. However, it appears like either the callback is never firing or the PHP file isn't returning the data. In the console there still isn't anything in the response.
You need to concatenate your url string parameter in get():
$.get('test.php?src=' + src, function(data){
alert(data);
});
And also, your src variable begins with a double quote and is closed with a single quote. That will cause issues.
var src="http://www.someonelinesite.com/file.swf";
Also, it's probably a bad idea to do this via $_GET since you are passing a URL. $_POST would be better or encode the URL before you pass it. The current url you are passing right now would look like this in a browser:
http://www.mysite.com/test.php?src=http://www.someonelinesite.com/file.swf
That's not pretty. Using encodeURIComponent(), your whole URL will end up looking like this:
http://www.mysite.com/test.php?src=http%3A%2F%2Fwww.someonelinesite.com%2Ffile.swf
Edit to $.ajax
$.get above would work just fine, but going with the implementation of $.ajax works too:
$.ajax({
url:'test.php',
type: 'GET', //Add the type
dataType:'json',
data: {'src': src}, //Add the data, leave it out the url
success:function(data){
alert(data)
}
});
Try this :
In Jquery :
var src="http://www.someonelinesite.com/file.swf";
$.ajax({
url:'test.php?src='+src,
dataType:'json',
success:function(response){
alert(response.size);
}
});
In php
$src=isset($_GET['src'])?$_GET['src']:'';
$size=getimagesize($src);
echo json_encode(array('size'=>$size));
?>
The client website needs to do a cross-domain JQuery Ajax call to a php file on my server, the php file will query the database for a bunch of stored javascripts which then need to be sent back to the client and be executed on the client's website. This is what i have so far, haven't done the grabbing javascript from database yet and it works. Is this the best way to do this (assuming i can grab the javascripts directly from the database without adding the escape sequence when echo'ing back to the client)? Thanks.
This is what i have so far:
client side:
$.ajax({ url: "http://localhost:8888/test.php",
dataType: "script",
});
server side (test.php):
<?php
echo "alert(\"WORKS!\");";
?>
Review the ajax documentation and handle the success callback option on the ajax method:
$.ajax({
url: "http://localhost:8888/test.php",
dataType: "html",
success : function(data) { alert(data); }
});
As noted by Ricardo, your PHP script should echo HTML or some other content appropriate for your scenario.
See http://api.jquery.com/jQuery.get/
And http://api.jquery.com/jQuery.getJSON/
I'm running into some problems with user-entered input that I want to send to PHP as JSON via AJAX that contains special characters, like ", ', etc. I'm sending the contents of an array (used for slickgrid), and everything works fine unless those characters are included. I know that PHP has the handy function mysql_real_escape_string, but is there any sort of jquery analogue? Here is the relevant code:
req = $.ajax({
url: url,
dataType: "text",
data: {"data": $.JSON.encode(data)},
type: "post",
success: onSaveSuccess,
error: onSaveError
});
Here's the PHP it is submitted to:
<?php
//$data = array();
//if (isset($_POST['data']))
//{
//$data = json_decode($_POST['data']);
//}
//header('Content-Type: text/javascript');
//echo json_encode($data);
print_r($_POST);
?>
To be clearer, when special characters are included, neither the success nor error events are triggered.
I looked in firebug and it doesn't appear to send anything at all when the special characters are included... Of course, it does when it's just letters or something.
It was due to the script from here apparently failing on certain kinds of input. Switching to json2.js and using JSON.stringify has solved the problem.
data: {"data": $.JSON.encode(data)},
Passes up a more complex JSON object than you need.
$data = json_decode($_POST['data']);
Is looking for a serialized JSON object but jQuery is doing a lot of work for you in the background.
Try this
data: $.JSON.encode(data),
In your AJAX call. Then in PHP
$data['myPostValue'] = $_POST['myPostValue'];
You're sending a JSON object to the $.ajax call, and it is changing it into the name value pair that the server would normally get from a post.
javascript has an escape() function. you can $.JSON.encode(escape(data)) might work
I have this weird issue which i'm absolutely don't even know how to address...
i have this little AJAX call:
$('#toexcel').live("click",function() {
var sql = grid.data().sql;
$.ajax({
url: "toExcel.php",
data: "sql="+sql,
success: function(response){
window.location.href = response.url;
}
});
alert(sql);
});
});
which passes sql query from grid object to toExcel. toExcel uses PHPExcel object to output excel file.
now:
when alert pops up it has normally looking query ...LIKE '%cdviled%'...
but when i access this query in toExcel.php it looks like ...LIKE 'Íviled04%'....
and query obviously fails.
why????? how??
PHP is interpreting %cd as a URL-encoded entity. You will want to run your parameters through encodeURIComponent() before sending off the ajax request.
Although, you might get better (and more secure) results by simply sending the search term via ajax and letting PHP assemble the SQL on the server side!
%cd is a valid URL-encoded representation of Í and PHP handles it as such.
The solution is simple. Change:
data: "sql="+sql,
to:
data: "sql="+encodeURIComponent(sql),