0

I am puzzled as to why PHP sees my request string as undefined.

 $_GET['ask'] 
in my php file produces this error -> Notice: Undefined index: ask.

But when I query the php file from the url bar in the browser like this

localhost/Websites/webProject/data.php?ask=myquery 

I have set the php file to echo my string and it does do exactly that but only when I query it from the browser URL bar.

But when running the AJAX code normally from the parent html/php file

 request.open("GET", "data.php?ask=myquery", true); 

The PHP file does not see the query string and thinks its undefined.

Why is this the case?

I have tried to use

 $_REQUEST[]; 
but to no avail.

I am using pure javascript for the AJAX requests.

Here is the javascript

requestResponse();
function requestResponse()
{
  var READY_STATE_DONE = 4; /* request finished and response is ready */
  var SUCCESS          = 200; /* "OK" */

  setInterval(function(){
    var request = new XMLHttpRequest();
    request.onreadystatechange = function(){
      if(this.readyState == READY_STATE_DONE && this.status == SUCCESS)
      {
        var response = this.responseText;
        console.log(request.responseText);
        document.getElementById("test").innerHTML += "<br>" + response;
      }
    }
    request.open("GET", "data.php?ask=myquery", true);
    request.send();

  }, 3000)

}

Here is the PHP content

testRequest();
function testRequest()
{
  $reqString = $_REQUEST['ask'];
  include("dbCredentials.php");
  include("dbConnect.php");


  if($reqString == "myquery")
  {
    echo("<br />REQUEST IS: " . $reqString);
    echo("<br /> Your request is granted");
  }
}

DISCLOSURE: I have replaced the previous php file with data.php.

FlashspeedIfe
  • 368
  • 1
  • 6
  • 15
  • 1
    probably because you're requesting `busData.php` in your AJAX but `data.php` when you go in the browser url, or at least that's not helping. – Jhecht Feb 16 '17 at 03:38
  • 1
    post your javascript please – Sergio Alen Feb 16 '17 at 03:38
  • 3
    Your query variable name is ask, PHP return undefined index is normally. You should be use `$_GET['ask'] `, not `$_GET['key'] ` – Calos Feb 16 '17 at 03:41
  • $_GET['key']. the "key" is a placeholder for the actual key which is named "ask" I have made it more clear now. – FlashspeedIfe Feb 16 '17 at 03:45
  • you would appear to be running this in an interval -- you may want to look into [WebSockets](http://stackoverflow.com/questions/12203443/is-native-php-support-for-web-sockets-available) which could streamline that process – Jhecht Feb 16 '17 at 03:46
  • With all of this being updated, what is the content of the PHP page you are requesting? – Jhecht Feb 16 '17 at 03:50
  • I've now posted the php code @Jhecht – FlashspeedIfe Feb 16 '17 at 03:52
  • any particular reason the code is put inside a function that's called immediately? – Jhecht Feb 16 '17 at 03:54
  • @Jhecht Its because right now I'am testing the code and would like to separate this behavior from the rest of the code. – FlashspeedIfe Feb 16 '17 at 03:56
  • 2
    Are you cached? – epascarello Feb 16 '17 at 03:59
  • Hard to give an answer on why this isn't working for you -- I made two local files and put your code into them (Commented out the two includes, which shouldn't be stopping it). and it works fine for me. – Jhecht Feb 16 '17 at 04:00
  • @Jhecht did you get results without querying the php file directly through the URL bar? – FlashspeedIfe Feb 16 '17 at 04:02
  • @epascarello do you think I should clear the cache? – FlashspeedIfe Feb 16 '17 at 04:02
  • 1
    Yes, @FlashspeedIfe, I got the javascript to work. – Jhecht Feb 16 '17 at 04:04
  • 1
    @epascarello IT WORKED!! Clearing the cache did the trick. But this experience means I have to find a way to prevent the cache from disrupting code execution. – FlashspeedIfe Feb 16 '17 at 04:05
  • 1
    GET requests cache, that is how the net works. – epascarello Feb 16 '17 at 04:05
  • @epascarello yes but I dont think this should have been a problem. Users should not be having to clear their cache often to get something to work – FlashspeedIfe Feb 16 '17 at 04:08
  • @FlashspeedIfe Do you understand how the web works. You request a file. It gets stored in the cache with a time that says this content will expire. This is so you are not constantly fetching the same data. So you had a bad request stored in your console. If you do not want the item to ever be cached, you need to set the correct header or you need to use a POST request. – epascarello Feb 16 '17 at 14:17

1 Answers1

0

Try using Jquery Ajax request. this is mostly effective when you want to pass strings instead of serialized data

HTML:

    <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
<script type='text/javascript'>
    $(function(){
        $.ajax({
            type: 'post',
            url: 'data.php?ask=whut',
            success: function(response){
                alert(response);
            }
        });
    });
</script>

PHP Content:

echo $_GET['ask'];