1

I've searched everywhere but can't find an answer to this problem.

I'm writing a little ajax script but can't get the correct value of the POST request. This is the code so far:

 <textarea id="message" name="message" style="width:100%;"></textarea>
 <input value="SEND" style="border-radius: 5px 5px 5px 5px;" type = 'button' onclick = 'ajaxFunction()'/>



<script type="text/javascript">                             <!--
            //Browser Support Code
            function ajaxFunction(){
               var ajaxRequest;  // The variable that makes Ajax possible!

               try {
                  // Opera 8.0+, Firefox, Safari
                  ajaxRequest = new XMLHttpRequest();
               }catch (e) {
                  // Internet Explorer Browsers
                  try {
                     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch (e) {
                     try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                     }catch (e){
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                     }
                  }
               }

               // Create a function that will receive data 
               // sent from the server and will update
               // div section in the same page.

               ajaxRequest.onreadystatechange = function(){
                  if(ajaxRequest.readyState == 4){
                     var ajaxDisplay = document.getElementById('chbox');
                     ajaxDisplay.innerHTML = ajaxRequest.responseText;
                  }
               }

               // Now get the value from user and pass it to
               // server script.

               var message = document.getElementById('message').value;
               var queryString = message ;
               ajaxRequest.open("POST", 'chatdata.php', true);
               //ajaxRequest.send(null); 
               ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
               ajaxRequest.send('queryString');
            }
</script>



<?php
$message1 = $_REQUEST['message'];
echo $message;
?>

when i use print_r($message); to see the content of the POST value this is what i get Array ( [queryString] => ). It has no values. What could be wrong with my code? (I would have used jQuery but i'm not well grounded in it yet.)

Reggis
  • 13
  • 3
  • You need to set the parameter name. See [this answer](http://stackoverflow.com/a/9713078). – Patrick Q May 25 '16 at 20:34
  • 1
    Maybe `var queryString = "queryString=" + message;`? Remember that post data is a series of `key=value` pairs like this: `key1=value1&key2=value2&key3=value3&...` – Alejandro Iván May 25 '16 at 21:02
  • It would be beneficial to define an event handler that uses the XMLHttpRequest object, rather than define an event handler that directly instantiates an XMLHttpRequest object. – Anthony Rutledge May 25 '16 at 21:44

3 Answers3

1

I fixed some bugs and code start works:

1.

<p id="chbox"></p> <!-- ajaxDisplay need this -->

2.

ajaxRequest.send("message="+queryString); //queryString is variable so without quotes

3.

var_dump($message1); //there was message without 1
v1000
  • 341
  • 3
  • 14
  • It's good now. Before you've written `ajaxRequest.send('queryString')` instead of what you write now. Javascript interpreted 'queryString' as string instead of a variable. Now you put `ajaxRequest.send("message="+queryString);` , queryString is a variable instead of string here – thecassion May 25 '16 at 21:46
0

Here's how you would do it in jQuery - much simpler:

$('#mybutt').click(function(){
 var txt = $('#message').val();
 $.ajax({
  type: 'post',
   url: 'my_ajax_processor_file.php',
  data: 'ta=' + txt,
  success: function(d){
   if (d.length) alert(d);
  }
 });

}); //END mybutt.click
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<textarea id="message" name="message" style="width:100%;"></textarea>
<input id="mybutt" value="SEND" style="border-radius: 5px 5px 5px 5px;" type='button' />

my_ajax_processor_file.php

<?php
    $txt = $_POST['ta'];
    $out = 'You sent: ' .$txt;
    echo $out;

Here are a bunch of free 5-min video tuts for jQuery

cssyphus
  • 31,599
  • 16
  • 79
  • 97
0

The trouble is with query string. You should put ajaxRequest.send(queryString) instead of ajaxRequest.send('queryString');. Don't use query string just use the name of the variable. It should work!

thecassion
  • 467
  • 7
  • 18