SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question *HELP* - Problem I Would Suppose Need Both JavaScript & PHP background

    Hello,

    I am working on a PHP website (I am just a beginner, know little) and making a website where people can post comments for discussion and wanted it to make it possible so that these comments could of be placed on other peoples websites. Best way I thought was JavaScript, since other peoples servers don't always support PHP,Perl,etc. So, I just finished the script for both PHP and Javascript so it can be placed on peoples website. Just doing some beta testing and found out that if people place a apostrophe in there message it generates a error since I use the code

    <? echo "<script>document.write('$topic1');</script>"; ?>

    Placing a apostrophe in a message creates a "bug" in JavaScript. This will be vice-versa if I used quotes in the message, example:

    <? echo '<script>document.write("$topic2");</script>'; ?>

    Someone placing a quote character in there message will create a "bug" in JavaScript. ATTENTION: As you can see, this is includes PHP & Javascript together. If there is a way to enable this, your comment will be highly appriciated ... thanks Stephen!

  2. #2
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two possible approaches occur to me, but I don't have much experience with PHP. I can tell you that for your variable to be interpolated by PHP, the string will have to be in double quotes. You can either escape your single quotes with a \ (as in \') or include the escaped quotes as part of the string that is assigned to the variable.

  3. #3
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help JVLB!

    Can you please show me an example of "escaped quotes" please, thanks! I am new to this stuff and know little terminology -- all help is appriciated!

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Escaped single quote:
    \'

    Escaped double quote:
    \"

    That's all there is to it. The use of the backslash as the escape code is fairly universal in modern programming languages, starting (I believe) with C++ (it may have started with C, but I doubt it since many keyboards at the time of its development lacked the backslash, though it may have been added as the language evolved).
    Last edited by JVLB; Jun 24, 2004 at 08:01.

  5. #5
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I would have to verify each message posted it would be a pain in the neck. Is there a way to automate this process without having to check to see if there has been apostrophe(s) been placed in the message's please?

  6. #6
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All thanks go out to help!

    *****************************
    Thanks a million for your help JVLB
    *****************************

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try assigning the string (message) to a (JS) variable, and document.write()ing that instead.
    ::: certified wild guess :::

  8. #8
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help adios, but sorry to mention this method doesn't work ... just tryed it, but JS error occured meaning the JS thought I wanted it to end when somebody had a apostrophe in there message. Your message(s) is thanks a millions! Thanks for all helped received!!

  9. #9
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm at a bit of a loss as to where we are in this. You are getting input, in say a textarea, right? Then you need to parse the text to find quotes within that text and escape them so it can be passed to a PHP script which then passes it to a JavaScript document.write? Or is it directly manipulated in the JavaScript?

    Actually, either way it sounds like a job for Regular Expressions, but I'm a little unclear as to where to go from there.

  10. #10
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the option I am having a problem with the I "need to parse the text to find quotes within that text and escape them so it can be passed to a PHP script which then passes it to a JavaScript document.write" option -- thanks your clearifaction JVLB, thanks ... this might help people with the problem I am having. Thanks!

  11. #11
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Massachusetts
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks go all out to people who helped. I am happy to state I finally solved the problem. Searching my PHP books the last few days, not trying to give up I found the problem that will make the error no longer exist. Using a function called AddSlashes($VariableName) automatically adds slashes where apostrophe or quotes occur. Thanks to both JVLB & adios and to all the other members that took interest into this "Thread". Thanks a million!!

  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately it's not only quotes problem. Imagine someone writes "script.php?message=syntax%0Aerror"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •