最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How exactly do you use json_decode to pass a javascript array to php? - Stack Overflow

programmeradmin4浏览0评论

This is how I got my php array to javascript

echo 'var daysofweek = '.json_encode($daysofweek).';';

Now I am aware that json_decode can do the opposite, however the problem is I don't know the syntax to do so.

I tried:

<script>
var array_days = new Array();

array_days[] = "psdfo";
array_days[] = "bsdf";

<?php
$array_days = json_decode(?>array_days<?php);?>

</script>

yes im clueless.

I forgot to mention that I want to send an array through post, having all my information regarding all the form values dynamically created by javascript. Otherwise, I wouldn't know which name="" to look for as it will be decided by the user. Unless someone else has an alternative solution...

This is how I got my php array to javascript

echo 'var daysofweek = '.json_encode($daysofweek).';';

Now I am aware that json_decode can do the opposite, however the problem is I don't know the syntax to do so.

I tried:

<script>
var array_days = new Array();

array_days[] = "psdfo";
array_days[] = "bsdf";

<?php
$array_days = json_decode(?>array_days<?php);?>

</script>

yes im clueless.

I forgot to mention that I want to send an array through post, having all my information regarding all the form values dynamically created by javascript. Otherwise, I wouldn't know which name="" to look for as it will be decided by the user. Unless someone else has an alternative solution...

Share Improve this question edited Dec 28, 2010 at 4:50 Adam asked Dec 28, 2010 at 4:31 AdamAdam 9,04916 gold badges70 silver badges134 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

PHP is a pre-processor and can't do what you're asking. You would need the PHP engine to be able to process the javascript on the page. json_decode is for decoding a json string in PHP.

To do that you would have to pass the javascript array to a PHP script using something like Ajax so your PHP script can process it anyway you would need, then pass back the result to your Javascript code.

<script>
var array_days = new Array();

array_days[] = "psdfo";
array_days[] = "bsdf";

<?php
$array_days = json_decode(?>array_days<?php);?>

</script>

I'm clueless in this part... is this javascript? if so, you need to use a JSON Parser like the JSON2.js https://github./douglascrockford/JSON-js

this file creates a Javascript JSON Object which allows you to parse a JSON string to a Javascript object. (you can always use eval() but it won't prevent against possible malicious code...) or the other way around... from a Javascript Object transform into a JSON string.

After converting to a JSON string you just need to send the data through AJAX to a PHP file, like:

var data = {
   key: value,
   key2: value2 
},
JSON = JSON.stringify(data);

$.ajax({
    url: 'php_file.php',
    type: 'POST',
    data: JSON,
    success: function(data){
        alert(data)
    }
});

(I'm using jQuery to save some lines...)

Then on the php_file.php you would have something like this:

<?php 
    $json = json_decode($_POST['data']);

    foreach($json as $value){
       //Do something with it!
    }
?>

Yes as the Wajiw said, PHP is pre-processor. But I think it might be possible with the ajaxed request. Send the Javascript array as an argument request to the other page and process it with the php, and again display back the results.

Note: Never tried this :) Thanks

First of all:

array_days[] = "psdfo";
array_days[] = "bsdf";

These lines are valid PHP but not valid JavaScript. The correct equivalent would be:

array_days.push("psdfo");
array_days.push("bsdf");

On to passing the resulting array of strings (that's what I am assuming) to PHP. To send it as form data in pure JS, we can create hidden input elements when the form is submitted (try it):

<form id="myform" method="POST" action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi">
    <input type="submit" value="Submit">
</form>
<script>
    document.getElementById('myform').onsubmit = function() {
        for(var i = 0; i < array_days.length; ++i) {
            var inputElement = document.createElement('input');
            inputElement.type = 'hidden';
            inputElement.name = 'array_days[]';
            inputElement.value = array_days[i];
            this.appendChild(inputElement);
        }
    };
</script>

PHP will interpret each input having the name of array_days[] as an array element of $_POST['array_days']. For example, you can access $_POST['array_days'][0] in PHP.

For AJAX, jQuery 1.4 and above will automatically achieve this effect when you pass an array as a data parameter to $.ajax.

Why not json_decode()? The answer is that older versions of web browsers do not support the JavaScript function JSON.stringify() that is equivalent to PHP's json_encode(), although there is a library to add support. One might consider it for a more plex case, but for such a simple case, it should not be necessary.

发布评论

评论列表(0)

  1. 暂无评论