Add ' to textarea

Posted on 2006-04-15
Last Modified: 2013-12-12

I've got a quesiton a bit like Q_21585750.html.  I've got a website where entries from textareas are saved to a MySQL database, and can later be viewed and edited.

I've got some code to reduce the risk of errors/malicous entries.   I use the code in a function applied to all text entered in the website (that may come from textareas or text boxes). Also use it to check any text variables picked up using $_GET.

  $pattern = "/[^a-zA-Z0-9\.\!\-\_\?\@\\r\,\ \)\(\']/";
  $value=preg_replace($pattern, "", $value);

My first problem was that the MySQL database generated errors when strings contained the ' character.  Can resolve that by adding the following rows to the function:

  $pattern2 = "/[']/";
  $value=preg_replace($pattern2, "\'", $value);

However, when I reload the information in a textarea to edit it, it gets cut short at the point where the ' appears.  Just wondering if there's a good way to get round this problem?



Question by:hew1
    LVL 17

    Expert Comment

    Hi hew1,

    There is an easier way! A few functions work, the most significant being:

    Joe P
    LVL 17

    Expert Comment

    Hmm, I may have read it a little wrong.  I read you putting information into the database, instead of the nice $pattern2 that you have, you could use the mysql_real_escape_string() putting the information into the mysql table.
    Example usage:
    $str = "Joe's String has a single qoute!";
    $sql = "INSERT INTO table VALUES('mysql_real_escape_string($str)', 'Joe P')";

    But it looks like your doing the opposite, going from a database into a textfield or likewise.  Well, I would like to see how your doing that, because it shold work if you have a textarea/textfield like so:
    <textarea><?php echo $valueFromTheMysqlTable ?></textarea>
    Likewise for a textfield:
    <input type="text" value="<?php echo $valueFromTheMysqlTable ?>>

    Joe P

    Author Comment

    Thanks!  Have added following code to function that cleans up text before it is saved.

    I've got two text areas where the text is displayed after being saved, a "veiw" form and an "edit" form.  The view form now works fine (ie displays ' ).  Code below:

      <td width='65%' rowspan='3' class="td1"><textarea name="dispusercomment" cols="<?php echo  $cols?  
      >" rows="<?php echo $userrows ?>" readonly>Service Report: <?php echo $usercomment ?></textarea></td>

    However, for some reason the edit form still cuts the text off at the first ' .  Code below:

      <td class="td2" <?php if($r==1 and $usercomment==NULL) echo " bgcolor='#FFFF99'"?>>
      <textarea name="usercomment" cols="70" rows="10"><?php echo $usercomment?></textarea></td>

    LVL 17

    Expert Comment

    For safer code, both should be:
    <?php echo $usercomment; ?>
    semicolons are important in php, not needed (but perferred) in javascript.

    Try that, tell me if you get the same results,
    Joe P

    Author Comment

    Thanks, have made change.  Noticed another possible problem I hadn't mentioned while I was doing that.
    $usercomment is passed from the view form to the edit form using the following line:

    <input type='hidden' name='usercomment' value ='<?php echo $usercomment; ?>'>

    may need to be?

    <input type='hidden' name='usercomment' value ="<?php echo $usercomment; ?>">
    LVL 17

    Accepted Solution

    That would be the best way to do it, notice then you need to watch for double quotes now too.
    Try something like:
    <input type='hidden' name='usercomment' value ="<?php echo mysql_real_escape_string($usercomment); ?>">
    <input type='hidden' name='usercomment' value ='<?php echo addslashes($usercomment); ?>'>

    Double quotes or single quotes won't really matter once you escape all the quotes.  I would suggest trying the top one first, then addslashes if that doesnt work, add slashes escapes everything that could cause a problem.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now