ajax更新mysql字段而不刷新页面

时间:2013-11-19 22:04:09

标签: php mysql ajax

我正在尝试获取右上方有一个X按钮的声明框,单击它并删除该消息,并更新mysql查询字段而不刷新页面

HTML

<form id="myForm" action="delanno.php" method="post">
    <div class='announce-box'>
     <div class='announce-message'>$show_message</div>
      <div class='announce-delete'>
       <a class='deleter'>
        <button type='submit' id='sub' name='$show_id' style='height:35px; width:40px'>
         <img src='../css/images/delete_25.png' width='25' height='25'>
        </button>
       </a>
     </div>
    </div>
</form>

php(delanno.php)

<?php
include("conn.php");

foreach($_POST as $name => $content) { // Most people refer to $key => $value

     $something = "$name"; //ignore this, i did this for testing purposes
     $query = mysql_query("UPDATE announce SET active='disabled' WHERE id='$something' ") or die(mysql_error());
} 

?>

ajax //这应该更新php字段而不刷新或发送到另一个页面

<script type="text/javascript">
    $("#sub").click( function() {
     $.post( $("#myForm").attr("action"),
        $("#myForm :input").serializeArray(),
        function(info){ $("#result").html(info);
           });
        });

        $("#myForm").submit( function() {
          return false;
        });
</script>

更多ajax //这应该在单击删除按钮时删除announce div。

<script>
$('.deleter').on('click', function(){
  $(this).closest('.announce-box').remove();

 })
</script>

如果我删除

  

返回false

它将它发送到php页面,它显然有效。我不知道该怎么办

编辑:除非将脚本发送到php页面,否则脚本不起作用

1 个答案:

答案 0 :(得分:0)

当DOM实际准备好时,选择DOM元素的Javascript应该在ready函数内部,例如

$(document).ready(function() {
  // $('#some-id') .. etc
});

我刚刚搜索了SO以获得重复,我认为它就在那里,但我没有找到它,所以只是发布作为答案,如果其他人这样做,只需标记它。