The question should have been:
How does the client keep control, and use the server as a servant and not just package something for the server and the server spits out a new page, leaving the original client out of the loop?
The answer is AJAX. Since I am learning PHP, I still will write the server side code in PHP. But if my PHP does generate HTML, it will send it back to the client who called it, and the client decides where to place it in its world.
I found some code online which I modified into a general purpose AJAX POST. My only thought is that this is the wrong forum. But here it is:
- // This AJAX library is taken from http://www.captain.at/howto-ajax-form-post-request.php
-
// I modified it to return the contents of the one text written by the PHP function in a passed handler.
-
var http_request = false;
-
var js_handler_pointer = null;
-
function makePOSTRequest(url, parameters, js_handler) {
-
http_request = false;
-
js_handler_pointer = js_handler;
-
if (window.XMLHttpRequest) { // Mozilla, Safari,...
-
http_request = new XMLHttpRequest();
-
if (http_request.overrideMimeType) {
-
// set type accordingly to anticipated content type
-
//http_request.overrideMimeType('text/xml');
-
http_request.overrideMimeType('text/html');
-
}
-
} else if (window.ActiveXObject) { // IE
-
try {
-
http_request = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try {
-
http_request = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e) {}
-
}
-
}
-
if (!http_request) {
-
alert('Cannot create XMLHTTP instance');
-
return false;
-
}
-
-
http_request.onreadystatechange = alertContents;
-
http_request.open('POST', url, true);
-
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
http_request.setRequestHeader("Content-length", parameters.length);
-
http_request.setRequestHeader("Connection", "close");
-
http_request.send(parameters);
-
}
-
-
function alertContents() {
-
if (http_request.readyState == 4) {
-
if (http_request.status == 200) {
-
//alert(http_request.responseText);
-
js_handler_pointer(http_request.responseText);
-
} else {
-
alert('There was a problem with the request.');
-
}
-
}
-
}
-
// How to call this is done by simply giving string to the PHP in the form:
-
// makePOSTRequest('post.php',"Name1=encodeURI(value1)&Name2=encodeURI(value2)...", js_handler);
-
-
Since this is a PHP forum, I feel obligated to include my PHP code:
[PHP]<?php
function insertsql($item, $key)
{
if (substr($key, 0, 6)=='PMAGIC')
{
global $records, $name;
$query = 'INSERT INTO DesignSession VALUES("'.$name.'",'.substr($item,1,strlen($item)-2).')';
$result = mysql_query($query) or die('Insert main failed: ' . mysql_error());
$records++;
}
}
$name=$_POST['Savename'];
$records = 0;
if ($name=="")
$responseText="Save name not entered.";
else
{
$link = mysql_connect('localhost','root','') or die('Could not connect: ' . mysql_error());
mysql_select_db('akatz712_mgs') or die('Could not select database');
$query = 'SELECT * FROM DesignSession_Background WHERE Name="'.$name.'"';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
if (mysql_num_rows($result)!=0)
{
$responseText="Save name ".$name." already used. Pick another.";
}
else
{
$backcolor = substr($_POST['backgroundcolor'],1,strlen($_POST['backgroundcolor'])-2);
$query = 'INSERT INTO DesignSession_Background VALUES("'.$name.'",'.$backcolor.',NULL)';
$result = mysql_query($query) or die('Insert background failed: ' . mysql_error());
array_walk($_POST, 'insertsql');
$responseText="Design Session ".$name." successfully saved with ".$records." design(s).";
}
mysql_close($link);
}
echo $responseText;
?>
[/PHP]