I format text with javascript asigning + to every emtpy space like this
var ft = text.replace(/ /g,"+");
Then I pass ft to a php script via jquery ajax as an get argument.
But
print $_GET['text'];
gives me the text with empty spaces instead +.
Any ideas?
You should get familiar with the concept of URL encoding.
PHP's urldecode function will run against all $_GET variables by default, so if you want to see raw input, use rawurldecode:
$encoded = array_map('rawurldecode', $_GET);
echo $encoded['text']; //blah+blah
Also, it's a good idea to use JSON to pass data from javascript to PHP.
Related
I send a QueryString formatted text like bellow to a php Script via Ajax:
title=hello&custLength=200&custWidth=300
And I want to convert this text to a JSON Object by this result in PHP:
{
"title" : "hello",
"custLength" : 200,
"custWidth" : 300
}
How can i do that. Does anyone have a solution?
Edit :
In fact i have three element in a form by title , custLength and custWidth names and i tried to send these elements via serialize() jquery method as one parameter to PHP script.
this code is for Send data to php:
customizingOptions = $('#title,#custLength,#custWidth').serialize();
$.post('cardOperations',{action:'add','p_id':p_id,'quantity':quantity,'customizingOptions':customizingOptions},function(data){
if (data.success){
goBackBtn('show');
updateTopCard('new');
}
},'json');
in PHP script i used json_encode() for convert only customizingOptions parameter to a json.
But the result was not what I expected and result was a simple Text like this:
"title=hello&custLength=200&custWidth=300"
I realize this is old, but I've found the most concise and effective solution to be the following (assuming you can't just encode the $_GET global):
parse_str('title=hello&custLength=200&custWidth=300', $parsed);
echo json_encode($parsed);
Should work for any PHP version >= 5.2.0 (when json_encode() was introduced).
$check = "title=hello&custLength=200&custWidth=300";
$keywords = preg_split("/[\s,=,&]+/", $check);
$arr=array();
for($i=0;$i<sizeof($keywords);$i++)
{
$arr[$keywords[$i]] = $keywords[++$i];
}
$obj =(object)$arr;
echo json_encode($obj);
Try This code You Get Your Desired Result
The easiest way how to achiev JSON object from $_GET string is really simple:
json_encode($_GET)
this will produce the following json output:
{"title":"hello","custLength":"200","custWidth":"300"}
Or you can use some parse function as first (for example - save all variables into array) and then you can send the parsed output into json_encode() function.
Without specifying detailed requirements, there are many solutions.
I am using PHP to save the values of a form as JSON into a cookie like so:
// set cookie with search values so we can use jQuery to repopulate the form
setcookie('jobSearchValues', json_encode($form_state['values']), 0, '/');
This works great and then on the JavaScript side I can use this to get at the values:
var jobSearchValues = JSON.parse($.cookie("jobSearchValues"));
$("#keywords").val(jobSearchValues.keywords);
Again this works great, but the problem is that when a value for one of the fields in the form has a space in it, the space gets replaced with a "+". So when the form gets repopulated the text field displays like this for example "hi+mom". Is there a better way to go about this? By the way, $form_state['values'] is a PHP array. There are 4 fields in the form that I am setting as JSON into the cookie.
Use setrawcookie( '<name>', rawurlencode( json_encode( $value ) ), ... ) and then manually url-decode & json-parse on the client side (with JSON.parse(decodeURIComponent(cookie)))
This is weird. json_encode is not supposed to replace spaces with +..
setcookie is probably urlencoding it.
You will have to urldecode it in javascript before using it.
Try this:
(taken from phpjs)
function urldecode(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
and then
var jobSearchValues = JSON.parse($.cookie("jobSearchValues"));
$("#keywords").val(urldecode(jobSearchValues.keywords));
In another question, it was pointed out that using semantic markup can be a really clean way to pass data to an onclick function, along the lines of the code below.
I have a second question relating to passing JSON and having the receiving function recognize it as such. I have a PHP generated JSON value of [{"authed":"2012-03-04 17:24:24"},{"authed":"2012-03-04 11:44:38"}] that I need to pass to a function. echoing that straight into the <a> tag won't work, so I am using urlencode() to get:
click
Unfortunately, when I alert this out from popup(), I via the following code:
function popup(t) {
var auth = t.getAttribute('data-auth');
alert(decodeURI(auth));
}
I get
[{"authed"%3A"2012-03-04+17%3A24%3A24"}%2C{"authed"%3A"2012-03-04+11%3A44%3A38%22"}]
which JSON.parse() is unable to handle. Any suggestions on decoding/passing JSON using this pattern?
You need to use htmlspecialchars() to escape for html--like you should be doing for everything you echo out in html. (You are doing that right? To prevent generating invalid html?) Don't use urlencode(), which is for encoding parts of a url path or query parameter.
<?php
$data = array(array('authed'=>'2012-03-04 17:24:24'), array('authed'=>'2012-03-04 11:44:38'));
$jsondata = json_encode($data);
?>
<a data-auth="<?php echo htmlspecialchars($jsondata, ENT_COMPAT, 'UTF-8')?>" onclick="popup(this)">click</a>
I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Use encodeURIComponent in Javascript: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp and urldecode in PHP: http://php.net/manual/en/function.urldecode.php
Unless you've encoded it multiple times (e.g. by explicitly calling the encode method AND inserting the value into a form field which is then submitted) you don't need to do anything - it is transparently converted back to its original form when the request is parsed.
You can use rawurldecode function in php, but this function is not UTF-8, then your have to convert to UTF-8 with utf8_decode like this
echo utf8_decode(rawurldecode('Educa%C3%A7%C3%A3o%20Multim%C3%ADdia'));
I think I have the need to take a uri which has been decoded in PHP, and re-encode it.
Here is the situation:
JavaScript passes encoded uri as query string parameter to php script.
PHP script embeds uri as a hidden input value in an html document, responds with the document to a user agent.
JavaScript reads embedded uri and sets location of current document based on value of hidden input.
On Step 2, I am finding that the Uri is fully decoded after reading it in via $_GET. So when I embed the uri in the hidden input, it becomes un-encoded. So I would like to run a PHP script which re-encodes the Uri properly ex:
http://my.example.com/dog walk?is=very great
==>
http://my.example.com/dog%20walk?is=very%20great
Is there a pre-built php function for this or should I just write my own?
PLEASE NOTE: urlencode and urldecode are not the answer to get the desired input/output I have in the example above.
Thanks,
Macy
Are you looking for : http://fr.php.net/manual/en/function.urlencode.php ?
I don't know if will help you, but PHP have 3 useful functions:
$url = parse_url('put the url here');
parse_str( $url['query'], $query ); // generating an array by reference (yes, kinda weird)
echo $query; //in this line, you can encode or decode.
or, if you want to mount a query, you can use http_build_query(); that accepts values from an array, like:
$url = 'http://my.example.com/dog walk?';
$array = Array (
'is' => 'very_great',
);
$url_created = $url . http_build_query($array);
urldecode:
http://www.php.net/manual/en/function.urldecode.php