how to read textarea value |
how to read textarea value |
akanae |
Feb 21 2014, 04:14 AM
Post
#1
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
Greetings to all,
i just need to know how to read textarea value one by one... for example: the input from textarea is " i like apple"... so i would like to read the value one by one start from "i" following by "like" and "apple"... the value will show to another page with difference value.. or example like this --->example the example that I've been search from Google only read value by line... so if anyone could help me..or provide links to any website referred... many thank's... |
Christian J |
Feb 21 2014, 09:26 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
You might split the string by spaces. You don't mention which scripting language you're using, but here's an example in javascript http://www.quirksmode.org/js/strings.html#split
|
akanae |
Feb 22 2014, 06:32 AM
Post
#3
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
actually i'm using PHP language...so i just need to split the string??
may i know..in this textarea if i want to read all the input in the textarea it is possible that the value include with the white space?? |
Christian J |
Feb 22 2014, 09:36 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
actually i'm using PHP language... Then I move the thread to the server-side scripting forum. QUOTE so i just need to split the string?? Yes, with PHP you might use the explode() function for that. Look it up at the excellent http://www.php.net/manual/en/index.php for details. QUOTE may i know..in this textarea if i want to read all the input in the textarea it is possible that the value include with the white space?? You mean if you want to put the array of exploded words back into a sentence again? Then you can use implode(). |
akanae |
Feb 23 2014, 12:08 AM
Post
#5
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
ouh okies...many thank's...
that help me...got it..^^ |
akanae |
Feb 23 2014, 12:08 AM
Post
#6
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
by the way...if text input from textarea and show the single string to another textarea can i use the implode()?
This post has been edited by akanae: Feb 23 2014, 12:19 AM |
Christian J |
Feb 23 2014, 08:47 AM
Post
#7
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
by the way...if text input from textarea and show the single string to another textarea can i use the implode()? No, for that you can just echo the content of the first TEXTAREA into the second. See http://www.php.net/manual/en/tutorial.forms.php The implode() function is meant to make a string from array elements. For example, the value of the variable $a below is the string "i like apple": CODE $a="i like apple"; Now I explode() it (using a space as delimiter), and store the resulting array as the value of variable $b: CODE $b=explode(' ', $a); Each array element in $b is now a separate string. To examine them you might use: CODE echo $b[0]; // "i" echo $b[1]; // "like" echo $b[2]; // "apple" or more conveniently use print_r(): CODE echo '<pre>'; print_r($b); echo '</pre>'; If you want to join the array elements back into the original string again, you can use implode() with a space as "glue": CODE $c=implode(' ', $b); and the result should yet again be "i like apple": CODE echo $c; |
akanae |
Feb 23 2014, 10:29 PM
Post
#8
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
okiedokie....thank's a lot...that help me a lot..many thanks..^^
|
akanae |
Apr 12 2014, 02:16 AM
Post
#9
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
Greetings all...
i have the problem again with the textarea input.. actually i use this code to read one by one word..which means read white space between the input.... QUOTE explode(' ',$_POST['input']); so the input will turn into array...they read one word input as one array... but the problem is..why the the warning appear like this if im just key in only one word? QUOTE Notice: Undefined offset: 2 in C:\xampp\htdocs\Try_1\translate.php on line 9 Notice: Undefined variable: word2 in C:\xampp\htdocs\Try_1\translate.php on line 148 and for the output it become like this..... QUOTE <b>Notice</b>: Undefined variable: translate3 in <b>C:\xampp\htdocs\Try_1\translate_home.php</b> on line <b>31</b><br /> right now im using conditionals if..elseif to read the array... but if im change to use while looping the textarea take several time to appear..and the error will appear like this... QUOTE Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\Try_1\translate.php on line 19 so any here can explain to me why it this happen?? it is any idea how to resolve this situation?? or perhaps i just need to make conditionals if...elseif for the other array?? or set the variable to read the other array?? |
Christian J |
Apr 12 2014, 07:21 AM
Post
#10
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Could you post the whole PHP script?
|
akanae |
Apr 12 2014, 10:45 AM
Post
#11
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
here is my full code....
CODE <?php ob_start(); if( $_POST['input'] ) { $words = explode(' ',$_POST['input']); $words_array = array($words[0],$words[1],$words[2]); // foreach ($word_array as $value) if($words_array[0] != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql = "SELECT key1 FROM abbrev WHERE words1 = '$words_array[0]'"; $result = mysql_query($sql, $conn) or die(mysql_error()); if (mysql_num_rows($result) >0) { while ($newArray = mysql_fetch_array($result)) { $words = $newArray['key1']; //$words1 =implode(' ', $words); } } else{ //echo ' '; $a = $words_array[0]; $words = $a; } } elseif($words_array[0] == " ") { $message1 = " "; $words = $message1; } if ($words != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql1 = "SELECT words2 FROM trabslate WHERE key2 = '$words'"; $result1 = mysql_query($sql1, $conn) or die(mysql_error()); if (mysql_num_rows($result1) >0) { while ($newArray = mysql_fetch_array($result1)) { $translate = $newArray['words2']; //$words1 =implode(' ', $words); } } else { $translate = $a; } }elseif ($words == " ") { $message1 = " "; $translate = $message1; } mysql_close($conn); include('translate_home.php'); } ob_end_flush(); ?> it there any opinion for the conditional or i just need to use looping?? This post has been edited by akanae: Apr 12 2014, 10:46 AM |
Christian J |
Apr 12 2014, 11:39 AM
Post
#12
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
That code example contains no PHP variables $word2 or $translate3. There might be other SQL-related errors as well.
|
akanae |
Apr 12 2014, 12:59 PM
Post
#13
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
|
akanae |
Apr 12 2014, 01:17 PM
Post
#14
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
so here im put there the variable into the code...
CODE <?php ob_start(); if( $_POST['input'] ) { $words = explode(' ',$_POST['input']); $words_array = array($words[0],$words[1],$words[2]); // foreach ($word_array as $value) if($words_array[0] != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql = "SELECT key1 FROM abbrev WHERE words1 = '$words_array[0]'"; $result = mysql_query($sql, $conn) or die(mysql_error()); if (mysql_num_rows($result) >0) { while ($newArray = mysql_fetch_array($result)) { $words = $newArray['key1']; //$words1 =implode(' ', $words); } } else{ //echo ' '; $a = $words_array[0]; $words = $a; } } elseif($words_array[0] == " ") { $message1 = " "; $words = $message1; } if ($words != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql1 = "SELECT words2 FROM translate WHERE key2 = '$words'"; $result1 = mysql_query($sql1, $conn) or die(mysql_error()); if (mysql_num_rows($result1) >0) { while ($newArray = mysql_fetch_array($result1)) { $translate = $newArray['words2']; //$words1 =implode(' ', $words); } } else { $translate = $a; } }elseif ($words == " ") { $message1 = " "; $translate = $message1; } if($words_array[1] != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql = "SELECT key1 FROM abbrev WHERE words1 = '$words_array[1]'"; $result = mysql_query($sql, $conn) or die(mysql_error()); if (mysql_num_rows($result) >0) { while ($newArray = mysql_fetch_array($result)) { $words1 = $newArray['key1']; //$words1 =implode(' ', $words); } } else{ $b = $words_array[1]; $words1 = $b; } } elseif ($words_array[1] == " ") { $message2 = " "; $words1 = $message2; } if ($words1!= NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql1 = "SELECT words2 FROM translate WHERE key2 = '$words1'"; $result1 = mysql_query($sql1, $conn) or die(mysql_error()); if (mysql_num_rows($result1) >0) { while ($newArray = mysql_fetch_array($result1)) { $translate2 = $newArray['words2']; //$words1 =implode(' ', $words); } } else{ $translate2 = $b;} } elseif ($perkataan1 == " ") { $message2 = " "; $translate2 = $message2; } if($words_array[2] != NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql = "SELECT key1 FROM abbrev WHERE words1 = '$words_array[2]'"; $result = mysql_query($sql, $conn) or die(mysql_error()); if (mysql_num_rows($result) >0) { while ($newArray = mysql_fetch_array($result)) { $words2 = $newArray['key1']; //$words1 =implode(' ', $words); } } else{ $c = $words_array[2]; $words2 = $c; } }elseif ($words_array[2] == " ") { $message3 = " "; $words2 = $message3; } if ($words2!= NULL) { $conn = mysql_connect("localhost", "root"); mysql_select_db("words"); $sql1 = "SELECT words2 FROM translate WHERE key2 = '$words2'"; $result1 = mysql_query($sql1, $conn) or die(mysql_error()); if (mysql_num_rows($result1) >0) { while ($newArray = mysql_fetch_array($result1)) { $translate3 = $newArray['words2']; //$words1 =implode(' ', $words); } } else{ $translate3 = $c; } }elseif ($words1 == " ") { $message3 = " "; $translate3 = $message3; } mysql_close($conn); include('translate_home.php'); } ob_end_flush(); ?> there are still have the problem when im just only key in one word into the textarea... the error QUOTE Notice: Undefined offset: 2 in C:\xampp\htdocs\Try_1\translate.php on line 8 Notice: Undefined variable: words2 in C:\xampp\htdocs\Try_1\translate.php on line 147 if im key in input 3 words into textarea there will be no error.... so im just curious why this happen?? its there in my code have set that user must key in 3 words?? how i want to unset the input??which means that user can key in input less than 3 words.... |
Christian J |
Apr 12 2014, 02:02 PM
Post
#15
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
|
akanae |
Apr 30 2014, 10:14 AM
Post
#16
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
sorry to interrupt again...
just want to know... right now my textarea input i'm using explode() to make the input read as array... so the input will be read one by one word...if there have the white space between the word.. what can i do if user insert input with enter key?? how to make the input word read in one by one.. example: input i like apple |
Christian J |
Apr 30 2014, 02:46 PM
Post
#17
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Perhaps you could use preg_split() instead of explode(). The former lets you split by a regular expression, which means you can check for any whitespace character:
CODE $words = preg_split("/[\s]+/", $_POST['textarea']); (where \s matches any whitespace character, including tabs and different operatings systems' types of line breaks, and + means one or more of them). |
akanae |
May 3 2014, 12:19 AM
Post
#18
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
many thanks...!
that works! |
akanae |
May 5 2014, 02:14 AM
Post
#19
|
Novice Group: Members Posts: 24 Joined: 21-February 14 Member No.: 20,404 |
one more things...
first of all that code work if i'm using CODE preg_split("/[\s]+/",$_POST['input']) may i know why my input cannot be read same way with the output?? for example input i like apple but output become like this i like apple p/s: i'm using difference textarea for input and output... |
Christian J |
May 5 2014, 08:41 AM
Post
#20
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
You mean you want to join the array items back into a string? Then you might use
CODE $string=implode("\n", $words); (where "\n" is a newline character). It gets more complicated if the input contains different kinds of whitespace, such as both blank spaces and newlines: CODE <textarea>i like apples</textarea> and you want to recreate the different kinds of whitespace in the output. |
Lo-Fi Version | Time is now: 25th April 2024 - 10:35 PM |