Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 356447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:06:46+00:00 2026-05-12T12:06:46+00:00

I have an multi-dimensional array that I want to send to a PHP script

  • 0

I have an multi-dimensional array that I want to send to a PHP script with a Javascript that parses the JSON data and plot it on Google Maps. I’m trying to simulate it using forms:

<?php
$jsontest = array(
    0 => array(
        'plate_no' => 'abc111',
        'longlat' => array(121.003895,14.631563),
        'info' => 'first item'
        ),
    1 => array(
        'plate_no' => 'abc222',
        'longlat' => array(121.103895,14.731563),
        'info' => 'second item'
        )
    );
$jsonarray = json_encode($jsontest);
?>
<form action="json-target.php" method="post" accept-charset="utf-8">
        <input type="hidden" name="jsonarray" value="<?php echo $jsonarray; ?>" id="jsonarray">
    <p><input type="submit" value="Continue &rarr;"></p>
</form>

json-target.php looks like this:

<?php
    print "The value of \$_POST is ";
    print_r($_POST);
?>

And the output of $_POST is Array ( [jsonarray] => [{ ). I wanted to pass the contents of the $jsonarray variable to a Javascript function (please see update below).

UPDATE: I also have a simple Javascript that’s supposed to parse the value received from $_POST and post the value via alert():

<script src="/js/json2.js" type="text/javascript" charset="utf-8"></script> 
<script type="text/javascript" charset="utf-8">
    var json = JSON.parse(<?php echo $_POST['jsonarray'] ?>);
    for (var i = 0; i < json.length; i++) { 
        alert(json[i]); 
     }
</script>

But the output is mangled with backslash characters.

var json = JSON.parse([{\"plate_no\":\"abc111\",\"longlat\":[121.003895,14.631563],\"info\":\"first item\"},{\"plate_no\":\"abc222\",\"longlat\":[121.103895,14.731563],\"info\":\"second item\"}]);

What’s a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:06:46+00:00Added an answer on May 12, 2026 at 12:06 pm

    JSON encoding makes extensive use of quotes. By simply outputting a JSON encoded string into your HTML value attribute, the quotes will interfere with the markup. They need to be escaped to be put into the HTML. Try this:

    <input type="hidden" name="jsonarray" value="<?php echo htmlspecialchars($jsonarray,ENT_QUOTES); ?>" id="jsonarray">
    

    Edit: In response to your update, I’m not sure what your JSON.parse is supposed to be doing. Anything encoded with json_encode() in PHP is technically a valid Javascript object, and doesn’t need to be parsed any further. If I had an object named $obj with a name property of ‘hello’, I could do this:

    <script type="text/javascript">
    var o = <?php echo json_encode($obj); ?>;
    alert(o.name);
    </script>
    

    and get an alert saying ‘hello’. The output of json_encode is a perfectly suitable javascript object.

    The fact that the output of your $_POST array has been escaped with slashes leads me to think that perhaps your magic_quotes_gpc directive is set to be on. If that’s the case, you’ll have to unescape your $_POST variables with stripslashes().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error you're getting is caused by your Name column… May 12, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer You can't insert into collections that use wildcard types. This… May 12, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer I've never used SimpleModal, but from the examples on their… May 12, 2026 at 11:00 pm

Related Questions

How can I take a multi-dimensional array like the below, and split it into
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi dimensional array. The only actual values (other than other arrays)
I have what amounts to a multi-dimensional array. int[][][] MyValues; What I want is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.