【问题标题】:Onclick changing the url without reloadingOnclick更改网址而不重新加载
【发布时间】:2014-12-15 12:54:23
【问题描述】:

我有这样的内容:

编辑 ID:

echo '<table>';
$row=mysql_fetch_array($query){
 echo '<tr onclick="window.location=\'index.php?id='$row['id']'\'"><td>Row1</td></tr>';
 echo '<tr onclick="window.location=\'index.php?id='$row['id']'\'"><td>Row2</td></tr>';
}
echo '</table>';

当用户按下 tr 时,将显示另一个与 http 请求匹配的表。 换句话说,我将在查询中使用 $_GET['id'] 值:

$sql="SELECT * FROM `table` WHERE `id`='".$_GET['id']."'";
$query=mysql_query($sql,$db);

通过这种方式,另一个表,包含根据用户请求的不同数据,将在单击一行时显示。它工作正常,但唯一的问题是我必须在不重新加载页面的情况下执行此操作。知道吗?我也需要一些例子,因为我是 Js、Ajax 等方面的新手。

【问题讨论】:

  • 遵循一些 ajax/js 教程,你会没事的。
  • 还要研究 SQL 注入以及如何防止它
  • 或者在你的情况下只使用一个简单的
  • table 是否需要替换为另一个table 还是要添加表格?
  • @AlexShumilov ,我必须在不重新加载页面的情况下执行此操作,并且在表和查询所在的同一页面上......

标签: javascript php http get


【解决方案1】:

我的建议是使用数据属性来设置 id。像这样的:

<?php

echo '<table id="aTable">';
 echo '<tr data-id="1"><td>Row1</td></tr>';
 echo '<tr data-id="2"><td>Row1</td></tr>';
echo '</table>';    

echo '<table id="anotherTable">';
echo '</table>';

?>

然后用 jQuery 捕捉点击事件:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>

    $(document).on('click','#aTable tr', function(e){

        var id = $(this).data('id');
        $('#anotherTable').load('index.php?id='+id);
    });

</script>

在您的索引中,您可以捕获 id 并使用以下内容回显表格:

<?php

    if(isset($_GET['id']) && is_numeric($_GET['id']))
    {
        //DONT USE mysql_*

        $mysqli = new mysqli("localhost", "user", "password", "database");

        if ($result = $mysqli->query("SELECT * FROM `table` WHERE `id`='".(int)$_GET['id']."'")) 
        {

            while ($row = $result->fetch_assoc()) {
                echo '<tr>';
                echo '<td>'.$result['column1'].'</td>';
                echo '<td>'.$result['column2'].'</td>';
                echo '</tr>';
            }       

        }
        //Add an exit because it's an AJAX call.
        exit;
    }

?>

编辑

对于动态数据属性,您可以使用如下内容:

<?php

    echo '<table id="aTable">';
    while($row = mysql_fetch_array($query))
    {
        echo '<tr data-id="'.$row['id'].'"><td>Row1</td></tr>';
    }    
    echo '</table>';

    echo '<table id="anotherTable">';
    echo '</table>';

?>  

【讨论】:

  • 如果 id 是来自数据库的请求?该值将是相对的......并且将是服务器端的
  • 如果该值是服务器端的,我不知道您的意思是不重新加载页面,因为如果它是您正在执行查询的请求,那么您已经在重新加载页面了吧?
  • 让我理解一下这个语法:var id = $(this).data('id'); , .data('id') 是来自 tr 的 data-id 吗?
  • 是的,$(this) 是被点击的tr。所以你得到了点击行的ID。
【解决方案2】:

通过 Ajax,您可以执行以下操作:

echo '<table>';
 echo '<tr><td><a href="javascript:;" data-id="1" class="load-data">Row1</a></td></tr>';
 echo '<tr><td><a href="javascript:;" data-id="2" class="load-data">Row2</a></td></tr>';
echo '</table>';

然后你的 javascript 会通过 jquery 变成这样

<script>
$('.load-data').click(function()
{
    var t = $(this);

    $.ajax({ 
        type: 'POST', 
        url: 'PAGE_TO_GET_DATA',
        data: { 'id': t.data('id') }, 
        success: function(data)
        {
            //based on the response you output to load in the content against ID = x, you can target another DOM element and output to that
            $('#output').html(data);
        }
    });
return false;
});
</script>

在您的 QUERY 字符串中,按如下方式清理您的输入:

$sql="SELECT * FROM `table` WHERE `id`='".mysql_real_escape_string($_GET['id'])."'";

附言我已经完成了这个半盲,但应该让你走上正轨

【讨论】:

  • 这样页面不会重新加载?
  • 否 - 尽管有一个 ahref 标签,javascript 最后返回 false,因此页面不会重新加载。
  • 如果 id 也是数据库请求??这将是相对的,我不知道是否可以这样做
  • 不确定您的意思。 data-id="" 的值可以是任何值,包括从数据库加载数据时要使用的 id
猜你喜欢
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 2016-03-20
  • 1970-01-01
  • 2020-06-20
相关资源
最近更新 更多