Quotes are escaped when passing JSON as a POST variable [duplicate] - javascript

This question already has answers here:
Why are $_POST variables getting escaped in PHP?
(6 answers)
Closed 9 years ago.
I'm doing an Ajax call to my server and need to send an array. I'm encoding the array using JSON. That results in this data sent to the server using a POST request:
selection=%5B%221%22%5D
On the server, I have this code:
echo urldecode($_REQUEST['selection']);
This results in:
[\"1\"]
Note that there are no backslashes in the request. I checked that with Firefox's dev tools.
Where were the backslashes added? Am I doing something wrong here? I can't decode the string like this.
This is the client-side code:
$.ajax({
type: "POST",
url: "<my-uri>/rule/add.php",
data: {
selection: JSON.stringify(["1"]) // in reality this is a variable array
}
}).done(function(data){
alert(data);
});

This happens because your server is configured to add slashes to quotes.
If you wish to avoid this go to your php.ini and set magic_quotes_gpc to 0.

Related

Get text of curtain element ID in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I need to parse the text out of an h3 element on an HTML page and save the text into a variable.
<h3 class="names-header">Names</h3>
I need the output:
Names
Saved into a variable like
$text = $output;
I've tried using DOMs, specifically this example but I've had no luck.
I've also tried to extract the data using JQuery, and submitting it as a post using Ajax on the same page. Then grabbing the post and saving it in PHP. This also didn't work, and it seems like there is a much quicker way to do this.
I've googled and tried for around 2 hours now and still can't figure out how to fix it. Any help/advice would be greatly appreciated right now. Thank you.
it would be easy to use jquery to do this! just use ajax like in the following code.
$.ajax({
type: 'POST',
url:'your php page',
data:{name: $('.names-header').text()},
success:function(response){
alert(response);
}
})
in you php do the following.
if(isset($_POST['name'])){
echo $_POST['name'];
}else{
echo 'no data to show';
}
this will allow you to catch the post data and do what ever you want.

How to Put Jquery Variable into PHP Variable? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
<script>
myjsvar= $('#project_start_date').val(); //result is 10-11-2014
</script>
<?php
$myphpvar = //how to get value of myjsvar into php variable?
?>
how to get value of myjsvar into php variable $myphpvar?
thanks
You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.
Your variable loc will have a value only when the code reaches the browser.
If you want to get some value from server and combine it with javascript variables then do the following.
Use an ajax request and send the desired values to server. The server will return with a response. Use that response text and store it in your action variable.

Pass json message notification in javascript chrome extenstion [duplicate]

This question already has an answer here:
send JSON data in send notification using parse.com (JavaScript)
(1 answer)
Closed 8 years ago.
Folloing working for me in javascript for notification alert("message onclick"); this way i want to pass json string message in push notification , i dont know how to pass this please help me. i want something like this,
alert("\{"action":"com.example.dictionarylib.notification.UPDATE_STATUS", "alert":"hello""\}");
but i dont know actual sysntax of passing json, please give me right way to pass this.
As I understand you need a function which will convert your JSON object to String.
There is function called stringify you can use for that.
Here is the documentation on stringify function.
And here is the usage example:
var json = {
"action": "com.example.dictionarylib.notification.UPDATE_STATUS",
"alert": "hello"
};
alert(JSON.stringify(json));
JSFiddle Demo.

How to encode and set url as query string to other url in location.href? [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I have following url
http://www.example.com/p1/val1/p1/val2
Now I want to pass third parameter as url like '/path/to/file'
http://www.example.com/p1/val1/p1/val2/url/?????
How can I do that with combination of js and php?
After passing above, I would like to get value of url as '/path/to/file'
I am passing above link through js's location.href.
How to do that?
I am getting following error?
Not Found
The requested URL http://www.example.com/p1/val1/p1/val2/url//path/to/file was not found on this server.
why didn't use traditional query string?
http://www.example.com?p1[]=val1&p1[]=val2&url=<?php echo urlencode('/path/to/file'); ?>
I'm assuming you process your urls this way:
http://www.example.com/p1/val1/p2/val2/...
gets transformed into:
http://www.example.com?p1=val1&p2=val2&...
So, your poblem is that your url parameter receives, as value, either '' or just 'path'.
You could try this, with javascript
var url = 'http://www.example.com/p1/val/p1/val2';
var path = '/path/to/file';
var finalUrl = url + '/url/' + encodeURIComponent(path);

Accessing python dictionary in javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
I am sending dictionary from python like this:
{'root':['value','path','type'],.......}
i am send it to javascript by ajax call request by serializing it,
How to access that dictionary in javascript.
thanks in Advance
Suppose you are making AJAX call in below way, you will get the response dict as resValue. use JSON.parse method on it
$.getJSON( "/url", {params }, function( data, status, xhr ) {
$.each(data.response, function(resKey, resValue){
if(resKey == "success"){
var _result = JSON.parse(resValue);
}
}
}

Categories

Resources