Reputation: 327
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>
Upvotes: 2
Views: 2367
Reputation: 631
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
.
Upvotes: 2