PHP - Returns/new Lines In Textarea Text?
I have a form that is processed using PHP (post method) and there is a textarea people will enter text into.
After processing, the text is displayed to the user. However, if the user did a Carriage Return (i.e. hit Enter) when they originally entered the text, that text does NOT show on a new line when displayed later. For example, if they type this into the text area... "Hello there (they hit Enter here) How are you (they hit Enter here) Good I hope" It will display like this, all on one line: "Hello there How are you Good I hope" In other words, it doesn't seem to remember that Enter was hit. Is there some way to "preserve" the fact that someone hit the "Enter" key, so that when text is displayed later, it will show the text on a new line if the user in fact hit "Enter" when originally typing it. I am probably not using the right terms in the right spots (i.e. newline, carriage return, enter, etc) but I think you get the picture Let me know if anyone knows how to do this. Thanks! Similar Tutorialsi have this in a form Code: [Select] <textarea id='textarea' name='tweet' maxlength=140 > <?php if(isset($_SESSION['description'])) { echo $_SESSION['description'];} ?> </textarea> the session variable is just their input and i use that to repopulate the form if the validation script catches an error with their input. The problem is when the user enters carriage returns and then if theirs an error.... this... "blah blah blah" turns into this... "blah blah\r\n\r\nblah" how do i remove that junk? Good morning to all. Recently I just read a post here , it's about how to insert different lines from <textarea> to different mysql rows . http://www.phpfreaks.com/forums/index.php?topic=325239.0 Thanks to AbraCadaver , his code works well. Quote $lines = array_map('mysql_real_escape_string', explode("\n", $_POST['textarea'])); $values = "VALUES ('" . implode("'), ('", $lines) . "')"; $query = "INSERT INTO table_name (column_name) $values"; But now if have more than one textarea , such as area1,area2 and area3 , how should change the code so that all data can be inserted into database simultaneously? Such as data of area1 is for column 1, row 1, data for area2 is for column2 , row 1, and data of area3 is for column 3 ,row 1. Is it possible to make this works? Thanks for every reply . Hello, is it possible to insert somehow information from textarea to mysql? For example i have 3 lines in text area: John Tacker is a bad guy Tom Tacker has a big nose Ted Tacker is the same person as John Tacker and i want to insert them in table's different rows I have a string that I run through nlbr2(), nlbr() works fine but when I apply the css property text-indent it only indents the first line of text. So it looks like xxxxxxxxxxx xxxxxxxxxxxxxx (carriage return) xxxxxxxxxxxxxx xxxxxxxxxxxxxx And I want the text to look like this xxxxxxxxxxx xxxxxxxxxxxxxx (carriage return) xxxxxxxxxxx xxxxxxxxxxxxxx How do you text-indent with carriage returns ? Thanks in advance S I need to read all the rows from a text file between two specific lines into an array where I can process them. I then need to do the same thing again until the end of the file. The file is too large to read the entire file into an array. The number of rows that are between the key lines are variable. This is what I have so far to read the first set of rows into the array but it is only reading the first row when in fact in this case there are 15. Any pointers as to where I am going wrong please? if ($_POST['srch']=="Import") { //open file for reading $family_rec=[]; $file_handle = fopen($fn, "r"); if(!$file_handle){echo "Could not open file";} while(!feof($file_handle)) { $line = fgets($file_handle); if ((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)) { $famref = explode("@", $line); $line = fgets($file_handle); //echo $line."<br>"; do{ $family_rec[]=$line; $line = fgets($file_handle); //echo $line."<br>"; } while((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)); print_r($family_rec); }//end of family }//end of while not eof }//end of import
Hi All, If someone types in my textarea without any hard returns, it echos the entire text on a single line, which expands my site very very far and screws up the layout. how can I echo the text but limit the number of characters per line before I insert my own line break? The code is just: Code: [Select] echo $message; I have a .txt file containing hundreds of lines of Names (e.g. Brad, Jennifer, etc.) What coding can I use to only echo the first 20? Hey guy, I really need your help with this. I have Input from a text area, in this format: a b c d e f g 12 34 435 124 What I need do to is to output only a certain line from every block of text, every block is divided by one or more empty lines. I have done this: $input = $_POST["input"]; $lines = explode("\r",$input); $num = count($lines); $line_to_extract = $_POST['line_to_extract']; for($i=0;$i<$num;$i++) { if($i == $line_to_extract) { echo 'Line '.$i.': '.$lines[$i].'<br />'; } } What I need to do is to detect the empty line so I can keep track of the line to extract. PLEASE HELP!!! I have the following text file: Code: [Select] 09/16/2011 - 09/16/2011 323780048 FedEx Ground Shipment Detail Report 09/22/2011 CAFE2472 Page: 1 TRACKING # RECIPIENT CD ZIP ZN CARRIER SVC BILL WGT COD DECL VALUE C GR CHG C SRCHG C NET ---------------- --------------- ------ --- ------- - --- -------- ---------- ---------- ---------- ---------- ---------- 561895962528473 97913 440607 4 Ground , RH 2.0 0.00 99.00 6.22 2.45 8.27 561895962528480 97928 352424 5 Ground , RH 17.0 0.00 99.00 9.89 2.45 10.60 561895962528497 97943 857164 6 Ground , RH 17.0 0.00 99.00 12.38 2.45 14.75 561895962528503 97955 193825 5 Ground , RH 4.0 0.00 99.00 7.17 2.45 8.42 561895962528510 6792 491069 3 Ground , RH 3.0 0.00 99.00 5.97 2.45 8.27 561895962528527 97967 030703 5 Ground , RH 1.0 0.00 99.00 5.75 4.10 10.06 561895962528534 97978 665429 4 Ground , RH 4.0 0.00 99.00 6.86 4.10 10.06 561895962528541 97992 371844 4 Ground , RH 1.0 0.00 99.00 5.51 4.25 10.22 561895962528558 98005 386108 4 Ground , RH 18.0 0.00 99.00 9.21 4.25 12.01 GRAND TOTALS COURTESY NET CHARGE 92.66 TOTAL WEIGHT(OF LBS) 63.7 TOTAL WEIGHT(OF KGS) 0.0 CSY DECL VAL SCHG 0.00 COURTESY DISCOUNT 12.52 COURTESY SPECIAL FEES 28.95 CSY FUEL SURCHARGE 7.27 PACKAGE COUNT 9 And I'm attempting to pull ONLY the tracking number and reference number (or order #) from this file. I've been able to get rid of the empty lines, get rid of the first few lines I don't need and grab just tracking and reference numbers , but I can' t figure out how to get rid of of the last few lines....here's my code: Code: [Select] $arr=file("/home/onest4/public_html/beta/fedex.txt"); $x=1; foreach($arr as $str) { if (trim($str) != '') if ($x > 3) { list($tracking,$ordernumber)=explode(" ",$str); echo $tracking; echo "<br />"; echo $ordernumber; echo "<br /><br />"; } else { $x++; continue; } } Here's the result: Code: [Select] 561895962528473 97913 561895962528480 97928 561895962528497 97943 561895962528503 97955 561895962528510 6792 561895962528527 97967 561895962528534 97978 561895962528541 97992 561895962528558 98005 GRAND TOTALS COURTESY NET CHARGE TOTAL WEIGHT(OF LBS) TOTAL WEIGHT(OF KGS) CSY DECL VAL SCHG COURTESY DISCOUNT COURTESY SPECIAL FEES CSY FUEL SURCHARGE PACKAGE COUNT Anyone help with this? Thanks! Extremely new here and to PHP and I have exhausted my newbie resources and my brain on this one. Concept: Cron Job to INSERT 5 lines per hour (Not the whole list at once) Script to read a pipe delimited text file (about 5k Lines) Open File Get last (bottom) 5 lines of file, ignore the rest Insert those 5 lines into my database Open File, Remove last (bottom) 5 lines I am able to do parts 1-3 but not part 4 (removing last 5 lines) Please help me! Code: [Select] $data = array_slice(file('data.txt'), -5); foreach($data as $value) { $fields=explode("|",$value); $num=count($fields); for ($n=0;$n<$num;$n++) { $null=""; print ($n==0) ? "Importing Video...<BR>" : $null; } // Insert into tmpvideos table here include("includes/config.php"); $last_id_result = @mysql_query( @"SELECT ext_id FROM tmpvideos ORDER BY ext_id+0 DESC, id DESC" ); if ( 0 < @mysql_num_rows( $last_id_result ) ) { $last_id = @mysql_result( $last_id_result, 0, "ext_id" ) + 1; } else { $last_id = "1"; } copy($fields[3], "thumbs/ex_".$last_id.".jpg" ); $fields[3] = "thumbs/ex_".$last_id.".jpg"; set_time_limit(0); mysql_query("INSERT into tmpvideos (id, ext_id, title, description, duration, category, date_added, thumb, video, views, rating, votes, status) VALUES ('','".$last_id."','$fields[0]','$fields[1]','$fields[5]','$fields[6]',NOW(),'$fields[3]','$fields[2]','','','','1')"); } I understand the 'concept' but am unable to grasp the code! I have tried many snippets and really need help. I have a textarea and a button, I would like by click a button, the text within the textarea will save as a .txt file and downloadable by user, how to do it with php?? Thanks!! Hi, very simple i'm sure... i have a code for a text box to pull info into from a mysql table. easy enough: Code: [Select] <?php echo "<input type=\"text\" id=\"b_info\" name=\"b_info\" value=\"$b_info\" style=\"width: 327px\" /text>"; ?> but suppose i dont just want a "line of text" i want one of those boxes with the scroll bar down the side (lol like the one im writing this in ) how do i change the above code to do it? i though doing this: Code: [Select] <?php echo "<input type=\"textarea\" id=\"b_info\" name=\"b_info\" value=\"$b_info\" style=\"width: 327px\" style=\"height: 185px\" /textarea>"; ?> would work, but obviously not, all ive done is make the box bigger easy workaround? Thanks Each status has a comment textarea.
How can I get it to alert what the user has typed in the textarea when the return key is pressed.
The textarea in the PHP loop is like this:
<textarea class='commentbox' id='" . $thisid . "'></textarea>The reason why I am asking this and not how to save it to the database etc is because I think I will be able to do that. Thanks, I have a form that generates html code, and displays it in a textarea with submit button. I want to submit button to take the data entered in that textarea1 on page 1, and upload to textarea2 on page 2 How can I do this? I have no idea where to start. Hello, i have a query that returns the rows of certain parts of a databse. i made a button on the end of each echo, to make sure that, the button being printed with each echo, correlates to that echo. However, the SESSIONS only return the same varaibles each time. The last in the list. $sql = "SELECT p.Title, p.PerfDate, p.PerfTime FROM performance AS p INNER JOIN production AS r ON p.Title = r.Title"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo '<tr> <td>'.$row['Title'].'</td> <td>'.$row['PerfDate'].'</td> <td>'.$row['PerfTime'].'</td> <td><button name="availability" value="'.$row['Title'].'">Availability</button></td> </tr>'; $_SESSION["Title"] = $row['Title']; $_SESSION["PerfDate"] = $row['PerfDate']; $_SESSION["PerfTime"] = $row['PerfTime']; } } else { echo "0 results"; } $_SESSION["name"] = $_GET["name"]; ?> How am i able to make it so each button correlates to each row?
There's a screenshot of how it looks for reference Edited December 8, 2019 by AdamSteele I'm getting frusted over an if else statememt that won't work. It is calling one variable from the table. If the variable is in the table it will display one thing, if the variable is absent, it should display something else. I've tried !empty, isset, even $num_rows =1, but the content in the else doesn't display. Here's the code: Code: [Select] <? $result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { extract($row); if (!empty( $MyCode )) { echo "here is the code word"; } else { echo "you need to go back and get the code"; } $row_count++; } mysql_close; ?> When the code variable is correct the "here is the code word" displays. when the code variable is incorrect, what should appear as "you need to go back and get the code" is just blank. Any thoughts on how to structure the query in such a way that eh incorrect text appears? Thanks, this is supposed to display the distinct first two characters of a string(billnmbr) in a combobox ive tried this on phpmyadmin and it returns 08.. which is correct when i run this on my server, it returns nothing.. there must be something wrong with the way i called the billnmbr does anybody have an idea on how i could call and display this correctly? thanks and have a nice day. <?php $mquery = "select distinct(substring(`billnmbr`, 1, 2)) from `tbltest`"; $mres =mysql_query($mquery) or die(mysql_error()); while($mrow=mysql_fetch_array($mres)){ $mm = $mrow['billnmbr']; echo "<option value=" . $mrow['billnmbr'] . ">"; echo $mrow['billnmbr'] . "</option>"; } echo " </select>"; ?> I'm hoping someone can help me cos my hair's going white with this one. I'm trying to put together a script that acts as web-based interface to an SQL server. There are actually two parts, admin.htm and admin.php. The first part is just a form that passes login credentials to the PHP file. That part seems to work fine, but I'll post the source anyway: - Code: [Select] <!DOCTYPE HTML> <html lang="en"> <head> <title>SQL admin login</title> <meta charset="iso-8859-1" /> </head> <body> <form action="admin.php" method="post"> <label for="username">Username: -</label> <br /> <input type="text" name="username" id="username" /> <br /> <br /> <label for="password">Password: -</label> <br /> <input type="password" name="password" id="password" /> <br /> <br /> <label for="server">Server: -</label> <br /> <input type="text" name="server" id="server" /> <br /> <br /> <label for="database">Database: -</label> <br /> <input type="text" name="database" id="database" /> <br /> <br /> <input type="submit" value="Login" /> <input type="reset" value="Reset" /> </form> </body> </html> Following is the content of admin.php. By this point I can see the connection in MySQL Workbench, and when I submit the query 'SELECT * FROM subscribers' it's being stored in '$_POST['query']', but 'mysql_query($_POST['query'],$_SESSION['con']);' is returning nothing. There is definitely a record in that table, and the user I'm logging on with has permission to run the 'SELECT' command against this database, so I can't figure out why mysql_query(); is returning nothing: - Code: [Select] <!DOCTYPE HTML> <?php session_start(); if(!$_SESSION['con']) { if(!($_POST['username'] || $_POST['password'])) { if(!($_SESSION['username'] || $_SESSION['password'])) { $error="Username and password variables empty."; } } else { $_SESSION['username']=mysql_real_escape_string($_POST['username']); $_SESSION['password']=mysql_real_escape_string($_POST['password']); $_SESSION['server']=mysql_real_escape_string($_POST['server']); $_SESSION['database']=mysql_real_escape_string($_POST['database']); $_SESSION['con']=mysql_pconnect($_SESSION['server'],$_SESSION['username'],$_SESSION['password']); if(!$_SESSION['con']) { $error="Failed to connect to server."; } else { $database=mysql_select_db($_SESSION['database'],$_SESSION['con']); if(!$database) { $error="Failed to connect to database."; } } } } if(!$_POST['query']) { $error="No query submitted."; } else { $result=mysql_query($_POST['query'],$_SESSION['con']); if(!$result) { $error="Query returned nothing."; } } ?> <html lang="en"> <head> <title>SQL admin interface</title> <meta charset="iso-8859-1" /> </head> <body> <form action="admin.php" method="post"> <textarea name="query" rows="10" cols="50">SELECT * FROM subscribers</textarea> <br /> <br /> <input type="submit" value="Submit query" /> </form> <?php if($error) { echo $_POST['query']."<br /><br />".$result."<br /><br />".$error; die(); } else { while($row=mysql_fetch_assoc($result)) { echo $row['name']." ".$row['email']; echo "<br />"; } } ?> </body> </html> Can anyone help? MOD EDIT: [code] . . . [/code] tags added. im not new enough for this to take so long to figure out, but for some reason $rs = mysql_query("SELECT * FROM cl"); returns the last record instead of all the records in the table. anyone know why it wouldnt take everything? I am trying to unserialize this string: a:3:{s:3:\"zip\";s:5:\"55068\";s:4:\"city\";s:9:\"Rosemount\";s:5:\"state\";s:2:\"MN\";} but I unserialize is returning false. Why is it doing that? $info = unserialize($_COOKIE['zipcode']); var_dump($info); |