0

I have following php code which create Drop-Down menu reading text file which contain name

files.txt

JAMES
MARK
TONY

drop-down.php

<?php
$path = "files.txt";
$file = fopen($path, 'r');
$data = fread($file, filesize($path));
fclose($file);

$lines =  explode(PHP_EOL,$data);
echo '<select name="file">';
foreach($lines as $line) {
  echo '<option value="'. urlencode($line).'">'.$line.'</option>';
}
echo '</select>';
?>

Here is the POST section of code, it is a loop

foreach ($_POST["bname"] AS $id => $value)
{
...
...   
USERNAME: "'.$_POST["file"][$id].'"

Everything working but when i submit data to other form i am getting only first letter of users like if i select JAMES and submit data i am only getting J letter in .$_POST. I want Full name JAMES, what is wrong in my code?

4
  • 1
    can you show us the script where you use $POST? Commented May 31, 2013 at 15:25
  • What is probably happening is you are saying I only want the first character of the string $_POST["file"][0] or something similar somewhere, but we need to see the part where you echo it out. Commented May 31, 2013 at 15:28
  • Yeah this worked fine for me. It must be what ever you're posting. Commented May 31, 2013 at 15:31
  • @sgroves - I have edit my question and add POST section Commented May 31, 2013 at 15:32

3 Answers 3

1

The name attribute of your select is wrong, must be:

echo '<select name="file[]">';

or

echo '<select name="file[someNumber]">';
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the Id, because that is saying "I want this value, and the second part says which character" (at least the way that you are using it). Try this:

"USERNAME " . $_POST["file"]

Strings are treated like arrays in PHP.

So if you had the string kitty

$string = "Kitty";
echo $string[0]; // prints "K"
echo $string[4]; // prints "y"

Comments

1

Try read the txt file with file(), it adds the rows directy into an array, then use for() to traverse the array.

<?php
$path = "files.txt";
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$nrl = count($lines);
echo '<select name="file[]">';
for($i=0; $i<$nrl; $i++) {
  echo '<option value="'. urlencode($lines[$i]).'">'.$lines[$i].'</option>';
}
echo '</select>';

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.