Page 1 of 1
alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 1:11 pm
by egg82
Okay, so for now this works:
Code: Select all
<form action="./" method="post" id="redir">
<textarea maxlength="255" rows="1" cols="17" name="chat_version" class="overflow:hidden;" wrap="off" onKeyPress="submitenter(event)">test</textarea>
</form>
<script type="text/javascript">
function submitenter(e){
var keycode;
if(window.event){
keycode = window.event.keyCode;
}else if(e){
keycode = e.which;
}else{
return true;
}
if(keycode == 13){
document.getElementById('redir').submit();
return false;
}else{
return true;
}
}
</script>
(I hope you have an HTML syntax)
What this does is simply turn a <textarea> into a <input type="text">
Why would I do this? The reason is this:
As i'm sure everyone knows, a user can input literally anything into a field. After they hit "submit", it's our job to sort it out and throw away or change the "bad"
The problem with text input: to auto-populate it, we use value="". So what if the user entered a "? WHOOPS! No matter how you code or form it - without changing the character - your nice neat form just wound up destroyed.
This is why I chose the textarea. However, it's large and a bit sloppy, so if anyone has any ideas on how to allow every character under the sun into a text input without destroying it, let me know. It would be appreciated

Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 3:59 pm
by phpHappy
How does a question mark destroy your form?
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 4:11 pm
by twinedev
egg82 wrote:The problem with text input: to auto-populate it, we use value="". So what if the user entered a "? WHOOPS! No matter how you code or form it - without changing the character - your nice neat form just wound up destroyed.
Code: Select all
<input type="text" name="txtField" id="txtField" value="<?php echo htmlspecialchars($aryData['Field'],ENT_QUOTES); ?>" />
And generally, since there is a lot of repeated info, I usually use a function such as: (note, I usually also have it custom per app so that it will also produce the <label> and also the Error information, which is stored in $aryErr[$field])
Code: Select all
function echoInput($field,$type='text',$other='') {
global $aryData;
$value = (isset($aryData[$field])) ? htmlspecialchars($aryData[$field],ENT_QUOTES) : '';
echo '<input type="',$type,'" name="txt',$field,'" id="txt',$field,'" value="',$value,'" ',$other.' />';
}
At the very least, I use the following function which is in most of my apps, as it is less typing and makes the "template" sections easier to read
Code: Select all
function echoHSC($text) {
echo htmlspecialchars($text,ENT_QUOTES);
}
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 5:16 pm
by egg82
I never even knew that function existed. Now i'm going to have to edit all my code again @.@
anyway, thanks. I didn't want to put custom javascript a hundred times on every page
oh, isn't setting a variable as global "bad"? Shouldn't one simply use $_GET and $_POST?
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 6:08 pm
by twinedev
This is not setting $aryData to be global, this is telling the function that it can use the variable outside it's scope. In my apps $aryData contains the raw useable data, not just items that may have been posted. (and I also use $aryLists to hold any list of items for drop downs, so in my function to generate a <SELECT> it does global $aryData,$aryList;)
I normally do not use the global exact for a set list of items all over, like $aryData $aryLists and $aryErr . Otherwise normally I will pass it into the function (and if will be large amount of data, or need to change it, pass it by reference instead of value)
-Greg
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 6:56 pm
by egg82
my, it seems C/C++ is becoming more and more apparent in PHP.
If you wanted to alter the variable outside the function, why not just have it return($aryData) and force $array = echoInput($array)?
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 7:10 pm
by twinedev
The thing is, that for the example I gave, the function not only needs the value of the $aryData['whatever'] to echo out for
value= but also the name of the index for use in the
name= and
id=, so this is just saving me from doing
echoInput('whatever',$aryData['whatever']);, it is just easier to be able to pass it the index, and let it use the main data. I try not to use
global too often, but this is a case where I like it.
As for the returning of data, for small things that will work, but as you start working with more information, it starts wasting memory. If $aryData is an array that has 2000 elements, doing:
now has to create a local copy of that whole array, and then process it back to the original where if I do it by reference, I can just do:
Code: Select all
processData($aryData);
function processData(&$array) {
// Do what we need to $array, and it is changing $aryData
}
and then it is just working with original data and modifying that. memory use cut in half in regards to this array.
Re: alternative to turning textarea into input text
Posted: Tue Jan 03, 2012 8:43 pm
by egg82
well done, it seems you know your stuff.
I will have a use for that in the near future, and I once again find myself thanking you for saving me hours of searching and reading