Trouble with csv file (was: Unable to remove last item from listbox)

I am able to move items from master list box to license list box, also able to remove items from license list box to master box but unable to remove last item from license box list so list box is empty. Need help with this

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

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

This is my js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
    $("#b3").click(function(){
    $("#master option:selected").each(function() {
    $("#licenses").append("<option>"+$(this).text()+"</option>");
    $("#master option[value= '"+ $(this).val() + "' ]").remove();
    });
    $('#licenses option').prop('selected', true);
    });

    $("#b4").click(function(){
    $("#licenses option:selected").each(function() {
    $("#master").append("<option>"+$(this).text()+"</option>");
    $("#licenses option[value= '"+ $(this).val() + "' ]").remove();
    });
    $('#licenses option').prop('selected', true);
    });
    })
    </script>

This option you’re appending has no value attribute, so it cannot be found by

(Additonal Hint: Your code has already selected $("#licenses option[value= '"+ $(this).val() + "' ]")… That’s what $(this) is… but you should still make your options homogenous.)

Thanx for reply I don’t understand, the code works perfectly I can remove items one by one or by multiple selection and save until the last one it gets removed in the listbox but when saved the last item remains on page refresh

okay… that’s not what your opening post states. So… can only fix the problems you tell us about.

What does your save code look like?

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

you’ve got a blank line at the end of your csv, which i think may be causing your problem.

Instead of the foreach, implode your array using “\n” instead, and see if that works.

removed empty line in csv still same problem tried implode instead of loop still same problem

I’m moving this to the PHP forum to get some csv help with the PHP part of the code.

thanx

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