Reggis
Reggis

Reputation: 13

Can't acess ajax post request in php

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.)

Upvotes: 1

Views: 61

Answers (3)

bujals
bujals

Reputation: 278

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

Upvotes: 1

thecassion
thecassion

Reputation: 546

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!

Upvotes: 0

cssyphus
cssyphus

Reputation: 40106

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

Upvotes: 0

Related Questions