Search code examples
phpformslistempty-list

Unable to save empty list


Need help to save empty list. I have 2 list which I can move to and from and save but when I remove last remaining item and want to save empty list it always save with last item so I can not save an empty list.

Here is the code for my 2 lists:

<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="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" >
<?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="masterlist" id="masterlist" value="">
<input type="submit" onclick="displayResult()" value="Save File" name="submit" >
</form>

And here is my Update.php file:-

<?php
header("Location:".$URL.'index.php');

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

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

This works for saving empty list but when I try to save empty list with multiple list it saves the previous list as empty . Wonder why?

<?php
header("Location:".$URL.'index.php');

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

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

if ($_POST['category1']) {
    $category1 = $_POST['category1'];
    $category1unique = array_unique($category1);
    sort($category1unique);
    foreach ($category1unique as $key => $value) {
        $resultcategory1.=$value. "\n";
    }
    file_put_contents('category1.csv',$resultcategory1);
}
if (empty($_POST['category1'])) {
    file_put_contents("category1.csv", "");
}

if ($_POST['category2']) {
    $category2 = $_POST['category2'];
    $category2unique = array_unique($category2);
    sort($category2unique);
    foreach ($category2unique as $key => $value) {
        $resultcategory2.=$value. "\n";
    }
    file_put_contents('category2.csv',$resultcategory2);
}
if (empty($_POST['category2'])) {
    file_put_contents("category2.csv", "");
}
?>

Solution

  • if ($_POST['masterlist']) will not succeed when the input is empty, since an empty string is falsey.

    If you want to check whether the input was submitted, use if (isset($_POST['masterlist']))

    You could also write an empty file when the input is empty:

    if (empty($_POST['masterlist'])) {
        file_put_contents("master.csv", "");
    

    With multiple forms, you need to change your code so it depends on which form was submitted.

    if (isset($_POST['form1'])) {
        $catkey = 'category';
        $filename = 'category.csv';
    } else if (isset($_POST['form2'])) {
        $catkey = 'category1';
        $filename = 'category1.csv';
    } else {
        die("Unrecognized form submitted");
    }
    
    if (empty($_POST[$catkey])) {
        file_put_contents($filename, "");
    } else {
        $category = $_POST[$catkey];
        $categoryunique = array_unique($category);
        sort($categoryunique);
        foreach ($categoryunique as $key => $value) {
            $resultcategory.=$value. "\n";
        }
        file_put_contents($filename,$resultcategory);
    }