onclick锚点标记html上的javascript [英] onclick javascript on anchor tag html

查看:143
本文介绍了onclick锚点标记html上的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在点击或更改按钮时更改选择查询,

表中的表名称字段名称等于片段之后的片段page =(About,ContactInformation)



我没有收到任何错误,也没有收到任何结果

索引页代码

 < script> 
函数selectQuery(str){
if(str ==){
document.getElementById(editor)。innerHTML =;
return;
} else {
if(window.XMLHttpRequest){
//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
} else {
//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);

xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
document.getElementById(编辑器)。innerHTML = xmlhttp.responseText;


xmlhttp.open(GET,queries.php?page =+ str,true);
xmlhttp.send();
}
}
< / script>

< ul>
< li>< a href =?page = Aboutvalue =Aboutonclick =selectQuery(this.value)>< i class =fa fa-file-text> ;< / i>& nbsp;& nbsp;关于我们< / a>< / li>
< li>< a href =?page = ContactInformationonchange =selectQuery()>< i class =fa fa-list-alt>< / i>&  & nbsp;与我们联系< / a>< / li>
< / ul>

queries.php页面

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

$ b $ result = mysql_query(SELECT general。'。$ page。'。
FROM general)或trigger_error(mysql_error());

echo $ result;

?>

我试图直接在queries.php文件中回显$页面,但也没有显示,似乎它甚至没有到达这里,

所以任何人都可以帮助解决方案

>你没有获取这些结果。您需要使用 mysql_fetch _ * 函数来获取这些行。现在接下来的部分是您是想要获得JSON响应还是只输出可直接使用的HTML标记。



以下是抓取的内容:

 <?php 

$ page = $ _GET ['page'];

$ data = array();
$ result = mysql_query(SELECT general。。$ page。FROM general)或trigger_error(mysql_error());
while($ row = mysql_fetch_assoc($ result)){
//获取结果
$ data [] = $ row;
}

echo json_encode($ data);

?>

必须注意:


请不要在新代码中使用 mysql _ * 函数 。他们不再维护并被正式弃用。请参阅 红色框 ?请改用 准备语句 ,并使用 PDO MySQLi - 这篇文章将帮助您决定哪些。如果您选择PDO,这是一个很好的教程




以下是带mysqli的版本:

 <?php 

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

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

$ return_value =阵列();

$ 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);
}

?>


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

解决方案

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);
}

?>

这篇关于onclick锚点标记html上的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆