简体   繁体   中英

Convert Javascript array to PHP array

help! I am currently working on getting the content of by class name using Javascript and store them into an array. Display array into drop-down list using php. This page is .php

What i have done so far:

Javascript:

var array = new Array();
$('.headline-bar').each(function () {
    array.push(this.innerHTML);
    array.serializeArray();
});

PHP :

<?php
$arrays = json_decode($_POST['array'], true);   
 foreach($arrays as $name) { ?>
      <option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
  <?php
    } ?>

Html:

<select>
    <option selected="selected">All</option>
      <?php
    $arrays = json_decode($_POST['array'], true);   
     foreach($arrays as $name) { ?>
          <option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
      <?php
        } ?>
    </select> 

    <input type="submit" value="Submit">

     <form action="#" id="release_year" method="post" > 
     <div class="headline-bar">2015</div> 
     <div class="headline-bar">2014</div>   
    </form>

You shouldn't use the serializeArray method but JSON.stringify() function to convert your array to a JSON string.

var array = new Array();
$('.headline-bar').each(function () {
    array.push(this.innerHTML);
});
var arrayJson = JSON.stringify(array);

Here the JSON string representing array is stored in arrayJSON .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM