使用Javascript更新数据库

Lea*_*Lea 2 javascript php ajax jquery

希望这将是我需要问的关于这个问题的最后一个问题..大声笑..我不能在工作解决方案上走得太远(希望如此......).参考这个问题:

使用javascript Onclick将数据传递到数据库

我试图使用JavaScript将值传递给数据库.以下是我正在使用的代码.而且只是为了视觉辅助,我已经包含了这个输出的截图.希望它有助于解释我想要实现的目标.我有的问题是javascript"投票"链接几乎什么都没做...大声笑...你点击它,没有任何反应.我希望链接文本只需更改为"您投票!" 一旦链接被点击,并且数据被发送/接收,但是警报将没有问题,只要我可以使其工作并更新数据库.

感谢大家:)

    <?php if(isset($_POST['score'])) {

mysql_query("INSERT INTO score (score_count) VALUES ($_POST[score])");
    }               $user_id = uid();
                $m = mysql_query("SELECT * FROM friends WHERE friend_user_id1 = '$user_id' AND friend_status != '0' LIMIT 15");
                while ($t = mysql_fetch_array($m))
                {
                        $fid = $t[friend_user_id2];
                        $f = mysql_query("SELECT * FROM users WHERE user_status != '' AND user_status_date != '0' AND user_id = '$fid' ORDER BY user_status_date ASC LIMIT 15") or die(mysql_error());

                        while ($rows = mysql_fetch_array($f))
                        {
    $date = parse_date($rows[user_status_date]);
                                echo "<div style='margin: 5px;'><table><tr><td valign='top' style='width:55px;'><a href='page.php?id=$rows[user_username]'>";
                                _photo($rows[user_id]);
                                echo '</a></td><td valign="top"> <a href="page.php?id='.$rows[user_username].'" class="blue"><b>'.$rows[user_username].'</b></a> - <span style="font-size:7pt;">'.$date.'</span><span style="font-size:7pt;"> - <a href="javascript:(void);" onclick="updateScore(this, correct)" class="blue">Vote</a></span>
    <br />'.$rows[user_status].'</td><td valign="top"></td></tr></table></div>';
                        }
                }
     ?>

    <script type="text/javascript">
    function updateScore(answer, correct) {
      if (answer == correct) {
    $.get('index.php', {'score': '1'}, function(d) {
        alert('Vote Accepted: ' + d);
    });

      }
    }

    </script>
Run Code Online (Sandbox Code Playgroud)

输出:

alt text http://www.freeimagehosting.net/uploads/a7185475b8.png

Pet*_*ley 10

哇,我从哪里开始.好的,我修复了你的代码.这是一个变化列表

  1. 格式化的易读性代码(这里需要一些严格的纪律)
  2. 在查询中使用它们之前清理输入(防止SQL注入)
  3. 为关联数组键查找添加了字符串分隔符(防止E_NOTICE错误)
  4. 在以HTML格式打印之前转义可能存在危险的值(阻止XSS)
  5. 删除了笨拙的echo语句,改为使用大型输出字符串的HTML模式
  6. 更新了要使用的javascript,$.post()而不是$.get()$_POST脚本顶部的数组中读取.

这是代码:

<?php

if ( isset( $_POST['score'] ) )
{
    $result = mysql_query( "INSERT INTO score (score_count) VALUES (" . mysq_real_escape_string( $_POST['score'] ) . " )" );
    echo $result ? 'Vote Succeeded' : 'Vote Failed: ' . mysql_error();
    exit;
}

$user_id = mysql_real_escape_string( uid() );
$m = mysql_query( "SELECT * FROM friends WHERE friend_user_id1 = '$user_id' AND friend_status != '0' LIMIT 15" );

while ( $t = mysql_fetch_array( $m ) )
{
    $fid = mysql_real_escape_string( $t['friend_user_id2'] );
    $f = mysql_query( "SELECT * FROM users WHERE user_status != '' AND user_status_date != '0' AND user_id = '$fid' ORDER BY user_status_date ASC LIMIT 15" ) or die ( mysql_error() );

    while ( $rows = mysql_fetch_array( $f ) )
    {
        $date = parse_date( $rows['user_status_date'] );
        ?>
        <div style="margin: 5px;">
            <table>
                <tr>
                    <td valign="top" style="width:55px;">
                        <a href="page.php?id=<?php echo escapeForHtml( $rows['user_username'] ); ?>">
                            <?php _photo( $rows['user_id'] ); ?>
                        </a>
                    </td>
                    <td valign="top">
                        <a href="page.php?id=<?php echo escapeForHtml( $rows['user_username'] ); ?>" class="blue">
                            <b><?php echo escapeForHtml( $rows['user_username'] )?></b>
                        </a> - <span style="font-size:7pt;"><?php echo escapeForHtml( $date )?></span>
                        <span style="font-size:7pt;"> - <a href="javascript:;" onclick="updateScore(this)" class="blue">Vote</a></span>
                        <br /><?php echo escapeForHtml( $rows['user_status'] ); ?></td><td valign="top">
                    </td>
                </tr>
            </table>
        </div>
        <?php 
    }
}

function escapeForHtml( $value )
{
    return htmlspecialchars( $value, ENT_COMPAT, 'UTF-8' );
}

?>

<script type="text/javascript">

function updateScore(answer, correct)
{
    if (answer == correct)
    {
        $.post('index.php', {'score': '1'}, function(d)
        {
            alert('Vote Accepted: ' + d);
        });
    }
}

</script>
Run Code Online (Sandbox Code Playgroud)

完成所有这些后,我可以清楚地看到你实际发生POST的成功条件对我来说是未知的.你比较answercorrect,但是这个代码片断并没有让我看到correct的来了.进入updateScore()函数后,我可以看到这answer是对单击的HTMLAnchorElement的引用 - 但是发送到的值的来源是什么correct

具体来说,我在这里讨论这个粗体部分

onclick ="updateScore(this,correct)"

编辑!

试试这个功能版本,在成功投票后更新链接

<script type="text/javascript">

function updateScore( answer )
{
    if ( confirm( "Are you sure?" ) )
    {
        $.post('index.php', {'score': '1'}, function(d)
        {
            alert('Vote Accepted: ' + d);
            $(answer).after("<span>You Voted!</span>").remove();
        });
    }
}

</script>
Run Code Online (Sandbox Code Playgroud)