Someone789 Posted June 27, 2008 Share Posted June 27, 2008 Hi, I would like to define PHP variables from a multiline textarea box. For example, I have the code: <form action="output.php" method="post"> Type Here: <textarea type="text" name="name" rows="10" cols="15" /></textarea> <input type="submit" /> </form> Upon pressing the Submit button, I need the text from each line in the text box to be made into a separate variable. For example, say I typed the following into the text box: Red Green Blue Orange When I press submit, I would like the text on the first line to be defined as $line1, the text on the second line to be defined as $line2, the text on the third line to be defined as $line3, etc. So that would make "Red" as $line1, "Green" as $line2, "Blue" as $line3, and "Orange" as $line4. Then, the output.php file would look like this: <?php echo "The first line you typed was $line1"; echo "The second line you typed was $line2"; echo "The third line you typed was $line3"; echo "The fourth line you typed was $line4"; ?> Which would display in the browser as: The first line you typed was Red The second line you typed was Green The third line you typed was Blue The fourth line you typed was Orange Thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/112147-defining-variables-from-a-multiline-textarea-box/ Share on other sites More sharing options...
Rowno Posted June 27, 2008 Share Posted June 27, 2008 You could use the explode function to break the text lines into an array like this: <?php $line = explode("\n", $_POST['name']); echo "The first line you typed was " . $line[0]; echo "The second line you typed was " . $line[1]; echo "The third line you typed was " . $line[2]; echo "The fourth line you typed was " . $line[3]; ?> Link to comment https://forums.phpfreaks.com/topic/112147-defining-variables-from-a-multiline-textarea-box/#findComment-575747 Share on other sites More sharing options...
Someone789 Posted June 27, 2008 Author Share Posted June 27, 2008 Works great, thanks! Only one last thing - I do want to specify a condition for if a line is left completely blank. Basically I want to make a function that says it will echo the "the first line you typed was.." phrase as long as the line is *not* left blank. Here's what I have so far. However, I bolded the part where I need to put something that would specify a blank line in the text box, so that it would echo correctly as long as the line was not empty, in which case it would echo a "line error". All I need is something to replace my bolded question marks which would signify that the line has been left blank. <?php $line = explode("\n", $_POST['name']); if ($line[0] != ??) { echo "The first line you typed was " . $line[0]; else echo "Line 1 error"; } if ($line[1] != ??) { echo "The second line you typed was " . $line[1]; else echo "Line 2 error"; } ?> Link to comment https://forums.phpfreaks.com/topic/112147-defining-variables-from-a-multiline-textarea-box/#findComment-575758 Share on other sites More sharing options...
Rowno Posted June 27, 2008 Share Posted June 27, 2008 Try this: <?php $line = explode("\n", $_POST['name']); if ($line[0] != "") { echo "The first line you typed was " . $line[0]; } else { echo "Line 1 error"; } if ($line[1] != "") { echo "The second line you typed was " . $line[1]; } else { echo "Line 2 error"; } ?> That should work. That will make it look for the content of "" which is nothing, so it will find lines with nothing on them. Oh and by the way, you were missing some { }'s in your if statements so I added them in. Link to comment https://forums.phpfreaks.com/topic/112147-defining-variables-from-a-multiline-textarea-box/#findComment-575865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.