I have a php array
array(6) {
["merchant_id"]=> string(6) "ajeesh"
["passkey"]=> string(4) "1234"
["amt"]=> string(5) "10.00"
["email"]=> string(16) "ajeesh#gmail.com"
["mobileNo"]=> string(10) "9874563210"
["orderID"]=> string(6) "123456"
}
which I got as a result of var_dump($_POST).
How can I copy all this value to a javascript array variable?How can it be possible? Suppose if the javascript array I made is
var thisSession = new Array();
TRY
I have tried this in the javascript
<script>
window.onload = function getApp(){
var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
alert (thisSession);
}
</script>
and this in the php
json_encode($_POST);
but the javascript is alerting "Object object".Im not gettign the array!why?
You can use JSON, encode the PHP variable, then parse it in JS:
var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
using php's json_encode & javascript's JSON.parse
var thisSession=JSON.parse('<?php echo json_encode($phparray)?>');
EDIT
If you want to access merchant_id you simply do
alert(thisSession.merchand_id);
Here, you need to json_encode the php data to use in javascript
$array = json_encode($_POST);
In your html
<script>
var data = JSON.parse("<?php echo $array; ?>"); // your new javascript object
</script>
Reference http://www.php.net/json_encode
There is no comparable object (associative array) in JavaScript to that which you've shown us in your example. You'd either have to use two Arrays or lose the ordering (and ability to have multiple keys of the same name) by using an Object. This second option is what json_encode will result in.
Related
I have used the datatable ajax call for listing I received the below json data in ajax call:
{
"sEcho":1,
"iTotalRecords":"1",
"iTotalDisplayRecords":"1",
"aaData":[
["Demo to Mam","2"]
],
"countOfTotalRecords":2
}
How I can use the countOfTotalRecords var value on view page?
You've to decode your JSON data using json_decode, then you have an object. Looks something like this:
$json = '{"sEcho":1,"iTotalRecords":"1","iTotalDisplayRecords":"1","aaData":[["Demo to Mam","2"]],"countOfTotalRecords"
:2}';
$decode = json_decode($json);
echo $decode->countOfTotalRecords; // this will display 2, regarding your data
use the below code
$str = '{"sEcho":1,"iTotalRecords":"1","iTotalDisplayRecords":"1","aaData":[["Demo to Mam","2"]],"countOfTotalRecords":2}';
$arr = json_decode($str);
$tcount = $arr->countOfTotalRecords;
echo $tcount;
I want to get a php array made by pg_fetch_all in a javascript array. But when I try to do it, firebug tells me that I'm trying to convert an array to string. Indeed, I don't understand why because both are arrays.
Here is where I create the php array :
$conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'";
$dbconn = pg_connect($conn_string);
$sql = "SELECT ".$colonne." FROM public.".$tablevar."";
$res = pg_query($sql) or die("Pb avec la requete: $sql");
$data = pg_fetch_all($res);
And here is my js code :
var array_dropdown =[<?php echo $data;?>];
And it doesn't work. Please help me !
PHP Arrays 101: Arrays in a string context are the literal word Array:
$x = array(1 => 2);
echo $x; // ouputs "Array"
You need to use json_encode():
var array_dropdown = <?php echo json_encode($data); ?>;
json_encode guarantees that whatever you pass in to the encode function will be output as syntactically valid javascript. Note the lack of [] around the above code - json_encode handles all of that for you.
Assume this as your PHP array
$array = array('foo' => 'bar');
The JS part:
var array = <?php echo json_encode($array); ?>;
I passed an array using Ajax and i echoed it in php , it works good. But i don't know how to separate its value and find its length.
Here is the code:
var myCheckboxes = new Array();
$(".book_type:checked").each(function() {
myCheckboxes.push($(this).val());
});
$.post("s.php?c="+category+"&k="+myCheckboxes,{data : "some data"}, function(response){
$("#show_result").html(response);
And the php is:
$a=$_GET['k'];
echo $a;
it displays the values of all checkbox like this
all,0,1,2,3,4
How can i find $a length as array?. if i use sizeof($a) it shows as 1.
Also how to separate those values into each single value.
Any suggestion
try like this
$a=$_GET['k'];
$value= explode(",",$a);
echo sizeof($value); //output 6
echo $value[0]; //all
echo $value[1]; //0
echo $value[2]; //1
echo $value[3]; //2
echo $value[4]; //3
echo $value[5]; //4
Try using:
$a=$_GET['k'];
$a=explode(',', $a);
echo count($a);
Pass the value as an array
$.post("s.php", {
data: "some data",
c: category,
k: myCheckboxes
}, function (response) {
$("#show_result").html(response);
})
then
$a=$_GET['k'];
where $a will be an array, so count($a) should give you the length
You should call JSON.stringify in your javascript:
// ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
... category+"&k=" + JSON.stringify(myCheckboxes) ...
or, alternatively, use jQuery ajax syntax sugar in your post request:
$.post("s.php", { k: myCheckboxes, ... });
This will provide a json as string passed to controller. From within your controller you should json_decode the received parameters:
$a = $_GET['k'];
print_r(json_decode($a));
Hope this helps.
I have a php page that receives a json object from javascript page, but i was not able to decode the json in php. How to decode the json and store in php such that $arr[0]=[1,2,34,5,2]; $arr[1]=[2,1,34,5,2]; $arr[2]=[8,1,34,5,2]; in php ?
after removing "myString = JSON.stringify(myObject);"
echo $value; outputs "Array"
echo $value[0]; outputs nothing
echo $value->{"key"}; outputs nothing either
how can i actually get the array contents?
javascript:
var mon=[1,2,34,5,2];
var tue=[2,1,34,5,2];
var wed=[8,1,34,5,2];
var myObject = {'key' :'value','key2':'value','key3':'value'};
myObject.key = mon;
myObject.key2 = tue;
myObject.key3 = wed;
myString = JSON.stringify(myObject); //this line removed
var jsonString = JSON.stringify(myObject);
$.ajax({
type: "POST",
url: "n3.php",
data: {data : jsonString},
cache: false,
success: function(aaa){
alert("OK");
$("#pageContent").html(aaa);
}
});
php:
<?php
$value = json_decode($_POST['data']);
echo $value; //this echos the whole json object
echo $value->{"key"}; //this outputs nothing
?>
You are JSON encoding your data twice on the Javascript side. When you call json_encode in PHP once, you get a JSON encoded object back. That's why echo $value outputs the whole string. If it was a PHP array at this point it would output "Array" or an error in case it was an object, it would not output the whole content.
Either json_decode it again, or don't double encode it in Javascript.
I want to create a javascript array from databse like this:
var m = [
[one]
[two]
[three]
]
it is important that typeof m be object.
Use Json_encode
$phparray; // This is your php array
$jsArray = <?php echo json_encode($phparray); ?>;
//do stuff with $jsArray now