What I'm trying to do is take the text from a textarea using the $_POST
method, and write each line with '-' at the start. This is what I got so far.
$lines = $_POST["textarea"];
foreach ($lines as $line)
echo " - " . $line . "<br />
";
(This is taken from php.net, I haven't programmed in PHP so long) When I run it, this is all I get:
Warning: Invalid argument supplied for foreach() in I:\xampp\htdocs\generate.php
Any help would be appreciated :)
$_POST["textarea"];
is no array. you have to split on the newline characters first:
$lines = explode("
", $_POST['textarea']);
foreach
expects an array as its first argument. You are passing in $lines
which is a string (possibly containing newline characters).
To process each line separately, first you have to split the input into an array of lines. You can do that with
$lines = explode("
", $_POST["textarea"]);
The function explode
splits the input string into an array of substrings delimited by whatever you pass as the first parameter.