【问题标题】:Update record without refresh all page on my site更新记录而不刷新我网站上的所有页面
【发布时间】:2012-05-28 06:33:25
【问题描述】:

如何在不刷新页面的情况下更新记录,我有一个系统,我想更新记录,在 0 和 1 之间更改其状态,以打开或关闭功能。这是我打开或关闭它的表单:

<table class="tablesorter" cellspacing="0">
    <thead>
        <tr>
            <th></th>
            <th>nane</th>
            <th>time</th>
            <th>out</th>
            <th>enter</th>
            <th>
                <div align="center">admin</div>
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php $qu_req=mysql_query( "select * from `exit_request` where `date`='$date_now' order by id desc "); while($row_req=mysql_fetch_row($qu_req)){ ?>
        <tr>
            <td>
                <input type="checkbox">
            </td>
            <td><a href="show_exit_req.php?id=<?php print($row_req[0]); ?>" class="style9" onclick="NewWindow(this.href,'name','400','300','yes');return false"><?php print($row_req[1]); ?></a>
            </td>
            <td>
                <?php print($row_req[2]); ?>
            </td>
            <td>
                <?php print($row_req[6]); ?>
            </td>
            <td>
                <?php print($row_req[7]); ?>
            </td>
            <td>
                <div align="center">
                    <input name="<?php print(" chk_exit ".$row_req[0]); ?>" type="radio" value="0" <?php if($row_req[3]==0){print( 'checked');} ?>/>
                    <label>accept</label>
                    <input name="<?php print(" chk_exit ".$row_req[0]); ?>" type="radio" value="1" <?php if($row_req[3]==1){print( 'checked');} ?>/>
                    <label>not acept</label>
                </div>
            </td>
            <td>
                <input class="alt_btn" name="send" type="submit" value="رد الادارة" />
            </td>
        </tr>
        <? } ?>
    </tbody>
</table>

更新代码

if(isset($_POST['send'])){
    $qu=mysql_query("select * from `exit_request` ");
    while($row=mysql_fetch_row($qu)){
        $id=$row[0];
        $chk_str="chk_exit".$id;
        $chk_str=$$chk_str;
        //if($chk_str==1)
        mysql_query("update `exit_request` set `accept`='$chk_str' where id=$id");
        print('<meta http-equiv="refresh" content="0;URL=index.php" />');
    }
}

【问题讨论】:

  • 当删除print('&lt;meta http-equiv="refresh" content="0;URL=index.php" /&gt;');这一行时,重定向不会发生。

标签: php jquery ajax


【解决方案1】:
【解决方案2】:

您可以使用 AJAX 请求发布您的表单序列化内容,如许多关于网络 AJAX 的教程中所见,如果您需要在请求发送到 PHP 后更新表单的内容,请尝试将 JSON 数据发送回表单并解析/update 表单,这将使您的数据更新而不更改页面。

这取决于你如何编写你的表单处理程序 您可以 jQuery 为您的 AJAX 请求查看示例文档, 另请参阅json_encode 用于表单处理的 php 方面,jQuery UI 用于制作对话框。

【讨论】:

    【解决方案3】:

    您可以找到示例 heresee this 网上有很多例子。 请在 stackoverflow 中参考此问题How do i update mysql database with ajax and php in innerhtml

    【讨论】:

      【解决方案4】:

      这是我为一个小聊天框编写的一些代码,用于提交新的聊天帖子:

      $("#shout").keypress(function(e) {
              if(e.keyCode == 13) {
                  $.ajax({
                      url: "http://example.com/api.php",
                      contentType: "text/html; charset=ISO-8859-1",
                      data: {
                          action: "shout",
                          message: $("#shout").val()
                      },
                      success: function() {
                          $("#shout").val("");
                      }
                  })
              }
          });
      

      只要您在带有 id 喊叫的输入字段上按 Enter 键,就会从输入字段中获取值,将其放入 AJAX 请求中,然后发送。另外,发送成功后会清空输入框。

      action & data 指定 URL 调用的 GET 参数(即http://example.com/api.php?action=shout&message=valueFromInputFieldGoeshere)。不过也可以用post,看看.ajax()的选项就知道了。

      希望这能让您了解如何将数据发送到服务器。

      这是相应的代码,用于检查聊天框是否有新帖子,如果是,则加载它们。

      $(document).ready(function() {
                  var lastShout;
      
                  // This one gets the timestamp of the last chat entry
                  $.ajax({
                      url: "http://example.com/api.php",
                      contentType: "text/html; charset=ISO-8859-1",
                      async: false,
                      data: {
                          action: "lastshout"
                      },
                      success: function(data) {
                          lastShout = data + 0
                      }
                  })
      
                  // This one loads the content of the chatbox containing the posts
                  $.ajax({
                      url: "http://example.com/api.php",
                      contentType: "text/html; charset=ISO-8859-1",
                      data: {
                          action: "getshouts"
                      },
                      success: function(data) {
                          $("#shouts").html(data);
                      }
                  })
      
                  // This will be executed every 5 seconds. It takes the timestamp from the beginning, asks the server again for the latest timestamp
                  // and then checks if the response timestamp is higher than the timestamp from the beginning.
                  // If so, he'll pull the chatbox content and put it into the specified div
                  setInterval(function() {
                      $.ajax({
                          url: "http://example.com/api.php",
                          contentType: "text/html; charset=ISO-8859-1",
                          async: false,
                          data: {
                              action: "lastshout"
                          },
                          success: function(data) {
                              data = data + 0
                              if(data > lastShout) {
                                  lastShout = data;
      
                                  $.ajax({
                                      url: "http://example.com/api.php",
                                      data: {
                                          action: "getshouts",
                                          init: 1
                                      },
                                      success: function(data) {
                                          if(data != "") {
                                              $("#shouts").html(data);
                                          }
                                      }
                                  })
                              }
                          }
                      })
      
      
                  }, 5000)
              })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        • 2016-02-17
        • 1970-01-01
        相关资源
        最近更新 更多