douzhen9428 2015-04-26 09:42
浏览 25
已采纳

在jquery对话框中用html表单更新mysql中的数据

I need a help in making a page where you can see data from a mysql query. Every row is echoed as a div with a unique id.

<div class="column" id="<?php echo $row['id']; ?>">

It has the same id as the data in the mysql database. Every div contains an edit button:

<a href="edit_column.php?id=<?php echo $row['id'] ?>" class="edit">Edit</a>

After clicking 'edit', a jQuery script is executed:

$('.edit').on('click', function() {
            var url = this.href;
            var dialog = $("#dialog");
            if ($("#dialog").length == 0) {
                dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
            }
            dialog.load(
                url,
                {},
                function(responseText, textStatus, XMLHttpRequest) {
                    dialog.dialog();
                }
                );
            return false;
        })

Then, a dialog box pops up with a HTML form and I even managed to put MySQL data into that form to edit it. But now I don't know how to continue. How should I continue to update the data in the database? I know how to update it with PHP and I even have a PHP script update_column.php but I don't know how to execute it from the dialog box and refresh the respective div element with updated data without refreshing the whole page in the browser. In edit_column.php, I only have the HTML form and the PHP script which returns data from the MySQL database.

  • 写回答

3条回答 默认 最新

  • doubi7346 2015-04-26 09:54
    关注

    all you have to do is to add event to submit the edit form on one of your items in the ajax form you got from the first ajax call, you will also have to save the edit id you can do it by save it to global var,

    var editId;
    $('.edit').on('click', function() {
            var url = this.href;
            editId = $(this).attr('id');
            var dialog = $("#dialog");
            if ($("#dialog").length == 0) {
                dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
            }
            dialog.load(
                url,
                {},
                function(responseText, textStatus, XMLHttpRequest) {
                    dialog.dialog();
                }
                );
            return false;
        });
    
    /*
    this is the event that will submit the ajax form
    */
        $('body').on('click', '#elementToSubmit', function() {
          formData = $('#ajaxForm').serialize();
          formData.id = editId;
          $.post('proccessEdit.php', formData, function(result) {// do something like hiding the modal or showing loading image
    
          });
        });
    

    in your php file the one which generate the dynamic form for the specific id will have to look like this

    <form id="ajaxForm">
    <!-- some inputs -->
    </form>
    <!-- this will submit the ajaxForm -->
    <a id="elementToSubmit">Save modifications</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退