Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not displaying code from .php script on html page

    Hello,

    I have an html script and a .php script.

    The .php requests a random word from a table of words in a mysql database.

    I wrote a html form to display the requested word in a text box.

    Here is my html file:

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    function myfunction()
    {
      var xmlhttp;
      if(window.XMLHttpRequest)
      {
        xmlhttp= newHttpRequest();
      }
      else
      {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      
    xmlhttp.onreadystatechange=function()
    {
      if(xmlhttp.readyState==4)
      {
        document.myLingo.word.value=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET", "get_Word.php",true);
    xmlhttp.send(null);
    }
    </script>
    <form name="myLingo">
    
    <input type="button" name="button" value="New Word" onClick="myFunction();" />
    <input type="text" name="word" />
    
    </form>
    
    </body>
    </html>
    and here is my .php script:

    Code:
    <?php
    
    
    $con=mysql_connect('localhost', 'xxxx', 'xxxx');
    if (!$con)
    {
      die('Could not connect: '.mysql_error());
      
    }
    
    mysql_select_db("my_lingo", $con);
    
    $sql=mysql_query("SELECT `word` FROM `words` ORDER BY RAND()") or trigger_error('Error: ' .mysql_error());
    
    $word=mysql_fetch_array($sql);
    
    echo $word[0];
    
    mysql_close($con);
    
    ?>
    When I run this in Internet Explorer; I get a error that is asking for an object on line 32 which is the line that has my button in the javascript.

    When i run it in firefox, I don't receive an error, but the page still does nothing.

    Any suggestions?
    The main function of the page is that, when I click the button, it's supposed to return me a random word from the .php script

    Thanks,
    jcjst21

  • #2
    New Coder
    Join Date
    Aug 2009
    Location
    London, England
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts
    you would. myfunction is defined as lowercase whereas the onclick has it as myFunction();

    javascript IS case sensitive (as you will find, are most languages)

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I fixed that part, but I'm still getting the same error....



    Message: Object expected
    Line: 12
    Char: 5
    Code: 0
    URI: http://localhost/Lingo/testLingo.html

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You need to learn how to DEBUG DEBUG DEBUG.

    Even with MSIE, you can use the MSIE Script Debugger.

    But better is to use Firefox with the Firebug plugin. A very complete debugger.

    You problem is here:
    Code:
        xmlhttp= newHttpRequest();
    See it?

    Where is the space after the keyword "new"??? And why is it "new HTTPRequest" when your IF test was looking for XMLHttpRequest?

    So:
    Code:
        xmlhttp = new XMLHttpRequest();
    Last edited by Old Pedant; 12-08-2009 at 02:01 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Aug 2009
    Location
    London, England
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts
    what is document.myLingo.word.value ?? you really should not access elements through DOM 0, chances are this is failing as its not an object (name="word" ?) for the assignment.

    anyway, can't really tell whats on your line 12, post an online example.

    in any case

    <input type="text" id="word" />

    ...

    document.getElementById("word").value = ....


    and yes the lack of spaces. get firebug and look at your console. also, use a js beautifier to fix code that looks dodgy and is hard to read - like this one
    http://fragged.org/javascript-beautify/
    regards

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Dmitar: What's wrong with
    Code:
    document.myLingo.word.value=xmlhttp.responseText;
    ????

    document.myLingo gives you are reference to the <form name="myLingo">
    and then .word references the element <input name="word"> in that <form>
    and then .value is the correct way to put data into a form field.

    That's standard (and ancient...back to Netscape 3) notation.

    It's true that giving a name to a <form> is deprecated in XHTML, but you *must* be able to give a name to an <input>, else the value of the field is not sent on to the next page when the <form> is posted. (Giving a field an ID does *not* impact whether it is sent to next page or not.)

    But in any case, giving a name to a <form> is not deprecated in standard HTML and is still supported in all browsers even in STRICT XHTML.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Aug 2009
    Location
    London, England
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts
    no, i agree - as i said, he's using DOM 0 to select the element and i am saying - for longevity of code's sake and as a old practice - probably he should not, that's all.


  •  

    Posting Permissions

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