How to add list items outside form in hidden field

I have 2 lists - master and category. When moving items from master list to category list I can save category list with new values but unable to save master list with removed items. I was thinking of means of reading master list into hidden field of category list but not sure how to go about. Need help with this and herewith my code:-

<select name=master[] id=master class="master" multiple="multiple" size='6'>
<?php   
    $file = fopen("master.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $master = $row[0];
?>
    <option value="<?php echo $master;?>"><?php echo $master; ?></option>
<?php
    }
?>
</select> 

<form action="" method="post">
    <input type=button class="master" name=b1 id=b1 value='Move >'>
    <input type=button class="master" name=b2 id=b2 value='< Remove'>&nbsp;&nbsp;&nbsp;&nbsp;
<select name=category[] id=category multiple="multiple" class=master>
<?php   
    $file = fopen("category.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $category = $row[0];
?>
    <option value="<?php echo $category;?>"><?php echo $category;?></option>
<?php
    }
?>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Save File" name="submit">
</form>

The move and remove function works so I am not including it but here is my js for writing to csv file.

<?php

if ($_POST['master']) {
$master = $_POST['master'];
foreach ($master as $key => $value) {
    $result.=$value. "\n";
}
file_put_contents('master.csv',$result);
}

if ($_POST['category']) {
$category = $_POST['category'];
$categoryunique = array_unique($category);
sort($categoryunique);
foreach ($categoryunique as $key => $value) {
    $result.=$value. "\n";
}
file_put_contents('category.csv',$result);
}
?>

How does this vary from your other post on a similar matter? How to post 2 list box values one form submit and save 2 different csv files

it is exactly the same i only need to save the master list box value after moveto or move from which i just cant get right so i made a new post to address this> paul suggested using ajax i spen the whole day reading and trying to implement but i could only save the selected remove item to be saved and not the whole list

Perhaps you could post the code you tried to use, so people can comment on why it isn’t working. I take it the “quick and dirty” suggestion I made didn’t work either.

I feel I am moving in the right direction but still can’t seem to reach my end goal. For now I have added function to get value by getElementById

<script>
function getValue()
{
  var master=document.getElementById("master");
  for (var i = 0; i < master.options.length; i++) {
     if(master.options[i].selected ==true){
          alert(master.options[i].selected);
      }
  }
}
</script>

I have added to my form onclick to target js

<input type="submit" value="Save File" name="submit" onclick="getValue()">

Now I only need to know how to save to master.csv as where I display now to to alertbox

alert(master.options[i].selected);

On a new approach I tried this but now I need to know how to add the value to my form

<form action="update.php" method="post">
    <input type=button class="master" name=b1 id=b1 value='Move >'>
    <input type=button class="master" name=b2 id=b2 value='< Remove'>&nbsp;&nbsp;&nbsp;&nbsp;
<select name=category[] id=category multiple="multiple" class=master>
<?php   
    $file = fopen("category.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $category = $row[0];
?>
    <option value="<?php echo $category;?>"><?php echo $category;?></option>
<?php
    }
?>
</select>
<input type="hidden" name="master" id="master" value="">
<input type="submit" value="Save File" onclick="displayResult()" name="submit" >
</form>

and here my js I have put alert(txt); in code to see if I get the right values which I do

<script>
    function displayResult() {
    var x = document.getElementById("master");
    var txt = "All options: ";
    var i;
    for (i = 0; i < x.options.length; i++) {
        txt = txt + "\n" + x.options[i].value;
    }
    alert(txt);
}
</script>

Now I need to pass txt to my form hidden field

Is that going to work? You get element by ID using “master”, but the element with id=master is your hidden form field, which is empty. So how do you get the correct things displayed, what is putting things into your hidden form field?

Thanxs for showing this out. I have changed the id=masterlist in hidden form field. Now I have added

document.getElementById('masterlist').value = txt;

after alert(txt);

If I submit my form it writes empty value This is my upload.php snippet

if ($_POST['masterlist']) {
$master = $_POST['masterlist'];
foreach ($master as $key => $value) {
    $result.=$value. "\n";
}
file_put_contents('master.csv',$result);
}

When I dump var I get this

array(3) { ["masterlist"]=> string(0) "" ["category"]=> array(7) { [0]=> string(5) "April" [1]=> string(8) "February" [2]=> string(7) "January" [3]=> string(4) "June" [4]=> string(5) "March" [5]=> string(3) "May" [6]=> string(5) "March" } ["submit"]=> string(9) "Save File" }

You might want to look at encoding the array before you put it into the hidden form field and decoding it in your PHP, I think that might be confusing matters. JSON-encoding might do it.

Yes, I got so far now by getting the value I must just see how to put contents in file. Herewith my update code.

<script>
    function displayResult() {
    var options = document.getElementById('master').options;

var values = [];
var i = 0, len = options.length;

while (i < len)
{
  values.push(options[i++].value);
}

txt=(values.join(', '));
    alert(txt);
    document.getElementById('masterlist').value = txt;
    }
</script>

This is the code that I use in my form:-

<form action="update.php" method="post">
    <input type=button  class="master" name=b1 id=b1 value='Move >'>
    <input type=button  class="master" name=b2 id=b2 value='< Remove'>&nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="masterlist" id="masterlist" value="">
<select name=category[] id=category multiple="multiple" class=master>
<?php   
    $file = fopen("category.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $category = $row[0];
?>
    <option value="<?php echo $category;?>"><?php echo $category;?></option>
<?php
    }
?>
</select>
<input type="submit" onclick="displayResult()" value="Save File" name="submit" >
</form>

When I echo $_POST[‘masterlist’]; I get following Results (comma separated values)

January, February, April, May, June, July, August, September, October, November, December

Now I must just find out how to put comma separated values with
command file_put_contents(‘master.csv’,$result);

explode() them into an array and foreach() through it to write each on an individual line?

Thanx, just did it

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.