I’m not entirely sure what you are trying to do there, but it makes very little sense to do an AJAX request to the same page you are on. Usually AJAX request are made to other pages to send/retrieve data from the server.
Like, consider this code:
<!DOCTYPE html>
<head>
<title>AJAX Example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
// Add the "submit" event callback to the form
window.onload = function() {
var form = document.getElementById('main_form');
if(form.addEventListener){ // Standard method
form.addEventListener('submit', on_submit, false);
}
else if(form.attachEvent){// IE crap
form.attachEvent('onsubmit', on_submit);
}
}
// The form's "submit" callback function
function on_submit(e) {
// Fetch the form data and create a data string for the AJAX call
var raw_html = document.getElementById('text_html').value;
var post_data = 'html=' + encodeURIComponent(raw_html);
// Do an AJAX call to submit the text.
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if(ajax.readyState == 4) {
if(ajax.status == 200) {
document.getElementById('preview_box').innerHTML = ajax.responseText;
}
else {
alert('AJAX call failed!');
}
}
}
ajax.open('POST', 'target.php', true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax.send(post_data);
// Stop the form from being submitted
if(typeof event != 'undefined' && event) { // IE crap
event.returnValue = false;
}
else { // Standard method
e.preventDefault();
}
return false;
}
</script>
</head>
<body>
<form id="main_form" action="target.php" method="post">
<textarea name="text" id="text_html"></textarea>
<input type="submit">
</form>
<div>
<span>Preview</span>
<pre id="preview_box"></pre>
</div>
</body>
</html>
This code uses AJAX to send the contents of the <textarea> to a PHP script called “target.php”, and put the response from that script into the <pre> box.
Is this what you are trying to do, or am I not understanding you?
And to use $_POST you must use AJAX post not GET. Try to learn how to post via AJAX. And if you want to send the value through query string to AJAX file then you should send the values via query string and use $_GET array in your PHP: