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




}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.