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

    Join Date
    Jun 2002
    Location
    Pennsylvania, USA
    Posts
    129
    Rep Power
    18

    Specific "string" causing error in php/javascript


    Good morning

    I wasnt sure if this post should go in the clientside forum or here....so I flipped a coin. I'm having a very odd problem that I can't seem to figure out. I have a .php file that's purpose is to echo a javascript function so my list boxes are dynamically dependent. Here is a small piece of my code...
    PHP Code:
    ......    }else if(currentchoice == "class"){
            <?php  $sql "SELECT class.Name, class.ID FROM class WHERE class.ID < 16 ORDER BY class.Name";
            
    $result = @mysql_query($sql,$connection) or die("Couldn't execute query.");
            
    $num mysql_num_rows($result);
            echo 
    "document.myForm1.selectitem2.options.length = $num;\n";
            
    //populate class combo box with class names
            
    $i=0;
            while(list(
    $cname$cID) = mysql_fetch_row($result)){
                echo 
    "document.myForm1.selectitem2.options[$i].text = '$cname';\n"
                echo 
    "document.myForm1.selectitem2.options[$i].value = '$cID';\n";
                
    $i++;
            }
    ?>
            document.myForm1.selectitem2.style.visibility="visible";
            document.myForm1.selectitem3.style.visibility="hidden";
        }else if(currentchoice == "multiple"){
            <?php 
            $sql 
    "SELECT spells.Name, spells.ID FROM spells ORDER BY spells.Name";
            
    $result = @mysql_query($sql,$connection) or die("Couldn't execute query.");
            
    $num mysql_num_rows($result);
            echo 
    "document.myForm1.selectitem2.options.length = $num;\n";
            
    //populate class combo box with class names
            
    $i=0;
            while(list(
    $cname$cID) = mysql_fetch_row($result)){
                echo 
    "document.myForm1.selectitem2.options[$i].text = '$cname';\n"
                echo 
    "document.myForm1.selectitem2.options[$i].value = '$cID';\n";
                
    $i++;
            }
    ?>
            document.myForm1.selectitem2.style.visibility="visible";
            document.myForm1.selectitem3.style.visibility="hidden";
        }else if .........
    When I run my script I get a Javascript error - Error: Expected ";" on the page load. Subsequently, whenever this function fires ( which happens to be on the OnChange event of my select box ) I get the javascript error - Error: Object expected After doing some extensive fiddling, I have narrowed the problem down to this line :
    PHP Code:
    $sql "SELECT spells.Name, spells.ID FROM spells ORDER BY spells.Name"
    If I change the "FROM spells" to "FROM AnyOtherTable" the script executes flawlessly. I have tried assigning the word "spells' to a variable and using that variable in the $sql string instead, didnt work. I have executed the problem $sql query in phpMyAdmin and it returns the recordset I am looking for perfectly. I am at a loss as to what to try next short of changing the name of that particular table. Is "spells" some kind of keyword or something?

    Any ideas or suggestions would be greatly appreciated!
    Thanks!
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,934
    Rep Power
    4033
    One of the values in $cname or $cID probably contains a ' in it. Try using addslashes() on them before you echo them.

    PHP Code:
    echo "document.myForm1.selectitem2.options[$i].text = '"addslashes($cname), "';\n"

    echo 
    "document.myForm1.selectitem2.options[$i].value = '"addslashes($cID), "';\n"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Pennsylvania, USA
    Posts
    129
    Rep Power
    18
    That was exactly the problem! Thanks so much...I don't even want to speculate how long it would have taken me to think of that. I know I won't forget any time soon.

    Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo