1

i am trying to change the select query on click or on change of the button,

the table name field names in the table general are equal to the fragment after page = (About, ContactInformation)

i'm not getting any error nor getting any result

index page code

<script>
function selectQuery(str) {
    if (str == "") {
        document.getElementById("editor").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("editor").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","queries.php?page="+str,true);
        xmlhttp.send();
    }
}
</script>

<ul>
  <li><a href="?page=About" value="About" onclick="selectQuery(this.value)"><i class="fa fa-file-text"></i>&nbsp;&nbsp;About us</a></li>
  <li><a href="?page=ContactInformation" onchange="selectQuery()"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Contact us</a></li>
</ul>

queries.php page

<?php
    $page = intval($_GET['page']);


    $result = mysql_query("SELECT general.'".$page."' ". 
    "FROM general") or trigger_error(mysql_error());

    echo $result; 

?>

i tried to echo $page in the queries.php file directly but also it is not showing, seems its not even getting here,

so can anyone help pls

karma cat
  • 51
  • 6

1 Answers1

1

You aren't fetching those results. You need to use mysql_fetch_* functions to get those rows. Now the next part is whether you would like to get a JSON response or just output an HTML markup that can be used directly.

Here's what the fetching would look like:

<?php

$page = $_GET['page'];

$data = array();
$result = mysql_query("SELECT general.".$page." FROM general") or trigger_error(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    // fetch the results
    $data[] = $row;
}

echo json_encode($data);

?>

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Here's a version with mysqli:

<?php

$default_columns = array('About', 'ContactInformation', 'Others', 'Home');

if(isset($_GET['page']) && in_array($_GET['page'], $default_columns)) {

    $return_value = array();

    $page = $_GET['page'];
    $db = new mysqli('localhost', 'username', 'password', 'database');
    $sql = "SELECT $page FROM general";
    $query = $db->query($sql);
    while($row = $query_fetch_assoc()) {
        $return_value[] = $row[$page];
    }

    echo implode(' ', $return_value);
}

?>
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • yes but i can't echo $page, its not from the database its from the other page – karma cat Dec 13 '14 at 11:36
  • @karmacat when you click that link what should happen anyway? – Kevin Dec 13 '14 at 11:38
  • change the query field name to get different results, i think the error is in the javascript in the index page can you please check it (the on click or on change ) – karma cat Dec 13 '14 at 11:45
  • @karmacat why is there an error? check the console browser. anyway try this http://pastebin.com/QuzzUSJ2 – Kevin Dec 13 '14 at 11:52