0

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

3
  • What are the <option> elements of that select? maybe you could transmit the name of the worksheet via <input type=hidden" name="worksheet" value="The Name" />? Commented Jul 9, 2015 at 22:45
  • populateDropDown obviously some framework we are not seeing here Commented Jul 9, 2015 at 22:47
  • From what I gather populateDropDown is a custom function from which the first 3 lines of code are taken. I think a more through description of what you're doing is necessary. Could you post the complete form code? I think you may have forgotten to make the form submission method POST. Commented Jul 9, 2015 at 23:30

2 Answers 2

1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I think this is the simplest way to do it.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.