Get PHP data into Javascript Array [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to transfer an array between PHP and Javascript
I am trying to return a series of arrays from PHP to javascript using AJAX.
I have tried pre-formatting the email addresses as a JSON object and returning that to my JS script and then parsing it as JSON but no luck.
I have a main array called emails, and I want these arrays returned from PHP and to be converted to a JS array, I have tried:
emails = $.makeArray($.parseJSON(email)) ;
But with no luck.
How can I achieve what I want?

You should be able to use JSON_encode to pass the array directly from PHP to a Javascript variable:
<?php
$arr = array(
array("foo" => "bar")
);
?>
<script type='text/javascript'>
var myarray = <?php echo JSON_encode($my_array); ?>;
alert(myarray[0].foo);
</script>

Related

Convert Query String to Array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];

passing a php array to a jquery function [duplicate]

This question already has answers here:
Passing PHP variable into JavaScript [duplicate]
(5 answers)
passing PHP objects to javascript [duplicate]
(4 answers)
Closed 9 years ago.
I need to pass a php variable to a jquery function. the php variable is an array of unknown size. is there a way to pass each member of the array to be processed individually by the function?
I Guess you should use JSON to pass the php variable to jquery. its the easiest way to do that.
You can use JSON to print the array and parse that to receive a JS object.
I assume you use AJAX (directly printing into the javascript source is possible but ill-advised).
Your php script needs to print output directly:
print_r(json_encode($data_array));
And on the receiving end use $.parseJSON:
var data_array = $.parseJSON(ajax_result);

Getting data with php in a javascript file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Echo PHP variable from JavaScript?
How can I get a PHP variable to AJAX?
I'm trying to get some data from my database with php, and put it in a javascript variable. what is the easiest way to do that using JQuery ?
This is easiest for me, where $var is your array in PHP, being set to the variable "a" in Javascript:
<script type="text/javascript">
var a = <?php echo json_encode($var); ?>;
</script>

Is there other ways of passing a php $_GET variable from url to a js variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get query string values in JavaScript
I know you could do this by echoing the variable $_GET['whatevervariable'] to a js variable.i was just wondering if there are other methods that can also do this?
This will provide the entire $_GET array to your JavaScript app:
<script type="text/javascript"><!--
_GET = <?php echo json_encode($_GET); ?>;
--></script>
The $_GET superglobal is just an array of the querystring.
You can fetch the querystring in javascript with window.location.search, but to use it like $_GET will need some sort of parsing and usually a few regular expressions to handle difficult characters etc.
Just try var qs = window.location.search; and then figure out exactly what you need, and how you will get it.
The easy solution if doing it inline is just echoing $_GET into a variable, like in danorton's answer.

How to parse JSONP with PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extract JSONP Resultset in PHP
I get the response in the following format. Im facing trouble on how to get inside "Plugin" variable and access other variables inside it.I used json_decode(), but i cant access the variables.
Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)
I tried
$a = json_decode($json,true);
echo $a['plugin_a'];
I dont get any output.
echo var_dump($json); gives me
string 'Plugin({
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_ce":"abc" })'
try substr();
http://sandbox.phpcode.eu/g/40c20.php
<?php
$json = substr('Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)', 9, -1);
print_r(json_decode($json));
Perhaps this will work for you:
$data=array('plugin_a'=>'abc','plugin_b'=>'bcd','plugin_c'=>'cde');
$json='{"Plugin":'.json_encode($data).'}';
$a=json_decode($json,true);
echo $a['Plugin']['plugin_a'];
It appears as though the actual json array may not have integrity. If this solution doesn't fit, can you post the code that actually builds the json array?

Categories