reading data from a text area

Hello every one,
I’ve recently started learning php and I’ve a fair understanding of the basics.
I’ve come across a problem where i need to read and display some data from a text area.(exactly what is written in the text area)

This simple code can help you understand my problem.

<form action="test.php" method="post">
<b>Test Area:</b><textarea rows="2" cols="135" name="var" wrap="physical"></textarea>
<input type="submit" value="Submit" />

test.php

<?php
$value = $_POST['var'];
// $test = explode("\n", $value);
// echo = $test;
echo $value;
?>

Basically if i type:

a
b
c
d,e
in the text area i want test.php to display the letters as it is.(not all in a single line).I’m sure ive missed something simple!!

Pls help

bump. I’m interested in this too.

Anyone got any ideas???

I’m sure it does display content of textarea “as is”, just don’t forget - if you’re outputting results as a web page (i.e. content type: text/html) you need to convert any “new line” to HTML’s
. There is a function in PHP that you can use: nl2br().

The code in your test.php file will look like this:
[php]<?php
$value = nl2br($_POST[‘var’]);
echo $value;
?>[/php]

Alternatively, you can use

 
tag to display pre-formatted text:
[php]<?php
$value = $_POST[‘var’];
echo ‘
’.$value.’
’;
?>[/php]
but this will be displayed in a fixed width font such as Courier, preserving any spaces, tabs, new lines etc.
This method is usefull for debugging. For example, if you have an array and want to view items, you can do this:
[php]<?php
$arr = array(‘a’, ‘b’, ‘c’, ‘d’);
echo ‘
’;
print_r($arr);
echo ‘
’;
?>[/php]

Thank you phphelp :slight_smile: ,the nl2br() function did the trick.

I’ve ran into another problem, what if i want to display just a certain letters when the form is submitted?

Consider my previous example:

a
b
c
d,e

If i just want to display “d,e” which is in the 3rd line.
Ive tried :
[php]echo $value[3];[/php] -->but it doesn’t work. Is there any other way?This is very essential for me, then i can insert what ever i want into a database from the textarea.

If you want to process text from textarea as a separate rows, just split it into array (I see you already have this in your code, commented out):
[php]$value = explode("\n", $_POST[‘var’]);

// display all rows
if(count($value)) foreach($value as $row){
echo $row.’
’;
}

// display 4th row only
echo $value[3];
[/php]

The foreach appears to be looping the statements in the text area multiple times.The first log (for example: “John Rambo age 41” is being inserted 4 times into the database :frowning: )
Further more its only inserting the first log (multiple times)
What am i doing wrong?

[code]

Name Details: [/code] name.php: [php]<?php include "config.php"; //Declare variables $value = $_POST['var']; if((is_null($value)) || ($value == "") || (is_null($value)) || ($value == "")) echo "Enter a Log"; else{ //$replace = str_replace(",","","$value"); $explode = explode(" ", $value); $value3 = $explode[0]; //first name $value4 = $explode[1]; //last name $value5 = $explode[3]; // age

if(count($value)) foreach($explode as $row){
//Insert into database
$sql = “INSERT INTO test (firstname,lastname,age) VALUES (’$value3’, ‘$value4’, ‘$value5’)”;

if (!mysql_query($sql)) {
die('Error: ’ . mysql_error());
}
}
echo “Data logged succesfully.”;
}
?>
[/php]

You do not need foreach loop here. You have 3 scalar variables: $value3, $value4, $value5 and you want to insert 1 record to database table - why do you do this in loop?

What if i want to insert multiple records into the database.For example if type in the textarea :

John Rambo age 41 Jake Sully age 30 Bruce Banner age 50
How would the code be and where would i use the foreach loop here?

In this case, you first need to split content into rows (using \n delimiter)… then within a loop for each row, split row by space delimiter. In your code I do not see - where you split content into rows?

Could you please provide me with a sample code ,for a better understanding.

Here is the sample code for you:
[php]<?php
// split content into lines
$rows = explode("\n", $_POST[‘var’]);

// for each row…
if(count($rows)) foreach($rows as $row){

// split row into tokens
$tokens = explode(" ", $row);

$value3 = mysql_real_escape_string($tokens[0]); //first name
$value4 = mysql_real_escape_string($tokens[1]); //last name
$value5 = mysql_real_escape_string($tokens[3]); // age

// insert record to table
$sql = “INSERT INTO test (firstname,lastname,age) VALUES (’$value3’, ‘$value4’, ‘$value5’)”;
if (!mysql_query($sql)) {
die('Error: ’ . mysql_error());
}

}
?>[/php]

Thank you phphelp for such a quick reply,it works perfectly well for me now :slight_smile:

I have come across another problem which i cannot figure out how to solve!!!
In the sample code which you provided ,if the $value3(first name) and $value4(last name) are same,i want it to echo a statement.Could you please tell me how to accomplish that.

Thanks in advance.

bump can i get some help with this please

<?php // split content into lines $rows = explode("\n", $_POST['var']); // for each row... if(count($rows)) foreach($rows as $row){ // split row into tokens $tokens = explode(" ", $row); $value3 = mysql_real_escape_string($tokens[0]); //first name $value4 = mysql_real_escape_string($tokens[1]); //last name $value5 = mysql_real_escape_string($tokens[3]); // age if($value3 == $value4){ echo ........ } else{ // insert record to table $sql = "INSERT INTO test (firstname,lastname,age) VALUES ('$value3', '$value4', '$value5')"; } if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } } ?>

I hope this what you want

yep that did the trick ,sry for such a late responce ::slight_smile:

I got another problem related to the script, what if i want to just read and display the text in the last line of the text area…is it possible to display only the last line of the text area?

[php]$lastrow = array_pop($rows);[/php]

Should i add that
[php]$lastrow = array_pop($rows);
[/php]
inside the for each loop ?

Sponsor our Newsletter | Privacy Policy | Terms of Service