AJAX script not working...

Discussion in 'JavaScript' started by Sleeping Troll, Apr 11, 2009.

  1. #1
    This is my script:

    function Handler(Button,Msg)
     {
      var FormData = "";
      if (Msg)
       {
        var DoIt=confirm(Msg)
        if (DoIt==true)
         {
           try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}
          Column = 0
          while (Column <= Columns)
           {
            switch (document.getElementById(Title[Column]).tagName)
             {
               case "DIV":
                if (document.getElementById(Title[Column]).innerHTML>"")
                 {
                   FormData = FormData + "Entry["+Column+"]=" + escape(document.getElementById(Title[Column]).innerHTML) + "&";
    	}
              break;
              case "INPUT":
              if (document.getElementById(Title[Column]).value>"")
               {
                 FormData = FormData + "Entry["+Column+"]=" +  escape(document.getElementById(Title[Column]).value) + "&";
                }
              break;
              case "TEXTAREA":
              if (document.getElementById(Title[Column]).innerHTML>"")
               {
                 FormData = FormData + "Entry["+Column+"]=" +  escape(document.getElementById(Title[Column]).innerHTML) + "&";
               }
              break;
             }
            Column = Column + 1;
           }
          FormData = FormData + "Button=" + Button + "&Sender=" + document.getElementById("Sender").value;
          xmlhttp.onreadystatechange = function()
          {
            if (xmlhttp.readyState == 4)
             {
               alert(xmlhttp.responseText);
             }
           }
          alert(FormData)
          xmlhttp.open("POST","Handler.php?"+FormData,true);
          xmlhttp.send(null);
          return false;
         }
      }
     }
    Code (markup):
    The alert confirms the string is correct, however server side post data is missing!?!

    Here is my handler code:

    <?php
    require_once('../Connections/Hawks.php');
    mysql_select_db($database_Hawks, $Hawks);
    var_dump($_POST['Entry']);
    switch ($_POST['Button'])
     {
      case "Save":
       $SQL="Show Columns From ".$_POST['Sender'];
       $ColumnTitles = mysql_query($SQL) or die("Could not connect Column Titles: " . mysql_error());
       while ($Title = mysql_fetch_assoc($ColumnTitles))
        {
         $i++;
         $SQL = "Update ".$_POST['Sender']." Set ".$Title[Field]."='".$_POST['Entry'][$i]."' Where ".$_POST['Sender']."ID = ".$_POST['Entry'][0];
         mysql_query($SQL);
        }
       break;
      case "Delete":
       $SQL = "";
       break;
     }
    ?>
    PHP:
    The page is responding...

    Can anyone spot the problem?
     
    Sleeping Troll, Apr 11, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    To send the params via POST use something like:
    
    xmlhttp.open("POST","Handler.php",true);
    
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", FormData.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(FormData);
    
    Code (markup):
    source:
    http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
     
    camjohnson95, Apr 13, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    sending the data through the querystring (like your script is doing), would use xmlhttp.open("GET"....etc..
     
    camjohnson95, Apr 13, 2009 IP