Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Can't get it to work. It is complicated to describe the whole project since it's 10 files php only.
If you could get this to work i would really appreciate it.

JavaScript
function getText() {
	var $a =	document.getElementById('text').value;
		xhr = new XMLHttpRequest();
		xhr.open('POST' , 'chatdb.php',true);
		xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
		xhr.send('chat='+$a);
		xhr.onreadystatechange=function(){
			if (xhr.responseText){
			//	document.getElementById('chatarea').innerHTML=xhr.responseText;
									}
				}
}


What I have tried:

I've tried this:

JavaScript
function getText(){
    var a =	document.getElementById('text').value;
    fetch ('chatdb.php', {method: 'post'})
        .then (response => response.text())
        .then (responseText => {
            document.getElementById('text').innerHTML = responseText;
        })
        .catch (console.error);
}
Posted
Updated 21-Sep-18 8:06am
Comments
Richard MacCutchan 21-Sep-18 9:41am    
"it's 10 files php only"
So why have you tagged it JavaSE6?
Mehdi Gholam 21-Sep-18 12:06pm    
Your code is javascript and not php.

1 solution

You're not sending the POST data:
JavaScript
function getText(){
    var a = document.getElementById('text').value;
    fetch ('chatdb.php', {
        method: 'POST',
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: "chat=" + encodeURIComponent(a)
    })
    .then (response => response.text())
    .then (responseText => {
        document.getElementById('text').innerHTML = responseText;
    })
    .catch (console.error);
}

Using Fetch - Web APIs | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900