Jump to content

[SOLVED] What's wrong with this ajax script?


GuitarGod

Recommended Posts

Hi,

 

I keep getting an error when I try to use this ajax script. So far I can't tell anything wrong with it. Any help?

 

function ajaxFunction( item_list )
{
var ajaxRequest;

var items = ajaxFunction.arguments.length;
var item_array = new Array();

for ( i = 0; i < items; i++ )
{
	item_array[i] = ajaxFunction.arguments[i];
}
        	
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 not support ajax!' );
        			return false;
        		}
        	}
        }
        
        ajaxRequest.onreadystatechange = function()
        {
        	if ( ajaxRequest.readyState == 4 )
        	{
		        		
        		        			document.getElementById( 'notes' ).innerHTML = ajaxRequest.responseText;
        				}
        }




	note_param = 'note='+item_array[1]+'';

	ajaxRequest.open( 'POST', './control.php?ajax=listnotes', TRUE );
ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
ajaxRequest.setRequestHeader('Content-length', '1'); 
ajaxRequest.setRequestHeader('Connection', 'close'); 



	ajaxRequest.send( note_param );




}

the only thing I had to change was 'TRUE' to 'true':

 

<script>
function ajaxFunction( item_list )
{
   var ajaxRequest;

   var items = ajaxFunction.arguments.length;
   var item_array = new Array();

   for ( i = 0; i < items; i++ )
   {
      item_array[i] = ajaxFunction.arguments[i];
   }
   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 not support ajax!' );
            return false;
         }
      }
   }
   ajaxRequest.onreadystatechange = function()
   {
      if ( ajaxRequest.readyState == 4 )
      {
         document.getElementById( 'notes' ).innerHTML = ajaxRequest.responseText;
      }
   }
   note_param = 'note='+item_array[1]+'';
   ajaxRequest.open( 'POST', './control.php?ajax=listnotes', true );
   ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
   ajaxRequest.setRequestHeader('Content-length', '1');
   ajaxRequest.setRequestHeader('Connection', 'close');
   ajaxRequest.send( note_param );
}
</script>
<input type="button" value="TEST" onclick="ajaxFunction('first','my note')" />
<div id="notes">This is the starting text</div>

<?php
  print_r($_POST);
?>

 

if you use jQuery though, we can shorten this up a bit:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
function ajaxFunction( )
{
  $.post('control.php?ajax=listnotes',{note:ajaxFunction.arguments[1]},function(data){
    $('#notes').html(data);
  });
}
</script>
<input type="button" value="TEST" onclick="ajaxFunction('first','my note')" />
<div id="notes">This is the starting text</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.