-3

I need to use "POST" consisting of value and a variable structure using JavaScript. The plain text should be sent to a PHP page where it will be displayed. How should I get about this?

From what I understand according to my requirement. It needs to be something like a FROM submission, but run only using JavaScript.

document.body.innerHTML += '<form id="content" action="http://10.10.10.10/index.php" method="post"><input type="hidden" name="info" value="'+plainText+'"></form>';
document.getElementById("content").submit();

I tried this code as well.Do you have an Idea on how to display the text sent here on a PHP page?

var request = new XMLHttpRequest();
request.open("POST", "10.10.10.10/index.php", true); 
request.onreadystatechange = function () { 
    if(request.readyState === 4){ 
        if(request.status === 200 || request.status == 0){    
            request.setRequestHeader("Content-type","text/plain;charset=UTF-8");
            request.setRequestHeader("Content-length", plainText.length); 
            request.send(plainText); 
        }
    }
}
request.send(null);
jcubic
  • 61,973
  • 54
  • 229
  • 402
Cobra
  • 73
  • 1
  • 3
  • 8
  • 1
    This is too broad a question - voting to close. You should demonstrate efforts you have made to solve the solution and narrow down where you are stuck. We will gladly help but won't do all the leg-work for you. Go and read around the topic of HTML, PHP and Javascript – Rob Schmuecker Jun 04 '15 at 08:46
  • What about using a search engine? – machineaddict Jun 04 '15 at 08:46
  • Please show us what you have done. – YMomb Jun 04 '15 at 08:46
  • With all due respect @Rob I'm not expecting leg-work for my self. My intention here is not to be rude. I have updated the question. This code works fine within but when I try and run as a .js file it doesn't execute with the same result. – Cobra Jun 04 '15 at 08:56

1 Answers1

2

You need to use ajax, if you need plain javascript then you should do something like this:

var request = new XMLHttpRequest();
request.onreadystatechange = function () {
    var DONE = this.DONE || 4;
    if (this.readyState === DONE){
        alert(xhr.responseText);
    }
};
request.open('POST', 'script.php', true);
request.send("<YOUR TEXT>");

if you use jQuery then simple:

$.post('script.php', '<YOUR TEXT>', function(response) { });

and then you can read it in php using:

file_get_contents('php://input');

or (deprecated):

$GLOBALS['HTTP_RAW_POST_DATA'];
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • var request = new XMLHttpRequest(); request.open("POST", "http://10.10.10.10/index.php", true); request.onreadystatechange = function () { if(request.readyState === 4){ if(request.status === 200 || request.status == 0){ request.setRequestHeader("Content-type","text/plain;charset=UTF-8"); request.setRequestHeader("Content-length", plainText.length); request.send(plainText); }}}request.send(null);} I tried this code as well. Do you have an Idea on how to display the text sent here on a PHP page? – Cobra Jun 04 '15 at 09:03
  • @Cobra you only need one request.send with plainText and setRequestHeader and request.send need to be outside of onreadystatechange. – jcubic Jun 04 '15 at 09:07
  • @Cobra you can't display text on php page because if you use ajax, php page is invisible. If you want to have it on different php page you may want to use php session or save the text in a file and read it in other script. – jcubic Jun 04 '15 at 09:10
  • Yes I understand about the request.send(null) should be removed. But is this correct? Should I keep both setRequestHeader (s)? request.setRequestHeader("Content-type","text/plain;charset=UTF-8"); request.setRequestHeader("Content-length", plainText.length); – Cobra Jun 04 '15 at 09:12
  • @Cobra you can keep them but they need to be outside of onreadystatechange. – jcubic Jun 04 '15 at 09:15
  • I put them both and and the send outside of onreadystatechange. But when I use window.alert(data.responseText); it gives a window saying 'undefined'. Can you tell me what the php side code would be to capture and display the content? (I'm sorry about so many questions I'm new at JS and PHP) @jcubic – Cobra Jun 04 '15 at 09:22
  • @Cobra if you want the same text then you should in php script do `echo file_get_contents('php://input');` – jcubic Jun 04 '15 at 09:24
  • I did a packet capture, so im sure the plain text is being sent. This is my php side code but, the text file comes up empty with just line spaces. @jcubic – Cobra Jun 04 '15 at 09:55
  • @Cobra you don't echo anything so you don't have response text. – jcubic Jun 04 '15 at 13:46
  • I see, the echo is what gives the response text. but the php code I just showed. It's not writing the text which im posting to a text file. is there anything missing in that? @jcubic – Cobra Jun 04 '15 at 18:56