1

I'm trying to print multiple values user has selected on form submit. However with following what I'm seeing is only the last element printed irrespective whether it is selected or not.

Note that the print on the screen I'm looking at is a print that a layman can understand!

    <?php
    if(isset($_POST['submit'])) {
    //I'm trying to show the user these are the values you've selected
    print_r($option['name']);
    }
    ?>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
        <td class="container">
            <select multiple name="mercha_A[]" class="selectpicker form-control" title="Merchandiser type">
      <?php foreach ($options as $option) { ?>
            <option value="<?php echo $option['value']; ?>" <?php echo (isset($_POST[ 'mercha_A']) && in_array($option[ 'value'], $_POST[ 'mercha_A'])) ? ' selected="selected"' : ''; ?>>
                <?php echo $option['name']; ?>
            </option>
    <?php } ?>
    </select>
        </td>
        <td><button type="submit" name="submit">Submit</button></td>
    </form>

Anyone needs a coffee on my account?

Mohan Wijesena
  • 225
  • 1
  • 3
  • 11

5 Answers5

3

wrap your print_r in <pre> Tags

echo "<pre>";
print_r($option['name']);
echo "</pre>;
JiiB
  • 1,432
  • 1
  • 12
  • 26
2
echo "<pre>";
print_r($_POST['mercha_A']); // you have to print the name attribute not option
echo "</pre>;
Sanjit Bhardwaj
  • 893
  • 7
  • 13
1

depending on your situation, you could use either of these, I think the last will best suite those who don't have a programming background. Because, I think JSON is a human readable format.

Method 1:

echo '<pre>'; print_r($_POST['mercha_A']); echo '</pre>';

Method 2:

echo json_encode($_POST['mercha_A']);
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28
0

I think you meant to print

$_POST['mercha_A'];

Otherwise, $option['name'] is completely undefined in your case, but even if you put the print_r() at the end of the script, it would only be the name of the last option in $options.

In order to make print_r() readable, you can View Source (Ctrl+U) in your browser, or wrap it in <pre></pre> tags.

Ynhockey
  • 3,845
  • 5
  • 33
  • 51
  • You're right! however even if I wrap the `print_r()` with a `
    ` tags I'm seeing an output like this --> `Array
    (
        [0] => Hello
        [1] => World
    )` . How do I print that so one sees it as "Hello World"?
    – Mohan Wijesena Jan 25 '18 at 12:44
  • That depends on what you want to do. To concatenate strings in an array use `implode()`, but this is not always the most desirable way to print arrays. – Ynhockey Jan 28 '18 at 14:13
0

Using extbase debugger from TYPO3.

Check it out it's insane :) https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/extbase/Classes/Utility/DebuggerUtility.php

It helps you to debug arrays and object in a readable way

DebuggerUtility::var_dump($array)
Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28