views:

161

answers:

4

i have a php script and i'm using ajax with it . i have a textarea form connect with the ajax class

the problem when i pass a text like ( &some text ) the function return an empty text ,i geuess that i have a problem with (&) , what is the problem here ?

here the javascript function

function sendFormData(idForm, dataSource, divID, ifLoading)
{
  var postData='';
  var strReplaceTemp;

  if(XMLHttpRequestObject)
  {
    XMLHttpRequestObject.open("POST", dataSource);
    XMLHttpRequestObject.setRequestHeader("Method", "POST " + dataSource + " HTTP/1.1");
      XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    XMLHttpRequestObject.onreadystatechange = function()
    {
      if (XMLHttpRequestObject.readyState == 4 &&
          XMLHttpRequestObject.status == 200)
      {
        try
        {
          var objDiv = document.getElementById(divID);
          objDiv.innerHTML = XMLHttpRequestObject.responseText;
        }
        catch(e){document.write("sendFormData: getElementById(divID) Error");}
      }
      else
      {
        if(ifLoading)
        {
          try
          {
            var objDiv = document.getElementById(divID);
            objDiv.innerHTML = "<img src=loading.gif>";
          }
          catch(e){document.write("sendFormData->ifLoading: getElementById(divID) Error");}
        }
      }
    }

    for(i=0; i<document.getElementById(idForm).elements.length - 1; i++)
    {
      strReplaceTemp = document.getElementById(idForm).elements[i].name;
      postData += "&aryFormData["+strReplaceTemp+"][]="+document.getElementById(idForm).elements[i].value;
    }

    postData += "&parm="+new Date().getTime();
    try
    {
      XMLHttpRequestObject.send(postData);
    }
    catch(e){document.write("sendFormData: XMLHttpRequestObject.send Error");}
  }
}
A: 

maybe the function.. But how should we know if we don't see the function :)

Ionut Staicu
i post it ........
Waseem
A: 

when i see HTML and & and problem, i look to make sure that my character encoding is all properly specified.

also, the code in your PHP script may be choking on an un/escaped '&' character.

David Alpert
+1  A: 

Make sure your & is encoded with &amp; if you're passing it using Javascript. All & need to be encoded, or some browsers can freak out a bit, and any validater will complain at you.

Nicholas Flynt
A: 

In your function, if you wrap document.getElementById(idForm).elements[i].value and even strReplaceTemp (in your postData +=) line with "encodeURI()", you won't have any issues with the data being properly received.

Eric Caron
If its not working, then your PHP is likely to blame. You could try escape or encodeURIComponent instead of encodeURI, but encodeURI should do the trick. When you throw an alert(postData) inside of your try, what does it say?
Eric Caron