Hello all, I'm new to PHP (please forgive the naivety of this post !) and am wondering if it is possible to write a PHP server script that will respond to an HTTPRequest. The following is an outline of of what I am trying to achieve : I am creating an interactive web site that allows users to click on (google) maps and the lat/long coordinates of each mouse click are pushed into a javascript array. This is all done client-side. I then wish for that javascript array to be sent to a server for some further processing which I do NOT wish to be done client side. I understand I can send this javascript array using HTTPRequest or XMLRequest POST method. If I am correct in that assumption, then so good so far. What I am not sure of is what happens server side, and is it possible to write a PHP script to respond to this HTTPRequest, process the javascript array and then return the result to the client in the form of an xml document ? If this is perfectly feasible to achieve with PHP, could someone give me some pointers how I write the server script ? Particularly, how would I get access to that javascript array I sent with the HTTP POST ? If anyone has/know of tutorials that might be of assistance I'd be grateful for those. I've looked through the 'PHP Manual' for some ideas, but the sections on HTTP appear to be more of a reference to me than a tutorial. Many thanks for taking time to read this, and especially so if you can give me any help. Kerry
Yes, it's possible to do this in PHP. I suggest you using some AJAX framework or library. I myself use JsHttpRequest to send requests to server scripts. I can't give you a link due to lack of posts, but you can find it in google.
there really isnt any need for a 'framework' or library. it's a total of like 5 functions you need at most. as far as returning xml goes, it's just be smipler to return some javascript object notation and eval() that. i put this together for you as an example. 3 files; ajax.js, fetch.php, and index.html. ajax.js: var numbox, input, number; function initEventHandlers() { numbox = document.getElementById("numbox"); // where you want the stuff returned to input = document.getElementById("number"); // the input } function send_data() { number = input.value; objAjax = initAjaxObj(); objAjax.open("POST","fetch.php",true); objAjax.onreadystatechange = getAjaxData; objAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); objAjax.send('n=' + escape(number)); return false; } function getAjaxData() { numbox.innerHTML = ""; if (objAjax.readyState == 4 && objAjax.status == 200) { var response; response = eval(objAjax.responseText); if(response[0].data) { numbox.appendChild(document.createTextNode(response[0].data)); } } } function initAjaxObj() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } } window.onload = initEventHandlers; Code (markup): fetch.php: <?php //db connect shit here $num = $_POST['n']; $sql = "select whatever from tbl1 where id=".$num; // send query $result = "some data here for ". $num; echo '[{data:"'.$result.'"}]'; ?> PHP: index.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript" src="ajax.js"></script> </head> <body> <h2 id="numbox"></h2> <form action="javascript://" method="post" onsubmit="send_data(); return false;"> <p> <label for="number">Enter a number: </label> <input type="text" id="number" size="4" /> </p> <p><input type="submit" value="fetch" /></p> </body> </html> HTML: hope that this helps out some and gives you a better understanding of how to go about doing it.
Thanks Ansi ! That is a good start for me. Thanks taking the time to do the example, it's most helpful.
This thing works. I've never seen ajax in such distiled form. Hey ansi, you have any website for your own programming stuffs. You should do one