Page 1 of 1

AJAX posted string is an array?

Posted: Fri Apr 04, 2008 1:12 pm
by enatefox
Hello,

I can not figure out how to use AJAX to post an entire form-- only a variable (i.e. one form field or variable). When 'process.php' receives the data, it says it's a STRING but when I split it, the output mysteriously ONLY states 'Array' so I can't get the data.

Here's what I have:

Code: Select all

 
<script>
function compress(){
  var catter = "";
  var elem = document.forms[0].elements[i];
 
    for(i=0; i<document.forms[0].elements.length; i++){
        catter += elem.getAttribute("name") + "=" + elem.getAttribute('value') + "&";
    }
    sendRequest(escape(catter));
}
function sendRequest(what) {
    new Ajax.Request("process.php", 
        { 
        method: 'post', 
        postBody: 'data='+what,
        onComplete: showResponse 
        });
    }
function showResponse(req){
    $('myspan').innerHTML= req.responseText;
}
</script>
 
Which is captured by 'process.php' as:

Code: Select all

 
if (isset($_POST)){
    $incoming = $_POST['data'];
    echo $incoming."<BR>";
    echo gettype($incoming)."<BR>";
    echo split("&",$incoming)."<BR>";
    echo is_string($incoming);
}
 
And gives me the most unusual response:
txt1=&txt2=&txt3=&
string
Array
1
What this means is that $incoming is a string but were I to attempt to split by the '&' (the whole point of this) it says 'Array.' This has left me with an unworkable form. I'd ideally be able to POST the whole form but if I could simply split this 'pseudo-string' I could still make this work.


**** EDIT:::::

I figured something out. Please, someone explain the reasoning behind this but I am able to access it now as an array:

Code: Select all

if (isset($_POST)){
    $incoming = $_POST['data'];
    $incoming = split("&",$incoming);
    print_r($incoming[1]);
}
By specifying the index, I can get a result. Can anyone tell me why it is being cast as an Array when it was clearly a String?