【问题标题】:ajax update mysql field without refreshing pageajax更新mysql字段而不刷新页面
【发布时间】:2013-12-03 16:24:57
【问题描述】:

我正在尝试让我的通知框在右上角有一个 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>

more ajax // 这应该会在单击删除按钮时删除通知 div。

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

 })
</script>

如果我删除

返回错误

它将它发送到 php 页面,它显然可以工作。我不知道从这里做什么

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

【问题讨论】:

    标签: php mysql ajax


    【解决方案1】:

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

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

    .

    我刚刚搜索了一个副本,我认为它在那里但我没有找到它,所以只是发布作为答案,如果其他人这样做了,只需标记它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2011-12-29
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多