2

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>
1
  • 1
    The question is why? Why not pass the JS object to a Javascript function to populate the drop down? Commented Apr 22, 2016 at 8:33

1 Answer 1

2

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.

1
  • 1
    Was just about to say this :P Commented Apr 22, 2016 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.