Jump to content

javascript, calling php and getting a return


wilsoc31
Go to solution Solved by kicken,

Recommended Posts

Any help would be appreciated here.. im not sure what im doing wrong,  ive researched but cant seem to find the solution...

so i have java code that is running and im trying to call a php to get a return.  for some reason my return (this.responseText) is empty.  i also get all 3 alerts so i know its hitting that code

 

var xmlhttp;
		if (window.XMLHttpRequest)
		    xmlhttp = new XMLHttpRequest();
		else
		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

     xmlhttp.onload = function() {
		       if (this.readyState == 4 && this.status == 200) {
		         this.history += this.responseText;
                  alert('alert1: '+ this.responseText);
		       }
             };
             alert('alert2: '+ this.responseText);
		     xmlhttp.open('GET',"save_pool_history.php?action=select_past", true);
             xmlhttp.send();
              alert('alert3: '+ this.responseText);

 

here is the php code.. from what i read, a simple echo should return results.. it does go into the function, i have the log output showing it does.  also i dont want to do it inside a div tag if that is possible.

 

<?php
session_start();



if($_GET['action'] == "select_past")
{       
   output_and_log( "          action: ".$_GET['action']. " select  team1 , team2, team1score, team2score from pool_play_history  where games_id= '".$_SESSION['bracket_id']."' and user_id = '".$_SESSION["userId"]."' and  team1score is not null and team2score is not null", "log","save_pool_history", "INFO", $myFile2);
 //  query for later after i get the simple echo to work....
  //$pquery =mysqli_query($con,"select  team1 , team2, team1score, team2score from pool_play_history  where games_id= '".$_SESSION['bracket_id']."' and user_id = '".$_SESSION["userId"]."' and  team1score is not nulal and team2score is not null");
       output_and_log( "          after query select past", "log","save_pool_history", "INFO", $myFile2); 
    
      echo "past1 - past2  9:8<br>";
}




?>

 

Edited by wilsoc31
Link to comment
Share on other sites

  • Solution
2 hours ago, wilsoc31 said:
var xmlhttp;
		if (window.XMLHttpRequest)
		    xmlhttp = new XMLHttpRequest();
		else
		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

This bit of code is very antiquated.  For new development, unless you have some need to support ancient IE systems, you should either just use new XMLHttpRequest unconditionally, or preferably, use fetch()

Part of your issue is also not understanding what this means in the code I think.  this refers to whatever object a function is attached to.  In your onload function, this will refer to your xmlhttp object.  Outside of that function, this would refer to something else, so this.responseText outside the function is not the same as this.responseText inside the function.  Outside the function, you'd use xmlhttp.responseText.

My suggestion is you get rid of this code, and write your code using the modern fetch() api.  That would look something like this:

fetch('save_pool_history.php?action=select_past').then((response)=>{
    if (!response.ok){
        throw new Error('Invalid Response');
    }
    
    return response.text();
}).then((text)=>{
    alert(text);
}).catch((error)=>{
    alert('Error fetching response.');
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.