#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    25
    Rep Power
    0

    Single and double quotes in form text input box?


    I am trying to display a form with an text input and display an initial value in the input box. All works well until the initial text value I want to use has a single or double quote in it. I have been trying every combination of addslashes stripslashes and everything else I can think of with no success. Here is an example:

    This is what I started with and worked fine until somebody entered the initial $field_value had a ' in it:
    <input type=text name=$field_name value='$field_value'>

    I tryied each of these:
    <input type=text name=$field_name value=$field_value>

    <input type=text name=$field_name value=\"$field_value\">

    <input type=text name=$field_name value=stripslashes($field_value)>

    <input type=text name=$field_name value=addslashes($field_value)>

    Some display the initial text upto the first space, some up to the first quote, so display nothing...

    Does anybody know the correct way to initialiaze a form text input box with a string that has spaces, single and double quotes? Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Windsor ON, Canada
    Posts
    462
    Rep Power
    18
    PHP Code:
    # inside php delimeters
    print "<input type=\"text\" name=\"$field_name\" ".
        "value=\"".stripslashes($field_value)."\">";
    # outside of php delimeters
    <input type="text" name="<?=$field_name?>" value="<?=$field_value?>">
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    25
    Rep Power
    0
    Sorry nopoints that does not solve the problem. It does not handle the case where the test has a word surrounded by double quotes. For example if

    $field_value = 'Bob said "hello"';

    print "<input type=\"text\" name=\"$field_name\" ".
    "value=\"".stripslashes($field_value)."\">";

    the line just shows the Bob said, the rest is truncated because it comes to the first " around hello and see that as the end of the field.

    Can using a text with spaces, ' and " in it to initialize a input box?

    Craig
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    you will have to convert those quotes to ascii text

    $field_value = "Bob said &amp;quot;hello&amp;quot;";
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    25
    Rep Power
    0
    Thank you scoutt! Yes, converting the quotes to HTML &quot worked. Are there any other characters that I will have to do this for?

    Thanks again!
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    shouldn't be, just single and double quotes. but it all depends on you and how you want to dispaly them.
  12. #7
  13. No Profile Picture
    Just Another Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Middle America
    Posts
    41
    Rep Power
    18
    how would someone go about converting these into ASCII characters on the fly? I need to be able to take a variable that is defined in a form field, add it to a MySQL database, and then be able to pull it from the SQL table and back into a form field. my users will not have any idea that they must use ASCII characters. what can i do to automatically replace quotation marks (' & ") with ASCII values?
    Jesus wept; Voltaire smiled. From that divine tear and from that human smile is derived the grace of present civilization.
  14. #8
  15. No Profile Picture
    Moderator/Software QA Engineer
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,650
    Rep Power
    2480
    use the htmlentities( $str ) function.
  16. #9
  17. No Profile Picture
    Just Another Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Middle America
    Posts
    41
    Rep Power
    18
    ok, htmlentities is working good. but i cannot get html_entity_decode to work. I copied the example right out of the PHP manual, but it gives me an error. This is the example they give:

    PHP Code:
    $orig "I'll \"walk\" the <b>dog</b> now";

    $a htmlentities($orig);

    $b html_entity_decode($a);

    echo 
    $a// I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

    echo $b// I'll "walk" the <b>dog</b> now 
    This is the error I get:
    Fatal error: Call to undefined function: html_entity_decode() in /www/htdocs/test.php on line 8

    What's happening here?
    Jesus wept; Voltaire smiled. From that divine tear and from that human smile is derived the grace of present civilization.
  18. #10
  19. No Profile Picture
    Moderator/Software QA Engineer
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,650
    Rep Power
    2480
    try this func:
    PHP Code:
    // For users prior to PHP 4.3.0 you may do this:
    function unhtmlentities ($string)
    {
        
    $trans_tbl get_html_translation_table (HTML_ENTITIES);
        
    $trans_tbl array_flip ($trans_tbl);
        return 
    strtr ($string$trans_tbl);

    then use it instead of html_entity_decode( ... )
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  20. #11
  21. No Profile Picture
    Just Another Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Middle America
    Posts
    41
    Rep Power
    18
    Yes!! That worked perfectly! Thanks!!

IMN logo majestic logo threadwatch logo seochat tools logo