Page 1 of 1

how to access textarea in php ??

Posted: Tue Dec 12, 2006 6:29 am
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

Posted: Tue Dec 12, 2006 6:46 am
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>

Posted: Tue Dec 12, 2006 7:54 am
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

Posted: Tue Dec 12, 2006 8:00 am
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

Posted: Tue Dec 12, 2006 8:23 am
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.

Posted: Tue Dec 12, 2006 7:19 pm
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.

Posted: Tue Dec 12, 2006 11:33 pm
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']

Posted: Tue Dec 12, 2006 11:41 pm
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.

Posted: Tue Dec 12, 2006 11:47 pm
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

Posted: Tue Dec 12, 2006 11:50 pm
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.

Posted: Wed Dec 13, 2006 12:27 am
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">

Posted: Wed Dec 13, 2006 1:45 am
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

Posted: Wed Dec 13, 2006 7:16 am
by neel_basu
OK Post The php File

Have You Tested That Compleate Example ??

Re: how to access textarea in php ??

Posted: Wed Dec 13, 2006 10:42 am
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.