繁体   English   中英

SQL查询点击按钮

[英]Sql query on clicked button

我想问一下按下HTML按钮时如何调用Sql查询。 我看了几个教程,但从未使它正常工作,这就是为什么我想在这里问。

所以..我有一个显示的项目列表,该列表是从数据库生成的。 代码如下所示:

<table width="100%" border="1" cellspacing="1" cellpadding="5" style="background-color:#FFC" >
<td>Id</td>
<td>Text</td>
<td>Solved?</td>
<td></td>
<?php
while( $array = mysql_fetch_assoc($queryRun) ) {
    ?><tr>
    <td><?php echo $id = $array['id'].'<br />';?></td>
    <td><?php echo $text = $array['text'].'<br />';?></td>
    <td><?php echo $reseno = $array['solved'].'<br />';?></td>
    <td><input type="button" value="Solve it" /></td>
    </tr><?php
}?>
</table>

现在,在此循环内生成的每个项目在表的末尾都有一个按钮“ Solved”,我想创建一个按钮,以便当用户单击该按钮时,执行一个Sql查询,将Solved的值更改为0(false )到1(true)。

您需要ajax。 编写按钮onclick并进行ajax调用。

我没有测试过,但是应该可以。

基本上,我已经将一个类应用于任何对象,以便轻松地选择jQuery中的内容:

<?php while ($array = mysql_fetch_assoc($queryRun)) { ?>
<tr class="row">
    <td class="cell_id">
        <?php echo $array['id']; ?>
    </td>
    <td class="cell_text">
        <?php echo $array['text']; ?>
    </td>
    <td class="cell_solved">
        <?php echo $array['solved']; ?>
    </td>
    <td class="cell_solve_it">
        <input class="button_solve_it" type="button" value="Solve it" />
    </td>
</tr>
<?php } ?>

然后,我创建了这个简短的jQuery脚本:

$('.row').each(function(){
    $('.button_solve_it', this).click(function(e){
        e.preventDefault();
        $.post(
            'solve_query.php',
            {
                id: $('.cell_id', this).html(),
                text: $('.cell_text', this).html()
            },
            function (data) {
                $('.cell_solved', this).html('1');
            }
        );
    });
});

总之:当你点击一个.button_solve_it按钮,你让一个Ajax请求solve_query.php ,在那里你执行你的SQL查询。 然后,在“已Solved?设置行的内容Solved? 列设为1 (或为true或您想要的任何值)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM