通过AJAX从UI Dialog传递表单信息

时间:2014-03-24 00:32:43

标签: php ajax forms jquery-ui

我有以下代码:

HTML

<div id="resume-begin" style="background:#CCCCCC; display:inline-block;">
    <img src="/images/plus-plus.png" style="display:inline;" class="trigger"/>
</div>
<div class="input-resume-data" style="display:none;">
    <form id="project-form">
        <label for="resume-create-name">Resume Project Name:</label>
            <input type="text" id="resume-create-it" name="resume-create-it" class="text ui-widget-content ui-corner-all">
    </form>
</div>

的jQuery

<script>
    $(document).ready(function() {
        $('#resume-begin').click(function() {
        ('#resume-begin').hide();
        $('.input-resume-data').dialog("open");
        });
    });
</script>

点击div resume-begin后,会打开我的UI对话框,如下所示:

<script>
    $(function() {
        $( ".input-resume-data" ).dialog({
            title: 'Name your Resume Project',
            autoOpen: false,
            resizable: false,
            height: 450,
            width: 380,
            show: { effect: 'drop', direction: "up" },
            modal: true,
            draggable: true,
            buttons: {
                "Submit": function() {
            var $this = $(this);
        var string = $('#resume-create-it').serialize();
                $.ajax({
                    url: 'functions.php',
                    type: 'POST',
                    data: string,
                    success: function(data){
            alert("Project created!");
                        $this.dialog('close');
                    }
                });
            },
                Cancel: function() {
                        $( this ).dialog( "close" );
                $( "#resume-begin" ).show( "slow", function() {
                        });  //end function
                }  //end cancel button
            }  //end buttons
        });  //end dialog
    });  //end jquery function
</script>

我想要的是什么:

一旦用户在UI输入中输入文本,然后单击Submit,我想将数据发布到PHP页面,我将把它保存到数据库中。目前,如果我输入数据并推送提交,则成功功能有效,但数据不会保存在数据库中。

在我的PHP中,我尝试抓取数据:

//Sanitize the POST values
$resumeProjectname = $_POST['resume-create-it'];

我不确定我是否正确传递数据,这是通过变量&#34; string&#34;在jQuery中,以及我是否在php文件中正确检索数据。

非常感谢任何帮助。如果您有任何疑问,请与我们联系!

感谢。

2 个答案:

答案 0 :(得分:1)

从我的观点来看,我发现了两个错误

    ('#resume-begin').hide();//$ missing

和另一个

var string = $('#resume-create-it').serialize();

应该是

var string = $('#project-form').serialize();

序列化适用于表格。 https://api.jquery.com/serialize/

答案 1 :(得分:0)

要发送您的数据,请执行以下操作

data: {
    resume_name: string
},
dataType: "json"

然后在PHP中使用

访问它
$resume_name = $_POST['resume_name'];

作为最后一点,我不会调用变量string,因为string是许多语言中的关键字。起初我以为你试图告诉它你发送的数据是一个字符串。也不要从服务器data调用响应,因为您在其上方使用了两行data。使用类似response的内容。

我应该提及 您不必使用JSON。您可以使用其他数据类型,但这是您缺少的关键部分之一。您需要设置dataType,以便jQuery知道如何格式化您的响应。我个人认为,因为你开始使用JavaScript,所以使用JavaScript Object Notation是有意义的。

相关问题