0

Its been asked a million different ways but this is specific to this code. This is not a duplicate post.

index.html

<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "process.php";
    var fn = document.getElementById("s").value;
    var vars = "s="+fn;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
Domain: <input id="s" name="s" type="text">  <br><br>
<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>

process.php

<?php
if($_GET["s"])
{
echo '<table>';
  echo '<thead>';
    echo '<tr>';
      echo '<th width="200">CLASS</th>';
      echo '<th width="150">HOST</th>';
      echo '<th width="150">TYPE</th>';
      echo '<th width="150">TTL</th>';
      echo '<th width="150">TARGET</th>';
      echo '</tr>';
      echo '</thead>';
  echo '<tbody>';

$result = dns_get_record($_GET['s'], DNS_ALL, $authns, $addtl);
foreach($result as $key=>$value){
    echo '<tr>';
    echo '<td>' . $value['class'] . '</td>';
    echo '<td>' . $value['host'] . '</td>';
    echo '<td>' . $value['type'] . '</td>';
    echo '<td>' . $value['ttl'] . '</td>';
    echo '<td>' . $value['target'] . $value['ip'] . '</td>';
    echo '</tr>';

}

  echo '</tbody>';
  echo '</table>';
}
?>

The Problem

I can't get process.php to return the echo? If you access process.php?s=google.com DIRECTLY then it works fine and echo's the response without a hitch. Figures it probably something small but I can't figure it our can someone please point out my mistake please? Thanks!

DEMO

Form: http://main.xfiddle.com/14eb8a38/ajax/index.html

PROOF: http://main.xfiddle.com/14eb8a38/ajax/process.php?s=google.com

  • 2
    You're sending the request with `POST`, but you're reading the parameter in `$_GET`. – Barmar Jan 01 '16 at 23:24
  • Change `$_GET['s']` to `$_POST['s']` – Barmar Jan 01 '16 at 23:25
  • Using http://php.net/manual/en/function.error-reporting.php would have told you that. – Funk Forty Niner Jan 01 '16 at 23:28
  • Yes your right, my mistake from hours upon hours of figuring this out. The issue is it on ly returns the Table Head, its not echoing the data that is being processed thru the for each statement. – Drastec Development Jan 01 '16 at 23:35
  • worked fine for me, besides other Undefined index: target and others. Problem is with `echo '' . $value['target'] . $value['ip'] . '';` that throws the Notice. How to fix that one, I couldn't say. – Funk Forty Niner Jan 01 '16 at 23:37
  • @Barmar maybe you know what's going on with ^ what I said there ^. I can't figure out why it's throwing me that, even though the rest is working fine. – Funk Forty Niner Jan 01 '16 at 23:46
  • Maybe its a server issue but the fact that its only returning the Table Head and not the data in the for each loop is what I don't understand? – Drastec Development Jan 02 '16 at 00:13
  • Did you change **all** GETs to POST? turn error reporting on `error_reporting(E_ALL); ini_set('display_errors', 1);` see if it yields anything. It should. – Funk Forty Niner Jan 02 '16 at 01:24
  • Yeah that was the issue, Thank you! I understand there may be duplicate posts and php manuals out there but people come here when they don't understand what they are reading. We aren't all full fledged programmers – Drastec Development Jan 02 '16 at 01:42
  • You're welcome. I closed the question as a duplicate and that's what we're here for, to help. Glad to hear that the matter was resolved, *cheers*. – Funk Forty Niner Jan 02 '16 at 03:21

0 Answers0