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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Memphis, TN
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post XMLHttpRequest not starting PHP at server

    I have the following code in JAVASCRIPT:
    Code:
    function Set_XML(data)	{
    	alert('Start set')
    	XReq = new XMLHttpRequest()
    	XReq.onreadystatechange = XReady()
    	XReq.open('POST','chatroom.php')
    	alert('After open')
    	x = 'Lines='+Line+'&Last='+Last+data
    	XReq.send(x)
    	alert('After send')
    }
    function New(NewT)	{
    	x=NewT.trim()
    	if (x!='')	{
    		XReq.abort
    		x='&CHat='+encodeURIComponent(x)
    		Set_XML(x)
    		OChat.innerHTML=SoFar+"</p><p style='color:red'>Sending</p>"
    		alert('after clear')
    	}
    	OText.value=""
    	setTimeout((function() {OText.focus()}), 0);
    }
    function XReady()	{alert("Ready="+XReq.readyState)
    	if (XReq.readyState == 4)	{alert('request returning')
    		if (XReq.status != 200)	alert('XML status = '+XReq.status)
    		else {if (XReq.responseText)	{
    			eval(XReq.responseText)
    			OChat.innerHTML=SoFar+"</p>"
    	}	}	Set_XML()	}
    	
    }
    The Program 'chatroom.php' never starts on the server side - that program starts with code:
    Code:
    <?php
    session_start();
    	Force_error();  // no such function - trying to put error in log
    	if (array_key_exists ("ID",$_SESSION) and $_SESSION['ID'] != '') $ID=$_SESSION['ID'];	// user is logged on
    	else {
    		Do_Echo ("alert('Error - you are not logged on');window.history.go(-1);");
    		exit(8);	}
    Any idea what I am doing wrong?
    Last edited by vinyl-junkie; Sep 26th, 2017 at 03:07 PM. Reason: added code tags

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,345
    Thanks
    4
    Thanked 482 Times in 470 Posts
    You have XReq as a LOCAL variable to Set_XML, which means it doesn't EXIST inside XReady()... well, unless you declare it BEFORE Set_XML as a global FIRST and omitted that code.

    Also some semi-colons wouldn't hurt, particularly since in many instances they are NOT optional!

    Creating a function that's near identical in name -- New-- to a language construct -- new -- is a REALLY bad idea.

    But then so is using innerHTML instead of the DOM so...

    Likewise not seeing where "Line" is set, it's confusing that you're using CamelCase instead of camelCase for variables (since the leading cap implies it's a class nor a var/func), and of course you've got no legacy polyfill so which browser(s) are you testing on?
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  3. #3
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Memphis, TN
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post
    You have XReq as a LOCAL variable to Set_XML, which means it doesn't EXIST inside XReady()... well, unless you declare it BEFORE Set_XML as a global FIRST and omitted that code.

    Also some semi-colons wouldn't hurt, particularly since in many instances they are NOT optional!

    Creating a function that's near identical in name -- New-- to a language construct -- new -- is a REALLY bad idea.

    But then so is using innerHTML instead of the DOM so...

    Likewise not seeing where "Line" is set, it's confusing that you're using CamelCase instead of camelCase for variables (since the leading cap implies it's a class nor a var/func), and of course you've got no legacy polyfill so which browser(s) are you testing on?
    Sorry, code contains

    `<script>
    `var Line=0,Last=0,OChat,OText,SoFar='<p><b>System:</b> Top of chat',XReq

    Testing on IE and Firefox (ans Apache PHHP I think)
    Line is passed back and forth between Browser and PHP (which really sets it)
    Does not answer question why isn't it working

  4. #4
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Memphis, TN
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Col_Boogie View Post
    Sorry, code contains

    <script>
    var Line=0,Last=0,OChat,OText,SoFar='<p><b>System:</b> Top of chat',XReq

    Testing on IE and Firefox (and Apache PHP I think)
    Line is passed back and forth between Browser and PHP (which really sets it)
    Does not answer question why isn't it working
    Cleaned up reply

  5. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,345
    Thanks
    4
    Thanked 482 Times in 470 Posts
    Are you sure OChat is even set to something?

    Really though the use of innerHTML instead of the DOM, tracking markup you shouldn't need to track with that pointless memory wasting 'sofar' variable, non-semantic markup.... you've got some real problems in there but it's hard to say without seeing the full code. Snippets just don't tell the whole story as there's STILL a lot of stuff I'm not seeing that's kind of essential.

    ... and again, some semi-colons would be nice -- your lack of them could even be breaking things which is why omitting your line-ending semi-colons is a BAD idea. (and goes bits-up face-down under CSP or Strict)

    I'd probably also be doing console.log instead of alert, the pause from alert can screw up async comms.

    -- edit -- oh, duh.

    XReq.onreadystatechange = XReady()

    XReady is the function, do you really mean to run its RESULT?

    XReq.onreadystatechange = XReady;

    Would make a hell of a lot more sense, you should be assigning the function, not the function RESULT!

    That EVAL isn't exactly a brilliant thing to be doing either given the security holes it opens. You should NEVER be sending client side scripting as part of yer response. you want to send a failure, do it in the status codes! You want to fail logon, send a 401... much less that dicking with window.history for Christmas only knows what... and that line variable that seems to serve no purpose unless you're tracking stuff that has no business being tracked client-side.

    I'd really have to see a LOT more of this to weigh in more.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

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