Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: echo a javascript with xmlhttp.open in it...

  1. #1
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default echo a javascript with xmlhttp.open in it...

    Ok this is the code:
    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can escape quotes with a backslash, but the prettiest way to do this is to use PHP's here-document syntax:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    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?
    Last edited by Null; 05-05-2006 at 08:37 AM.

  4. #4
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Still got an error. I now have this code (post the whole scriptcode now):
    Code:
    ...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

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Null
    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 Code:
    <!-- ... -->
    <?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.

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

  6. #6
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    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!!!
    Last edited by Null; 05-05-2006 at 10:19 AM.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    "sajax?"
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    "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

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, a toolkit. I see.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

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
  •