Jump to content

problem with textarea when reading to table


ee

Recommended Posts

I have a html form that submits results to a text file. This is then opened and read into a table producing a new line for each post in an new page. My problem is when a user hits enter in the textarea (Details) on the initial form, this seriously throws out my table when reading the results from the  text file, spreading the lines from the 'details' textarea in diff cells of the table.

If the user doesnt hit enter in the initial textarea there is no problem on the initial textarea. CAN ANYBODY HELP ME!?!?

 

$myFile = "details.txt";

$fh = fopen($myFile, 'a') or die("can't open file");

$stringData = $fname. "\t".$lname. "\t".$phone. "\t".$positiontype. "\t".$details. "\t".$education. "\t".$TofD."\n";

fwrite($fh, $stringData);

fclose($fh);

 

and then it is placed in table line by line:

 

 

echo "<table border=1 bgcolor = \"#000000\" bordercolor = \"#333333\"> \n";

echo "<tr><th>First Name</td>

    <th>Surname</td>

    <th>Phone No</td>

    <th>Position Type</td>

    <th>Details</td>

    <th>Education</td>

    <th>Time of Day to Contact</td>   

    </tr>";

for ($i=0; $i<$number_posts; $i++)

//split the lines

$line = explode ( "\t", $test[$i] );

echo "<tr><td>$line[0]</td>

    <td>$line[1]</td>

    <td>$line[2]</td>

    <td>$line[3]</td>

    <td>$line[4]</td>

    <td>$line[5]</td>

    <td>$line[6]</td>

</tr>";

}

echo "</table>";

?>

I think I understand the problem correctly.  Let me restate: When a user submits data, sometimes they have line breaks in the textarea that get transmitted and screws up your text file by making posts span multiple lines.

 

Many ways to fix this.

 

Sanitize input data (any one of these):

  • Convert the new lines submitted in the text area to breaks - This will allow you to preserve the line breaks and have them printed in the results (hint: http://us2.php.net/str_replace [search: \n] [replace: (break tag)])
  • Use the same method above to replace with a space (" ") or null ("") - This will remove line breaks.

 

*Note: You may have to adjust the TEXTAREA attributes to prevent sending "\n" with line wraps (unless you want it).

 

Description

The wrap attribute is used to define how text will wrap in the text box. Possible values are "off", which disallows word wrapping, "physical", which allows word wrapping and send the text with line breaks in tact when transmitting the data using scripting, and "virtual", which show word wraps in the box but sends the data as one long string of text when transmitting the data with scripts. 

 

<textarea wrap="virtual">YOUR TEXT HERE</textarea>

 

 

Other Potential Fixes: Use a database to store your data, Use XML to markup your textfile.

 

Best, Nathan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.