AJAX: mysql returning empty, despite there being records.

Status
Not open for further replies.

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
This code is supposed to retrieve the records in the table as xml nodes and insert the fields as child data nodes. The ajax works on my home pc, but not on http://ferrety.pcriot.com/. i've got the database server as localhost, the full username as set up on cpanel > MySQL Databases and the correct password. it goes through with the connection. it doesn't however, return errors or return the records with ParentID 0 on refresh/reset fields. Why won't it list the records? Here is the xml generator:

Code:
<?php
$q = $_GET["q"];
if (is_numeric($q)) {
    if ($q > 3 || $q < 0) {
        $q = 0;
    }
} else {
    $q = 0;
}
$sql = "";
$tsql = "";

function getchildren($level, $parentid) {
    $sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = ".$parentid." ORDER BY text_RegionDescription ASC;\n";
    $GLOBALS["sql"] .= $sql;
    $result = mysql_query($sql);
    if ($result) {
        while ($row = mysql_fetch_array($result)) {
            echo "    <region>\n";
            echo "        <regionid>" . $row["bigint_RegionID"] . "</regionid>\n";
            echo "        <regionparent>" . $row["bigint_ParentRegionID"] . "</regionparent>\n";
            echo "        <regionname>" . $row["text_RegionDescription"] . "</regionname>\n";
            echo "        <indent>" . $level . "</indent>\n";
            echo "    </region>\n";
            getchildren($level+1, $row["bigint_RegionID"]);
        }
    }
}

$conn = mysql_connect("localhost", **full dbase username**,**correct password**;
if (!$conn) {
    die("Could not connect: " . mysql_error());
}
mysql_select_db("f3retty0_ferrety", $conn);
switch ($q) {
    case 1: // add region
        $pid = $_POST["menu_parentregion"];
        $desc = $_POST["text_regionname"];
        $tsql = "INSERT INTO 1_regions (text_RegionDescription, bigint_ParentRegionID) VALUES (\"".$desc."\", ".$pid.");\n";
        $sql .= $tsql;
        $result = mysql_query($tsql);
        break;
    case 2: // modify region
        $id = $_POST["list_regions"];
        $pid = $_POST["menu_parentregion"];
        $desc = $_POST["text_regionname"];
        $tsql = "UPDATE 1_regions SET bigint_ParentRegionID = ".$pid.", text_RegionDescription = \"".$desc."\" WHERE bigint_RegionID = ".$id.";\n";
        $sql .= $tsql;
        $result = mysql_query($tsql);
        break;
    case 3: // remove region
        $id = $_POST["list_regions"];
        $tsql = "UPDATE 1_regions SET bigint_ParentRegionID = 0 WHERE bigint_ParentRegionID = ".$id.";\n";
        $sql .= $tsql;
        $result = mysql_query($tsql);
        $tsql = "DELETE FROM 1_regions WHERE bigint_RegionID = ".$id.";\n";
        $sql .= $tsql;
        $result = mysql_query($tsql);
        break;
    default: // (re)load form
}
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
echo "<root>\n";
getchildren(0, 0);
echo "    <sql>" . $sql . "</sql>\n";
echo "</root>";
mysql_close($conn);
?>
 
Last edited:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
javascript ajax and dhtml script

Here follows the javascript ajax and dhtml script:
Code:
// Regions AJAX & DHTML
// xml request
var http_request = false;
var parents = new Array();
function makeRequest(method, url, parameters) {
    http_request = false;
    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;
    url += (method=="GET")?parameters:"";
    http_request.open(method, url, true);
    if (method == "POST") {
        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((method=="GET")?null:parameters);
}
// xml parser
function alertContents() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var xmldoc = http_request.responseXML;
            var rows = xmldoc.getElementsByTagName("region");
            for (var r = 0; r < rows.length; r++) {
                var i = 0;
                var len = parseInt(rows[r].childNodes.length / 2);
                var arr = new Array(len);
                for (var c = 0; c < rows[r].childNodes.length; c++) {
                    var region = rows[r].childNodes[c];
                    if (region.childNodes.length > 0) {
                        arr[i] = region.firstChild.data;
                        i++;
                    }
                }
                addrecord("list_regions", arr);
            }
            if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild) var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql);
        } else {
            alert('There was a problem with the request.');
        }
        document.getElementById("ajaxbg").style.visibility = "hidden";
    }
}
// instantiate ajax requests
function ajaxRequest(obj) {
    document.getElementById("ajaxbg").style.visibility = "visible";
    switch (obj.value) {
        case "Add":
            if (trim(document.form_regions.text_regionname.value) == "") {
                alert("Cannot create a blank region!");
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.text_regionname.focus();
                return false;
            }
            var poststr = "menu_parentregion=" + encodeURIComponent(document.form_regions.menu_parentregion.options[document.form_regions.menu_parentregion.selectedIndex].value) +
                          "&text_regionname=" + encodeURIComponent(document.form_regions.text_regionname.value);
            makeRequest("POST", "scripts/ajax_regions.php?q=1", poststr);
            document.form_regions.list_regions.options.length = 0;
            document.form_regions.menu_parentregion.length = 1;
            document.form_regions.text_regionname.value = "";
            break;
        case "Modify":
            if (document.form_regions.list_regions.selectedIndex == -1) {
                alert("Please select a region to modify!");
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.list_regions.focus();
                return false;
            }
            if (document.form_regions.list_regions.options[document.form_regions.list_regions.selectedIndex].value.split(",")[0] == document.form_regions.menu_parentregion.options[document.form_regions.menu_parentregion.selectedIndex].value) {
                alert("Cannot set the region as it's own parent!");
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.menu_regions.focus();
                return false;
            }
            if (trim(document.form_regions.text_regionname.value) == "") {
                alert("Cannot make a region blank!");
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.text_regionname.focus();
                return false;
            }
            var poststr = "list_regions=" + encodeURIComponent(document.form_regions.list_regions.options[document.form_regions.list_regions.selectedIndex].value.split(",")[0]) +
                          "&menu_parentregion=" + encodeURIComponent(document.form_regions.menu_parentregion.options[document.form_regions.menu_parentregion.selectedIndex].value) +
                          "&text_regionname=" + encodeURIComponent(trim(document.form_regions.text_regionname.value));
            makeRequest("POST", "scripts/ajax_regions.php?q=2", poststr);
            document.form_regions.list_regions.options.length = 0;
            document.form_regions.menu_parentregion.length = 1;
            document.form_regions.text_regionname.value = "";
            break;
        case "Remove":
            if (document.form_regions.list_regions.selectedIndex == -1) {
                alert("Please select a region to remove!");
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.list_regions.focus();
                return false;
            }
            var l = document.form_regions.list_regions.selectedIndex;
            var t = ltrim(document.form_regions.list_regions.options[l].text);
            if (!confirm("Are you sure you want to remove " + t + "?")) {
                document.getElementById("ajaxbg").style.visibility = "hidden";
                document.form_regions.list_regions.focus();
                return false;
            }
            var poststr = "list_regions=" + encodeURIComponent(document.form_regions.list_regions.options[document.form_regions.list_regions.selectedIndex].value.split(",")[0]);
            makeRequest("POST", "scripts/ajax_regions.php?q=3", poststr);
            document.form_regions.list_regions.options.length = 0;
            document.form_regions.menu_parentregion.length = 1;
            document.form_regions.text_regionname.value = "";
            break;
        default:
            makeRequest("GET", "scripts/ajax_regions.php?q=0", "");
            document.form_regions.list_regions.options.length = 0;
            document.form_regions.menu_parentregion.length = 1;
            document.form_regions.text_regionname.value = "";
    }
}
// string trim functions
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
    if (chars == undefined) var chars = "";
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
    if (chars == undefined) var chars = "";
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
// leftpadding for tree display
function strRepeat(str, len) {
    var ret = "";
    for ($i = 0; $i < len; $i++) {
        ret += str;
    }
    return ret;
}
// dynamic updater
function addrecord(id, arr) {
    var opt1 = document.createElement('option');
    var opt2 = document.createElement('option');
    //alert(arr[2]);
    opt1.text = strRepeat("\u00a0\u00a0\u00a0\u00a0\u00a0", arr[3]) + arr[2];
    opt2.text = strRepeat("\u00a0\u00a0\u00a0\u00a0\u00a0", arr[3]) + arr[2];
    opt1.value = arr[0] + "," + arr[1];
    opt2.value = arr[0];
    var sel1 = document.getElementById("list_regions");
    var sel2 = document.getElementById("menu_parentregion");
    try {
        sel1.add(opt1, null); // standards compliant; doesn't work in IE
        sel2.add(opt2, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        sel1add(opt); // IE only
        sel2.add(opt); // IE only
    }
}
// onload event handlers
window.onload = function () {
    return ajaxRequest(document.form_regions.button_reset);
}
// onselect event handlers
document.form_regions.list_regions.onchange = function () {
    var val = this.options[this.selectedIndex].value.split(",");
    document.getElementById("text_regionname").value = ltrim(this.options[this.selectedIndex].text,"");
    for (var i = 0; i < document.getElementById("menu_parentregion").options.length; i++) {
        if (document.getElementById("menu_parentregion").options[i].value == val[1]) {
            document.getElementById("menu_parentregion").selectedIndex = i;
        }
    }
}
Edit:
PS: this is for the REGION MANAGER page available at http://ferrety.pcriot.com/?p=1
Edit:
nevermind, problem sorted - i had the database name wrong. -_-
 
Last edited:

dpilmore

New Member
Messages
109
Reaction score
1
Points
0
change it from localhost to this:

mysql-<yourserver>.x10hosting.com

that is the mysql server address
<yoursever>= well mine is chopin but put yours there

another thing mate, your link goes straight to your website admin bit.
 
Last edited:
Status
Not open for further replies.
Top