how to access textarea in php ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jack001
Forum Newbie
Posts: 9
Joined: Sun Dec 10, 2006 2:48 am

how to access textarea in php ??

Post by jack001 »

hi guys

I have a form in which when a user doesnt enter any required field an alert message pop ups and when the user clicks on the OK button it goes back to the form and empties the textarea fields while the text fields remains unchanged...

is there any value attribute for textarea?

ple reply and help me

thnx in advance
Jack
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Something like

Code: Select all

<?php
$html['message'] = htmlentities($_POST['message'],ENT_QUOTES, 'UTF-8');
?>
<textarea name="message" id="message" cols="40" rows="6"><?php echo (isset($_POST['message'])) ? $html['message'] : ""; ?></textarea>
jack001
Forum Newbie
Posts: 9
Joined: Sun Dec 10, 2006 2:48 am

Post by jack001 »

matthijs wrote:Something like

Code: Select all

<?php
$html['message'] = htmlentities($_POST['message'],ENT_QUOTES, 'UTF-8');
?>
<textarea name="message" id="message" cols="40" rows="6"><?php echo (isset($_POST['message'])) ? $html['message'] : ""; ?></textarea>
I didnt get you

this is my code

<textarea rows="3" name="Address" cols="43" style="border-style: solid; border-width: 1px"></textarea></td></tr>


how to edit it??
plz reply
User avatar
Ind007
Forum Newbie
Posts: 14
Joined: Fri Dec 01, 2006 12:39 am
Location: India

Post by Ind007 »

Code: Select all

<html>

<head>
  <title></title>
</head>

<body>

<?php
if(isset($_POST['submit']))
{

// do some text field validatioin
	if($_POST['textarea1']=="")
	{
			$text_val=$_POST['text1'];
         echo "<script language='javascript'> alert('textarea cannot be empty');</script>\n";
         $texta_value="";
		}
		else
		{
		//YOU CAN TAKE ANY ACTION HERE

		}

}
		else
		{
			$text_val="";
			$texta_value="";
		}
?>





<form name='f1' method=post action='samepage.php'>
<input type='text' name='text1' value=<?php echo  $text_val;?>>
<textarea  name='textarea1' col5=2 rows=5 value=<?php echo $texta_value; ?>></textarea>
<input type='submit' name='submit' value='submit'>
</form>

</body>

</html>
but it is hardway of doing validation using php, you can make use of javascript to do this as time is the factor
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Ind007 wrote:but it is hardway of doing validation using php, you can make use of javascript to do this as time is the factor
I disagree. In php, especially if you are accessing a database you never trust user input and should always validate data, even if validation is also done using javascript. Even with simple tools (like firefox webdeveloper) javascript validation can be bypassed and potentially harmful data inserted.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Never rely on client side technology for security or validation. A user can easily turn off JavaScripting and then your validation is caput.
jack001 wrote:is there any value attribute for textarea?
Yes and no... the value you want for the text area goes between the <textarea> and </textarea> tags, as matthijs explained in his post. But there is not value attribute for textarea as you would have like in the <input> elements.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

What Would Be The Better You Think Using htmlentities

Or

Using A Client Side JavaScript To Pass TheText In Textarea To The Value attribute Of That Text Area On Every Keypress So That php Can Parse It Like An Input Element $_POST['Textareaname']
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

htmlentities()/htmlspecialchars() is pretty much required for all form field values, including text areas.

As for using Javascript to pass the text in a textarea to the value of the same textarea does nothing but set the text. You would need to use Ajax to send the information to PHP if you didn't want to refresh the page. At any rate, that's just a waste of time most likely. In fact sending the contents of the text area every key press would be a horrific CPU and bandwidth eater.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

No Ididn't Meant That
I Mean On KEYPRESS On the TEXTAREA The value of that TEXTAREA Will be = innerTEXT Of That textarea So The Textarea Will Have The Same Text In its Value
Ore In The Value of Any Other hidden Fields Value Attributes

And For This purpouse AJAX Is Not Required
Cause Its Not Semding The Data To The Server Its Sharing The Data In Client Side
Between two Attributes php Will Parse Its value Attribute Just Like An input Element

EDITED
Last edited by neel_basu on Tue Dec 12, 2006 11:50 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The text in the textarea and innerText are one in the same. Textarea tags do not have a value attribute, it is the text inside the container that is their value.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Well This Function Will Pass The Text To Value
================================

Code: Select all

function pass_val(ev)
  {
    var hld_txt = document.getElementById("tst").innerHTML;
    document.getElementById("hid_hld").value = hld_txt;
  }

Code: Select all

<form name="frm">
<textarea name="tst" rows="2" name="S1" cols="20" id="tst" onKeyUp="pass_val(event);"></textarea>
<br />
<input type="text" name="nm_hid_hld" size="20" id="hid_hld">&nbsp;
</form>
And here Is A Compleate Example
========================

Code: Select all

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Pass_TextArea_Text</title>
<script>
function pass_val(ev)
  {
    var hld_txt = document.getElementById("tst").innerHTML;
    document.getElementById("hid_hld").value = hld_txt;
  }
</script>
</head>

<body>

<form name="frm">
<textarea name="tst" rows="2" name="S1" cols="20" id="tst" onKeyUp="pass_val(event);"></textarea>
<br />
<input type="text" name="nm_hid_hld" size="20" id="hid_hld">&nbsp;
</form>
</body>

</html>
php Will Parse The Form Filed (hid_hld) not The Textarea

Code: Select all

$_POST[hid_hld]
If You Don't Want To Display The hid_hld Textbox You Can Make It Hidden

Code: Select all

<input type="hidden" name="nm_hid_hld" size="20" id="hid_hld">
jack001
Forum Newbie
Posts: 9
Joined: Sun Dec 10, 2006 2:48 am

Post by jack001 »

neel_basu wrote:Well This Function Will Pass The Text To Value
================================

Code: Select all

function pass_val(ev)
  {
    var hld_txt = document.getElementById("tst").innerHTML;
    document.getElementById("hid_hld").value = hld_txt;
  }

Code: Select all

<form name="frm">
<textarea name="tst" rows="2" name="S1" cols="20" id="tst" onKeyUp="pass_val(event);"></textarea>
<br />
<input type="text" name="nm_hid_hld" size="20" id="hid_hld">&nbsp;
</form>
And here Is A Compleate Example
========================

Code: Select all

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Pass_TextArea_Text</title>
<script>
function pass_val(ev)
  {
    var hld_txt = document.getElementById("tst").innerHTML;
    document.getElementById("hid_hld").value = hld_txt;
  }
</script>
</head>

<body>

<form name="frm">
<textarea name="tst" rows="2" name="S1" cols="20" id="tst" onKeyUp="pass_val(event);"></textarea>
<br />
<input type="text" name="nm_hid_hld" size="20" id="hid_hld">&nbsp;
</form>
</body>

</html>
php Will Parse The Form Filed (hid_hld) not The Textarea

Code: Select all

$_POST[hid_hld]
If You Don't Want To Display The hid_hld Textbox You Can Make It Hidden

Code: Select all

<input type="hidden" name="nm_hid_hld" size="20" id="hid_hld">

hi neel

can u adjust this in my php form???

plz dear
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

OK Post The php File

Have You Tested That Compleate Example ??
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: how to access textarea in php ??

Post by RobertGonzalez »

jack001 wrote:I have a form in which when a user doesnt enter any required field an alert message pop ups and when the user clicks on the OK button it goes back to the form and empties the textarea fields while the text fields remains unchanged...
Why are you emptying the textarea and leaving all the others unchanged? And what happens when javascript is disabled? Why not let the server handle this?

If this thread doesn't start to actually be about PHP Code I am going to move it to Client Side soon.
Post Reply