Sending a PHP object via AJAX request

Hello,

I was just wondering what I need to do in order to send an AJAX request that contains in its target url a PHP object.

The following Admin_Template object $a is what I’d like to be sent:


class Admin_Template {

    var $num_of_content_holders = 1;
    var $num_of_side_bars = 0;

    public function add_cholder()
    {
        $this->num_of_content_holders++;
    }

    public function num_of_columns( )
    {
        return $this->num_of_content_holders +  $this->num_of_side_bars;
    }
    
}

$a = new Admin_Template();


Below is my JS code:


<script type="text/javascript">

    var xmlhttp;

    function add_cholder()
    {
        xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
        
        var url="admin.template.form.php";
        url=url+"?"; // want the php object sent here
        url=url+"&sid="+Math.random();
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
        
    }

    function stateChanged()
    {
        if (xmlhttp.readyState==4)
        {
            document.getElementById("c_holders").innerHTML=xmlhttp.responseText;
        }
    }

    function GetXmlHttpObject()
    {
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            return new XMLHttpRequest();
        }
        if (window.ActiveXObject)
        {
            // code for IE6, IE5
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }


</script>

The target php page “admin.template.form.php” is supposed to receive the php object and make changes to it.

I’ve tried to keep this post as simple (general) as possible, because I’ve just started playing with AJAX. If there’s any additional info/code needed, please let me know.

Much appreciated.

How is your JavaScript going to get a PHP object to send?? This is a strange question. You can serialize the object to a string, but JavaScript won’t be able to do anything with it, so why is AJAX involved? I don’t see why you would want to send that serialized object to the browser and back, when the “receiving” code could instantiate an Admin_Template on its own.