1

I'm trying to display a CSV file in a browser and print the selected (via radio button) row.

Here's my code to print the CSV file (with a radio button next to each row):

<?php

echo "<html><body><table border='1'>";

$f = fopen("data.csv", "r");
$row = 1;
echo "<form method='post' action='submit.php'>";
while (($line = fgetcsv($f)) !== false) {
   $to_print = implode(",", $line);  // added this and changed value below (to use it)
   echo "<tr><td><input type='radio' name='env[$row]' value='$to_print' /></td>";
   foreach ($line as $cell) {
      echo "<td contenteditable='true'>" . htmlspecialchars($cell) . "</td>";
   }
   echo "</tr>\n";
   ++$row;
}

fclose($f);
echo "</table><br><Input type='submit'></form></span></body></html>";

?>

I have a submit button at the bottom that (when clicked) should pass the data in the selected row to submit.php which should print it.

Here's the submit.php:

<?php

echo "<html><body>";

print_r($_POST);  // this prints what I need

if (! empty($_POST['env'])) {
    $j=0;
    foreach ($_POST['env'] as $env) {
        // Can't get anything to print in here
        ++$j;
    }
}

echo "</body></html>";

?>

But it prints nothing. I'm guessing it has something to do with the 'value' of my radio button:

value='<?php echo $line;?>]'

That can't be right...

Any suggestions would be great. Thanks!

6
  • $line is an array. If you want to echo it as a whole implode() it first. And you will get the column names first. Commented Aug 10, 2015 at 17:40
  • Your post name is more than env Commented Aug 10, 2015 at 17:40
  • Okay I added an implode (edited above) to my submit and still nothing... any other suggestions? Commented Aug 10, 2015 at 17:47
  • view your page source and post that. Commented Aug 10, 2015 at 20:11
  • May I suggest you avoid using echo for spitting out HTML as much as possible. ... ?> <tr><td><input type='radio' name='<? echo env[$row]; ?>' value='<? echo $line; ?>' /></td> ... It will make your HTML structure mode readable. Commented Aug 10, 2015 at 20:27

1 Answer 1

3

The problem with your code is this line:

echo "<tr><td><input type='radio' name='env[<?php echo $row;?>]'

You are echoing some HTML from within PHP. Inside the line your are echoing out, you are opening <?php tags again. Debugging what is being POSTed, you are actually just sending the string "<?php echo $row;?>"

To fix this issue, change your echo line to this:

echo "<tr><td><input type='radio' name='env[$row]' ...

As long as you are using double quotes, the $row variable will be interpreted and you'll get the outcome that you want.

Edit:

Once you have the data output to the page correctly, you need to reconsider how you are looping over the data.

The value of $line is set by [fgetcsv](http://php.net/manual/en/function.fgetcsv.php) which returns an array. You are setting this array as the value to the input like this: value=$line, but seeing as $line is an array, PHP is making an array-to-string conversion, and you're not getting what you expect.

Solution: Either pick a single index of the array to set as value (such as $value=$line[0]) or output the td within the inner loop, where you are looping over each field within the row.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay I changed that (edited above)... now I see that my row is being properly passed to the submit... but it's still not printing anything. Yet, if I add a print_r($_POST); to the top of my submit, I can see that my values are being passed. Does something need to be changed in the submit? Thanks for your help!
Using $value=$line[0] did work to print a single column. But I need the whole row... I updated my code above with comments. Almost there... just need to find a way to access the array in my submit loop. Thanks again!

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.