PDA

View Full Version : echo a javascript with xmlhttp.open in it...



Null
05-04-2006, 11:10 PM
Ok this is the code:

echo '<script type="text/javascript">
<!--
var updateTimeout;
more code, more code, more code

xmlHttp.open('GET', 'xml.php?xpos='+xPos+'&ypos='+yPos+'&random='+Math.random(), false);
xmlHttp.send(null);

more code, more code, more code
// -->
</script>';
Well shortened the code a bit but the bold line gives this error:
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /../test/page1.php on line 62

The problem is in the ' tag, how can I fix this? Tried many options using " and '" but nothing worked...

Thx

Twey
05-05-2006, 12:22 AM
You can escape quotes with a backslash, but the prettiest way to do this is to use PHP's here-document syntax:

echo <<<ENDOFSCRIPT
<script type="text/javascript">
var updateTimeout;
more code, more code, more code

xmlHttp.open('GET', 'xml.php?xpos='+xPos+'&ypos='+yPos+'&random='+Math.random(), false);
xmlHttp.send(null);

more code, more code, more code
</script>
ENDOFSCRIPT;Please... spare us the unnecessary comment delimiters :) Any browser that doesn't do Javascript these days will ignore the <script> tag anyway. Unless you're planning on people visiting your site being stupid enough to use, say, IE 2, you've nothing to worry about. :)

Null
05-05-2006, 09:26 AM
Please... spare us the unnecessary comment delimiters :) Any browser that doesn't do Javascript these days will ignore the <script> tag anyway. Unless you're planning on people visiting your site being stupid enough to use, say, IE 2, you've nothing to worry about. :)

Ow.... these "unnecessary comment delimiters" where in a tutoral, I am no pro you know.... but ok I can delete these too then.

A question about <<<ENDOFSCRIPT. You have this at the start of the script??? doesn't this have to be <<<STARTOFSCRIPT or something like this? Doesn't find this logical......

Greetz

EDIT: after some research If found out you can use anything you like as a syntax, as long as it is the same. So instead of <<<ENDOFSCRIPT I can also use <<<END or <<<HOUSE etc and also close with it like HOUSE;

Is this correct?

Null
05-05-2006, 10:01 AM
Still got an error. I now have this code (post the whole scriptcode now):


...some html and other tags here...
<?php
for ($i=0; $i<sizeof($user->online); $i++)
{
echo <<<END
<script type="text/javascript">
var updateTimeout;
SET_DHTML(CURSOR_HAND, "'.$user->online[$i]['name'].'");

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

updateTimeout = setTimeout("checkPos()", 1000);

function my_DropFunc() {
xPos = dd.obj.x;
yPos = dd.obj.y;

xmlHttp.open('GET', 'xml.php?xpos='+xPos+'&ypos='+yPos+'&random='+Math.random(), false);
xmlHttp.send(null);

updateTimeout = setTimeout("checkPos()", 1000);
}

function my_DragFunc() {
clearTimeout(updateTimeout);
}

function checkPos() {
xmlHttp.open('GET', 'xml.php?random='+Math.random(), false);
xmlHttp.send(null);

response = xmlHttp.responseXML;
xPos = response.firstChild.getAttribute('xpos');
yPos = response.firstChild.getAttribute('ypos');

dd.elements.'.$user->online[$i]['name'].'.moveTo(xPos, yPos);
updateTimeout = setTimeout("checkPos()", 1000);
}
</script>
END;
}

?>
</body>
</html>
I get the error:
Parse error: parse error, unexpected $ in /test/page1.php on line 80

Line 80 = </html>

They've lost me here :confused:

mwinter
05-05-2006, 10:53 AM
Ow.... these "unnecessary comment delimiters" where in a tutoral [...]There are many poor tutorials on the Web. Be careful about what you read.


A question about <<<ENDOFSCRIPT. You have this at the start of the script??? doesn't this have to be <<<STARTOFSCRIPT or something like this? Doesn't find this logical......The identifier Twey chose might be a little misleading, but no, you don't. The identifier after the angle brackets indicates what the PHP interpreter should be looking for to end the string.


If I were you, I'd save yourself the hassle and just drop out of PHP parsing mode:



<!-- ... -->
<?php
for ($i = 0; $i < sizeof($user->online); ++$i) {
$name = $user->online[$i]['name'];
?>
<script type="text/javascript">
var updateTimeout;

SET_DHTML(CURSOR_HAND, "<?php echo $name; ?>");

/* ... */

function checkPos() {
xmlHttp.open('GET', 'xml.php?random='+Math.random(), false);
xmlHttp.send(null);

response = xmlHttp.responseXML;
xPos = response.firstChild.getAttribute('xpos');
yPos = response.firstChild.getAttribute('ypos');

dd.elements.<?php echo $name; ?>.moveTo(xPos, yPos);
updateTimeout = setTimeout("checkPos()", 1000);
}
</script>
<?php
}
?>
<!-- ... -->
The syntax error in the code you posted occurred because the PHP interpreter couldn't find the closing instance of the identifier. It must start on the first column. No whitespace or other characters are permitted before it.




if (window.XMLHttpRequest) { // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
That's exceedingly simplistic and prone to failure. Consider using the getRequestObject function (http://www.dynamicdrive.com/forums/showpost.php?p=22008&postcount=5) I defined in a previous post (if you use it, include the declaration for the global variable, as well!). You also seem to ignore the case where the XMLHttpRequest object is not available, providing no fallback.

Your code seems to contain several undeclared global variables. All variables, both local and global, should be declared with a var statement.

Mike

Null
05-05-2006, 11:04 AM
Hi,

Thx for the reply, I am going to try using the php in the javascript (didn't know this could be done).

About:

Your code seems to contain several undeclared global variables. All variables, both local and global, should be declared with a var statement.
This is true, cause you don't have the whole code. Thats gonna be part 2 of my problem, uploading coordinates to a users dbtable (which is no javascript question :)). Think I need sajax for that.... If any1 has experience with sajax and want to help me, plz say so. :rolleyes:

But for now I need to fix this problem (step by step ;)). I will report back as soon as I have tested it.

Many thx

UPDATE
Tried it and it works like a charm. THX!!!

Twey
05-05-2006, 11:39 AM
"sajax?"

Null
05-05-2006, 11:46 AM
"sajax?"
Yes, currently I can move an image on my site and it's coordinates (on drop) are saved using xml and AJAX. It writes the coordinates to a xml file and updates it everytime the image is moved. The problem is, this can only be done with 1 image. I want it so that every image has is own unique id (done that by using the id as $name) and the coordinates must be written to a db instead of the xml file.

I think I can use SAJAX best here. More info about sajax:
http://www.modernmethod.com/sajax/

Greetz

Twey
05-05-2006, 12:24 PM
Ah, a toolkit. I see.

Null
05-05-2006, 12:29 PM
Now that we are talking about it anyway, shall I ask my sajax question here or do I have to make a new topic? I already asked a simular question on the sajax forum, but it's spammed to the death and have no reply yet (and it has been weeks now)...

Let me know

Twey
05-05-2006, 12:32 PM
I think you should make a new topic.
The identifier Twey chose might be a little misleadingWell no, not really. <<<ENDOFSCRIPT should be read as "from here to ENDOFSCRIPT," in which case my choice of identifier makes perfect sense.

Null
05-05-2006, 12:36 PM
I think you should make a new topic.

Ok and where do I make this new topic? Under javascript, or php or something else?

Twey
05-05-2006, 12:39 PM
Javascript, methinks.

mwinter
05-06-2006, 11:48 AM
The identifier Twey chose might be a little misleadingWell no, not really.I did use the word 'might' and the phrase 'a little' for a reason. :p


<<<ENDOFSCRIPT should be read as "from here to ENDOFSCRIPT," in which case my choice of identifier makes perfect sense.Should, certainly, and I had no problem with that. However, the concept of boundary identifiers isn't universal (though it's also part of MIME). This is why an identifier containing the word 'end', appearing at the start, might be a little confusing to some. :)

Mike

Twey
05-06-2006, 12:09 PM
Heh, fair enough :)