Menu
Hello,
Im just wondering if anyone knows of a way to do this with PHP.
I have an option box with a list of values corresponding to pages. If the user selects any of these pages, I would like to run a query to MySQL depending on the pageid associated with the selection, and display it without navigating away from the current page.
This will then give them the contents of the page they are trying to edit as a guide with the form beneath for any changes they wish to make. Once they make the changes the form can then navigate away from this page.
Any ideas?
Thanking you in advance
a.g.r.c
[code=html]<script src='ajax.js' type='text/javascript'></script>[/code]
and be sure to read it first):[code=php]/************************************************************************************************
* *
* CustomAjax.js by Scragar *
* *
*************************************************************************************************
* *
* see FAQ for information on usage and help. *
* *
*************************************************************************************************
* *
* This script is released with a licence that dictates: *
* *
* Any and all variations of this code fall under the same licence *
* *
* You may never charge for information or copies of this script, *
* either in it's orrigional or an modified form. *
* *
* You must not use this script, or any derivative there of to encrypt *
* or otherwise attempt to obscure information *
* *
* If requested you must provide a link to the home page of this script *
* *
*************************************************************************************************
* *
* If you choose to agree to these rules you may use or modify any code contatined *
* as detailed by the rules of the licence. This header may be removed, however *
* the licence must stay intact or all rights to this and variations of the script *
* are withdrawn. *
* *
************************************************************************************************/
/*
<script>
var SID = "<? echo ini_get("session.name")."=".session_id(); ?>&";
</script>
copy the lines above into your page to enable SIDs from PHP, if you have no idea what it is, or how to use it then the odds are you don't want it. You may want to avoid indiscriminate interpritation of query strings with this enabled.
*/
var reverseparam = false;
var defaultpage = "";
var apendqry = "";
var multiquery = false;
// do not remove next 3 lines!
var lookingfor = new Array();
var fillin = new Array();
var rptmode = new Array();
// a rptmode of 0 is to ignore all elements in the case of a repeat. DEFAULT
// a rptmode of 1 is to use only the first element.
// a rptmode of -1 is to use only the last element.
// a rptmode of 2 is to compound results in order.
// a rptmode of -2 is to compound results in reverse order.
// standard assignment of wanted variables.
lookingfor[lookingfor.length] = "";
fillin[fillin.length] = "";
rptmode[rptmode.length] = 0;
var onfailure = true;
var errofunc = false;
var onbusy = false;
var busyfunc = false;
var defaultReturn = false;
var resultfunc = false;
var loadingelement = "";
var loadingDelay = 0.75;
// anything I'm missing? let me know :P
/****************************************
* *
* end easy edit. feel free *
* to look, but I'd advise *
* you not to touch unless *
* you have a backup *
* :P *
* *
* PS. sorry if it's hard to read *
* *
****************************************/
if(!multiquery){
var ajaxResponse = false;
}
var numRs = 0;
function getxml(){
if(ajaxResponse){
if(busyfunc){
var tmp = busyfunc(ajaxResponse.status, ajaxResponse);
if(typeof(tmp) == "undefined")
return onbusy;
else
return tmp;
}else{
return onbusy;
};
};
if (window.XMLHttpRequest){ // firefox, flock, safari, konqueror, opera, netscape...
ajaxResponse = new XMLHttpRequest();
}else
if(window.ActiveXObject){ // IE
try{
ajaxResponse = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try {// more IE :( if only they could do things like normal browsers...
ajaxResponse = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
};
};
if( !ajaxResponse ){
return onfailure;
};
numRs++;
setTimeout("if(numRs > 0 && loadingelement){ document.getElementById(loadingelement).style.display = 'block';};", loadingDelay*1000);
ajaxResponse.open('GET', arguments[0], false);
ajaxResponse.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxResponse.send(null);
numRs--;
if(loadingelement && numRs == 0)
document.getElementById(loadingelement).style.display = "none";
if(ajaxResponse.status == 200){
var xmldoc = ajaxResponse.responseText;
ajaxResponse = false;
return xmldoc;
}else{
ajaxrespose = false;
return false;
};
};
function mkRequest(post, get, uri){
if(reverseparam){
if(!uri)
var tmp = "";
else
var tmp = uri;
uri = post;
post = tmp;
};
if(!uri)
var uri = defaultpage;
if(!get)
var get = "";
if(!SID)
var SID = "";
if(multiquery){
var ajaxResponse = false;
}else{
if(ajaxResponse){
if(busyfunc){
var tmp = busyfunc(ajaxResponse.status, ajaxResponse);
if(typeof(tmp) == "undefined")
return onbusy;
else
return tmp;
}else{
return onbusy;
};
};
};
if (window.XMLHttpRequest){ // firefox, flock, safari, konqueror, opera, netscape...
ajaxResponse = new XMLHttpRequest();
}else
if(window.ActiveXObject){ // IE
try{
ajaxResponse = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try {// more IE :( if only they could do things like normal browsers...
ajaxResponse = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
};
};
if (!ajaxResponse) {
return onfailure;
};
numRs++;
setTimeout("if(numRs > 0 && loadingelement){ document.getElementById(loadingelement).style.display = 'block';};", loadingDelay/1000);
ajaxResponse.open('POST', uri+"?"+SID+get, false);
ajaxResponse.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxResponse.send(apendqry+post);
numRs--;
if(loadingelement && numRs == 0)
document.getElementById(loadingelement).style.display = "none";
if(ajaxResponse.status == 200){
var xmldoc = ajaxResponse.responseText;
if(!resultfunc || resultfunc(xmldoc, ajaxResponse)){
var reg;
var j;
for(var i = 0; i < lookingfor.length; i++){
reg = new RegExp("</?"+lookingfor[i]+">");
j = xmldoc.split(reg);
if(j.length >= 3 && document.getElementById(fillin[i])){
switch(rptmode[i]){
case 2:
document.getElementById(fillin[i]).innerHTML = "";
for(var k = 1; k < j.length-1; k+=2)
document.getElementById(fillin[i]).innerHTML += j[k];
break;
case 1:
document.getElementById(fillin[i]).innerHTML = j[1];
break;
case -1:
document.getElementById(fillin[i]).innerHTML = j[j.length-2];
break;
case -2:
document.getElementById(fillin[i]).innerHTML = "";
for(var k = j.length-2; k > 0; k-=2)
document.getElementById(fillin[i]).innerHTML += j[k];
break;
default:
if(j.length == 3)
document.getElementById(fillin[i]).innerHTML = j[1];
break;
};
};
};
};
ajaxResponse = false;
return defaultReturn;
}else{
if(errofunc){
var tmp = errofunc(ajaxResponse.status, ajaxResponse);
ajaxResponse = false;
if(typeof(tmp) == "undefined")
return onfailure;
else
return tmp;
}else{
ajaxResponse = false;
return onfailure;
};
};
};
function formget(){
var strdata = "";
var x = arguments[0].getElementsByTagName("INPUT");
if(x.length != 0){
for(var i = 0; i < x.length; i++){
strdata += "&" + x[i].name + "=" + webencode(x[i].value);
};
};
var x = arguments[0].getElementsByTagName("SELECT");
if(x.length != 0){
for(var i = 0; i < x.length; i++){
strdata += "&" + x[i].name + "=" + x[i].selectedIndex;
};
};
var x = arguments[0].getElementsByTagName("TEXTAREA");
if(x.length != 0){
for(var i = 0; i < x.length; i++){
strdata += "&" + x[i].name + "=" + webencode(x[i].value);
};
};
return strdata;
};
function webencode(){
arguments[0] = escape(arguments[0]).replace('+', '%2B').replace('%20', '+');
return arguments[0].replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
};[/code]
[code=html]<a href='nojs.php' onclick='return mkRequest('meap');'>send the message meap to server</a>[/code]
[code=html]
<select name="pageid">
<option value="" selected="selected">*Choose a page</option>
<option value="">-------------</option>
<option value="1">Home</option>
<option value="">-------------</option>
<option value="2">About</option>
<option value="3">-> Mission</option>
<option value="">-------------</option>
<option value="4">Services</option>
<option value="5">-> Web design</option>
<option value="6">-> Software</option>
<option value="7">-> Graphic Design</option>
<option value="8">-> Hosting</option>
<option value="9">-> V Sec</option>
<option value="">-------------</option>
<option value="10">Portfolio</option>
<option value="11">-> Web</option>
<option value="12">-> Case Studies</option>
<option value="13">-> Software</option>
<option value="14">-> Database</option>
<option value="15">-> Flash</option>
<option value="16">-> Client List</option>
<option value="">-------------</option>
<option value="17">Enquiries</option>
</select>
[/code]
reverseparam = false;
it's URL, GET, POSTmultiquery = true;
will allow more than 1 request at once.[code=php]var onfailure = true;// if function fails follow links?
var errofunc = false; // if function fails execute a javascript function using this.
var onbusy = false; // if multirquery is false and request in process follow links?
var busyfunc = false; // if multirquery is false and request in process execute function by setting it here.
var defaultReturn = false;// when the function finishes follow the link?
var resultfunc = false;// when function is sucsessfull it runs this function if you set one.[/code]
[code=php]lookingfor[lookingfor.length] = "";// name of tag to look for
fillin[fillin.length] = ""; // ID of element to fill.
rptmode[rptmode.length] = 0; // repeat mode, as explain in function.[/code]
[code=html]<select name="pageid" onclick="mkRequest(formget(this),'','PAGE.php");">[/code]
[code=html]<element_to_search_for>
content of div or whatever that you want to fill etc.
</element_to_search_for>[/code]
[code=php]header("Content-type: application/xml");[/code]
[code=php]lookingfor[lookingfor.length] = "element";// just name of element, no < or > needed.
fillin[fillin.length] = ""; // ID of element to fill.
rptmode[rptmode.length] = 0; // repeat mode, as explain in function.[/code]
[code=php]
<?php
$pageid=$_POST['pageid'];
echo $pageid;
?>
[/code]
[code=html]<span class="style14">
<select name="pageid" onchange="mkrequest(this.options[this.selectedIndex].value);">
<option value="" selected="selected">*Choose a variable to echo</option>
<option value="">-------------</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</span>
<div id='pageid'></div>[/code]
[code=php]<?php header("Content-type: application/xml");
$pageid=$_POST['key'];
echo "<pageid>$pageid</pageid>";
?>[/code]
[CODE]
var reverseparam = false;
var defaultpage = "function.php";
var apendqry = "key=";
var multiquery = false;
// do not remove next 3 lines!
var lookingfor = new Array();
var fillin = new Array();
var rptmode = new Array();
// a rptmode of 0 is to ignore all elements in the case of a repeat. DEFAULT
// a rptmode of 1 is to use only the first element.
// a rptmode of -1 is to use only the last element.
// a rptmode of 2 is to compound results in order.
// a rptmode of -2 is to compound results in reverse order.
// standard assignment of wanted variables.
lookingfor[lookingfor.length] = "pageid";
fillin[fillin.length] = "pageid";
rptmode[rptmode.length] = 0;
[/CODE]
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/alistair/public_html/ajax/function.php:2) in <b>/home/alistair/public_html/ajax/function.php</b> on line <b>2</b><br />[/quote]
0.1.9 — BETA 5.5