jQuery UI 对话框 - PHP 中的 Ajax

发布于 2024-08-14 15:16:16 字数 2420 浏览 13 评论 0原文

我在 PHP 文件中有一个 HTML 表单,如附加的代码片段:

当我点击“保存详细信息”按钮时,我希望页面加载 jQuery UI 模式对话框。该对话框将通过 Ajax 执行控制器操作(例如:savedetails)。 本质上,控制器操作将获取“frmEmployees”中的所有 POST 详细信息,并通过 Ajax 将更改保存到数据库。

我对加载包含 Ajax 内容的对话框的逻辑感兴趣(通过控制器操作获取所有 POST 变量,例如通过 Ajax 的“/public/empdetailcontroller”)。到目前为止,我有类似下面的 HTML 的内容。

有什么想法吗?

片段:

    <form name="frmEmployees" id="frmEmployees" method="POST" action="">
        <table>
            <tr><td>Name:</td><td><input type="text" name="empName" size="50"></td></tr>
            <tr><td>City:</td><td><input type="text" name="empCity" size="50"></td></tr>
            <tr><td>Country:</td><td><input type="text" name="empCountry" size="50"></td></tr>
            <tr><td colspan=2 align=center><input type="button" name="btnsubmit" value="Save Details"></td></tr>
        </table>
    </form>

    <div id="dialogSaveChanges"
         title="Saving.."
         style="display:none;"><p><span
           class="ui-icon
           ui-icon-info"
           style="float:left; margin:0 7px 20px 0;"
           ></span><span id="dialogText-savechanges"></span></p></div>

    <script language="JavaScript>
        $(document).ready(function() {
            $('#dialogSaveChanges').dialog({
                        autoOpen: false,
                        width: 400,
                        modal: true,
                        title: titleText,
                        closeOnEscape: false,
                        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                        resizable: false,
                buttons: {
                    Ok: function() {
                        $(this).dialog('close');
                    }
                }
            });
            $('#btnSaveChanges').click(function() {
                $('#dialogSaveChanges').dialog('open');
                $("span#dialogText-savechanges").html("Your Changes have been saved successfully.");
            });
        });
    </script>

I have an HTML form in a PHP file like the attached snippet:

When I hit the "Save Details" button, I want the page to load a jQuery UI modal dialog. That dialog will execute a controller action (ex:savedetails) through Ajax.
Essentially, the controller action will get all the POST details in "frmEmployees" and saves the changes to a database through Ajax.

I am interested in the logic to load the dialog with the Ajax content in it (Get all the POST variables through the controller action, say "/public/empdetailcontroller" via Ajax). So, far I have something like the HTML below.

Any Ideas?

Snippet:

    <form name="frmEmployees" id="frmEmployees" method="POST" action="">
        <table>
            <tr><td>Name:</td><td><input type="text" name="empName" size="50"></td></tr>
            <tr><td>City:</td><td><input type="text" name="empCity" size="50"></td></tr>
            <tr><td>Country:</td><td><input type="text" name="empCountry" size="50"></td></tr>
            <tr><td colspan=2 align=center><input type="button" name="btnsubmit" value="Save Details"></td></tr>
        </table>
    </form>

    <div id="dialogSaveChanges"
         title="Saving.."
         style="display:none;"><p><span
           class="ui-icon
           ui-icon-info"
           style="float:left; margin:0 7px 20px 0;"
           ></span><span id="dialogText-savechanges"></span></p></div>

    <script language="JavaScript>
        $(document).ready(function() {
            $('#dialogSaveChanges').dialog({
                        autoOpen: false,
                        width: 400,
                        modal: true,
                        title: titleText,
                        closeOnEscape: false,
                        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                        resizable: false,
                buttons: {
                    Ok: function() {
                        $(this).dialog('close');
                    }
                }
            });
            $('#btnSaveChanges').click(function() {
                $('#dialogSaveChanges').dialog('open');
                $("span#dialogText-savechanges").html("Your Changes have been saved successfully.");
            });
        });
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-08-21 15:16:16

您需要提交表单才能发送表单值。逻辑将遵循如下所示:

  1. 将函数(例如,submitForm)绑定到表单的提交事件,返回 false 以阻止正常(非 AJAX)表单提交。
  2. SubmitForm 函数进行 $.ajax 调用。
  3. AJAX 调用配置为在发送请求之前打开对话框(事件:beforeSend)。
  4. 对话框中填充了“正在加载...”文本/图像。
  5. Ajax 调用成功或失败(事件:成功/失败)时,对话框中将填充结果或错误消息。

代码可能如下所示:

$('#frmEmployees').submit( function() {
  $.ajax({
    url: this.attr('action'), // Make sure your form's action URL is correct.
    method: 'POST',
    data: this.serialize(), // this = $('#frmEmployees')
                            // Add hidden form inputs to send any control
                            // parameters to your server script.
    beforeSend: openDialogFunction,
    success: handleFormSuccess,
    failure: handleFormFailure
  });

  return false; // prevent normal form submission.
});

如果您像这样编程,您的页面也可以在没有 javascript 的情况下工作,只是不会有对话框。

You'll need to submit the form in order for the form values to be sent. The logic will follow something like this:

  1. Bind function (e.g., submitForm) to form's submit event, returning false to prevent normal (non-AJAX) form submission.
  2. submitForm function makes $.ajax call.
  3. AJAX call is configured to open the dialog before the the request is sent (event: beforeSend).
  4. Dialog box is populated with "Loading..." text/image.
  5. On success or failure of the ajax call (events: success/failure), the dialog box is populated with the results or an error message.

Code might look like:

$('#frmEmployees').submit( function() {
  $.ajax({
    url: this.attr('action'), // Make sure your form's action URL is correct.
    method: 'POST',
    data: this.serialize(), // this = $('#frmEmployees')
                            // Add hidden form inputs to send any control
                            // parameters to your server script.
    beforeSend: openDialogFunction,
    success: handleFormSuccess,
    failure: handleFormFailure
  });

  return false; // prevent normal form submission.
});

If you program it like this, your page will also work without javascript, it just won't have the dialog box.

旧伤还要旧人安 2024-08-21 15:16:16

不确定我完全理解你想要做什么,但让我尝试一下......

所以,你想要:

  1. 通过 AJAX 将表单详细信息发送到控制器。
  2. 将表单数据从控制器保存到DB。
  3. 成功后,将显示一个对话框,其中包含“成功”消息和已保存的项目。
  4. 出错时(您没有提到这一点),我假设您会显示替代方法,对吗?

查看 jQuery Ajax 方法

Not sure I completely understand what you are trying to do, but let me try...

So, you want to:

  1. Send form details to a controller via AJAX.
  2. Save the form data to the DB from the controller.
  3. On success, show a dialog box with a "Success" message and the saved items.
  4. On error (you didn't mention this), I assume you would display an alternate method, correct?

Look into the jQuery Ajax methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文