0

I am make an interactive map in D3. I stored all data in MySQL database. Since the user is able to select year to see all data, I have to pass the year to server side(php) to query about the data of the specific year.

I do not know specific function in D3.js to help us pass variables to php, so I use XMLHTTP to send information to php.
My javascript:

a = "ARGGDP"
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "database/allcountry.php?a=a", true);
xmlHttp.addEventListener("load", ajaxCallback, false);
xmlHttp.send(null);

function ajaxCallback(event){
    alert( "loaded");
}

My php

header("Content-Type: application/json");

$a = $_GET['a'];
$username = "root"; 

However, it shows "Undefined index: a". Any idea about what should I do to fix this problem?

3 Answers 3

1

You missed a ; at the end of your var "a" and didn't add the var to your url.

a = "ARGGDP";
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "database/allcountry.php?a="+a, true);
xmlHttp.addEventListener("load", ajaxCallback, false);
xmlHttp.send();

function ajaxCallback(event){
    alert( "loaded");
}

PHP

echo "<pre>";
print_r($_GET);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

1 Comment

It only prints <pre>Array ( ) in my php page. Since I want to pass a)(table name) into php, I can use variable a in my query.
1

You can use following;

function sendAjax(data) {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  } else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      alert("loaded")
    }
  }
  xmlhttp.open("GET","database/allcountry.php?a=" + data,true);
  xmlhttp.send();
}

sendAjax("ARGGDP");

In php

$a = $_GET['a'];
echo $a;

Comments

0

Your ajax request is working, the undefined index: a notice means the $_GET variable does not have a key 'a', maybe you are modifying $_GET somewhere in your code?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.