0

当用户单击单选按钮时,我正在尝试更新 MySQL 数据库。我打算使用 jQuery/Ajax 来完成这个。我查看了这个网站和其他网站,看看我做错了什么,但我找不到答案。

第一页代码:

<script type="text/javascript" src="http://www.njacfootball.com/styles/autofill/jquery.js"></script>
<script type="text/javascript">
  $(document).ready(UpdateRecord(gid, user, pick, wk) {
    jQuery.ajax({
      type: "POST",
      url: "savePick.php",
      data: {
        gid: gid
        usr: user
        pk: pick
        wk: wk
      },
      cache: false,
      success: function(response)
      {
        alert("Pick successfully updated/saved");
      }
    }
  });
</script>

[查询mysql数据库]

    $i=0;

while ($i < $num) {
$date=mysql_result($result,$i,"dt");
$gid=mysql_result($result,$i,"SchedID");
$wk=mysql_result($result,$i,"SchedWk");
$away=mysql_result($result,$i,"away");
$time=mysql_result($result,$i,"ftime");
$home=mysql_result($result,$i,"home");
$hscore=mysql_result($result,$i,"hscore");
$ascore=mysql_result($result,$i,"ascore");
$status=mysql_result($result,$i,"status");
$upick=mysql_result($result,$i,"Pick");
$ucor=mysql_result($result,$i,"Correct");

$formatteddate = strftime("%A %b %d, %Y", strtotime($date));
?>   
<input type="hidden" name="<? echo "gid_".$i; ?>" value="<? echo $gid; ?>" />
<table width="75%" border="0" align="center" style="border-width: 1px; border-color:#000000; border-style: solid;"><tr>
  <th width="50%"><div align="left"><span class="style8"><? echo $formatteddate; ?>  <? echo $time;?>M</span><br />
  <th width="15%"><span class="style7">Who Wins?</span></th>
<th width="25%"><span class="style7">
<? 
if ($status=='F') {
    echo "Final";  }
else {
    echo $status; } ?>

</span></th>
<th>Correct?</th>
</tr>
<tr>
<td><div align="center" class="style7">
  <div align="right"><? print "$away"; ?></div>
</div></td>
<td> <div align="center" class="style7">
    <? if ($upick==$away) { 
            if (($today==$date) && ($status=='')) { 
                if (($curtime>$time) && ($status=='')) {  ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" checked="checked" disabled="disabled" />
           <? } if (($curtime<$time) && ($status=='')) {  ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" checked="checked" onClick="UpdateRecord(<? echo $gid; ?>, <? echo $userid; ?>, '<? echo $away; ?>', <? echo $wk; ?>);" /> <? } 
                    } 
            else if (($today>$date) && ($status=='')) { ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" checked="checked" disabled="disabled" />
      <? }  else if (($today<$date) && ($status=='')) { ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" checked="checked" onClick="UpdateRecord(<? echo $gid; ?>, <? echo $userid; ?>, '<? echo $away; ?>', <? echo $wk; ?>);" />    

    <? }  }  else { 
    if (($today==$date) && ($status=='')) { 
                if (($curtime>$time) && ($status=='')) {  ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" disabled="disabled" />
               <? } if (($curtime<$time) && ($status=='')) {  ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" onClick="UpdateRecord(<? echo $gid; ?>, <? echo $userid; ?>, '<? echo $away; ?>', <? echo $wk; ?>);" /> <? } } 
    else if (($today>$date) && ($status=='')) { ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" disabled="disabled" />
             <? }  else if (($today<$date) && ($status=='')) { ?>
                    <input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" onClick="UpdateRecord(<? echo $gid; ?>, <? echo $userid; ?>, '<? echo $away; ?>', <? echo $wk; ?>);" />  

    <? }  }  
    if ($status<>'') {
        if ($upick==$away) { 
            ?><input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" checked="checked" disabled="disabled" /><?
        }
        else {
            ?><input name="pick_<? echo $i; ?>" type="radio" id="pick_<? echo $i; ?>" value="<? print $away; ?>" disabled="disabled" />
        <? }
    }
    ?>
</div></td>
<td> <div align="center"><span class="style7"><? echo $ascore; ?></span></div></td>
<td>
    <div align="center">
      <?
    if (($status=='F') && ($ucor=='Y') && ($upick==$away)) {
        ?>
      <img src="/images/check.jpg" alt="yes" height="21px" />
      <?
    }
    else if (($status=='F') && ($ucor=='N') && ($upick==$away)) {
        ?>
      <img src="/images/x.jpg" alt="no" height="21px" />    
      <?
    }
?>  

[代码继续]

第 2 页上的代码:

{connection info}

include ("../../../styles/variables.php");

    $gameid = $_POST["gid"];
    if (!$gameid) return;

    $usr = $_POST["usr"];
    if (!$usr) return;

    $upk = $_POST["pk"];
    if (!$upk) return;

    $weeknumb = $_POST["wk"];
    if (!$upk) return;

    $query="SELECT UserID, Week, GameID, Pick FROM $picktable WHERE GameID='".$gameid."' AND UserID='".$usr."'"; 
    $result=mysql_query($query) or die(mysql_error());
    $n=mysql_numrows($result);
    if ($n==0) {
        $q1="INSERT INTO $picktable (GameID, UserID, Week, Pick) VALUES ('".$upk."','".$usr."','".$weeknumb."','".$pick."')";
        $result1=mysql_query($q1) or die(mysql_error());  }  
     else {
        $old_pick=mysql_result($result,0,"Pick");
        if ($old_pick<>$upk) {
            $q2="UPDATE $picktable SET Pick='".$upk."' WHERE GameID='".$gameid."' AND UserID='".$usr."'";
            $result2=mysql_query($q2) or die(mysql_error());  } 


    mysql_close();

?>

使用 firebug,单击单选按钮时,出现以下错误:

ReferenceError:未定义 UpdateRecord [中断此错误]

UpdateRecord(448, 24, "团队", 2);

任何帮助将不胜感激。谢谢!

4

2 回答 2

0

如果没有随之而来的 HTML,我们将无济于事,因为我们不知道在哪里gid, user, pick, wk定义。您需要在 HTML 中输出它们,例如

<input class='ajaxSubmit' type="radio" gid="23892" user="myUser" pick="myPick" wk="myWk"/>

然后在单击时执行提交 ajax:

$(document).ready(function(){
    $('.ajaxSubmit').on('click',function(){
        var gid = $(this).attr('gid');
        var user = $(this).attr('user');
        var pick = $(this).attr('pick');
        var wk = $(this).attr('wk');
        $.ajax({
            type: "POST",
            url: "savePick.php",
            data: {
                gid: gid
                usr: user
                pk: pick
                wk: wk
            },
            cache: false,
            success: function(response)
            {
                alert("Pick successfully updated/saved");
            }
        }
    });
    });
    });
于 2012-09-11T03:16:26.660 回答
0

将 UpdateRecord 移出 DOM 准备就绪。然后将处理程序附加到文档就绪内的单选按钮。

var UpdateRecord = function(gid, user, pick, wk) {}

$(function(){
    $('input[type=radio]').on( "change" , function(){
        UpdateRecord(....)
    });
});
于 2012-09-11T03:09:04.983 回答