...

View Full Version : Not displaying code from .php script on html page



jcjst21
12-08-2009, 01:15 AM
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:




<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:




<?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

Dimitar
12-08-2009, 02:32 AM
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)

jcjst21
12-08-2009, 02:37 AM
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

Old Pedant
12-08-2009, 02:56 AM
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:


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:


xmlhttp = new XMLHttpRequest();

Dimitar
12-08-2009, 03:11 AM
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

Old Pedant
12-08-2009, 03:35 AM
Dmitar: What's wrong with

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.

Dimitar
12-08-2009, 04:06 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum