Kyle
Kyle

Reputation: 67

PHP: $_POST of input that has a variable name

So, I'm having trouble figuring this one out. I populate a select box with values from Excel, and give the select box the name of the worksheet.

function populateDropDown($excelSheet) {
$objPHPExcel = PHPExcel_IOFactory::load($excelSheet, $num);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo '<select name= ' . $worksheetTitle . '>';
    for ($row = 1; $row <= $highestRow; ++ $row) {

        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            if ($col == 0) {
                $id = $cell->getValue();
            } else if ($col == 1) {
                $val = $cell->getValue();
            }
        }
        echo '<option value=' . $id . '>';
        echo $val;
        echo '</option>';
    }
    echo '</select>';
}
return $worksheetTitle;
}

At the end of this function, I return the title and it is stored into a variable on the form page.

<?php $selectBox1 = populateDropDown("workbook1.xlsx"); ?>

When the form is submitted, how can I get the data of this variable? I've tried the following without it working:

$var = $_POST[$selectBox1];
$var = $_POST['$selectBox1'];
$var = $selectBox1;

Hardcoding the sheet name works, however:

$var = $_POST['Sheet1'];

So, I'm positive it's a problem with the variable. I'm sorry if this has been asked before, I searched for quite some time but didn't find anything that helped. Thank you for any help!

Again, just to clarify my question, how can I use $_POST to get the data from the select box, when the name of the select box was named with a variable?

Edit: added entirety of populateDropDown function

Upvotes: 0

Views: 259

Answers (2)

DJC
DJC

Reputation: 1173

Why don't you also send a hidden value with the worksheet title, giving the hidden input a name that you DO know, then you can grab in from the $_POST array.

<input type="hidden" name="my-select-name" value="<?php echo $worksheetTitle;?>">

Then after the form is posted, in your processing script you can get the value and use it to identify your select box.

$value = $_POST['my-select-name'];

$selectdata = $_POST[$value];

Upvotes: 1

Martin
Martin

Reputation: 22760

What you want to do is reshape your naming of your select box, in HTML try making the select box value into an array:

<select name='[select][".$worksheetTitle."]'>
<option>...</option>
</select>

Then when you get the $_POST data, you can run a foreach through the array of [select] which will be an array of 1 value:

if (count($_POST['select']) > 0 ){
    foreach ($_POST['select'] as $key=>$value){
        $this_is_the_worksheet_name = $key;
        $this_is_the_worksheet_select_value = $value;
    }
}

This will give you access to the values, this is a little bit long winded but your question seems to be abridged, so I've worked a bit of a long way round. The reason to put the select choices into an array is that with foreach accessing the array key (thus the worksheet name) is trivial.

Upvotes: 1

Related Questions