将多个帖子数据发送到ajax / jquery

时间:2016-04-26 00:18:56

标签: jquery ajax

我是ajax / jquery的新手,所以我复制并修改了我现有的代码以尝试工作。我似乎无法将WO和评论都发布到我的php文件(其中包含SQL)。现在它什么也没做。使用以下内容仅允许其发送WO,但注释仍为空白。

data: 'wo='+wo,

我甚至尝试过

data: {'wo='+wo, 'Comment='+Comment},

HTML:

<div class="panel-body">
    <div class="row">
        <div class="col-lg-12">
            <div class="form-group">
                <p>WO notations appear at the top of the work order screen. Only submit useful information that can be provided to builders/centerpoint. Other importation information may include bricks, scaffles, dogs, or home owners preventing installation. Be sure to include phone numbers if available.</p>
                <label>Information</label>
                <div>
                    <input class="form-control" placeholder="Enter more information here" type="text" name="Comment[]">
                </div>
            </div>
            <button type="submit" name="notation" class="btn btn-default" onClick="AddNotation(<?php echo $_POST['results']; ?>);">Submit Button</button>
             <button type="reset" class="btn btn-default">Reset Button</button>
         </div>
    </div>
</div>  

Jquery的:

<!-- Notation -->
<script>
function AddNotation(wo)
{
    jQuery.ajax({
        type: "POST",
        url: "functions/woNotation.php",
        data: {wo: wo, Comment: Comment},
        cache: false,
        success: function(response)
        {
            alert("Your notation has been added to this work order");
        }
    });
}
</script>

2 个答案:

答案 0 :(得分:1)

您需要从comment字段获取<input>值。为输入字段指定一个特定的类,如

<input class="form-control comment" placeholder="Enter more information here" type="text" name="Comment[]">

并更改提交按钮的HTML以传递对自身的引用,以便点击处理程序可以找到相关元素。

<button type="submit" name="notation" class="btn btn-default" onClick="AddNotation(this, <?php echo $_POST['results']; ?>);">Submit Button</button>

然后更改函数以获取同一行中注释的值。

function AddNotation(button, wo)
{
    var Comment = $(button).closest(".row").find(".comment").val();
    jQuery.ajax({
        type: "POST",
        url: "functions/woNotation.php",
        data: {wo: wo, Comment: Comment},
        cache: false,
        success: function(response)
        {
            alert("Your notation has been added to this work order");
        }
    });
}

答案 1 :(得分:0)

wo有一个值,因为您将wo对象作为AddNotation函数中的参数传入。

Comment为空,因为您将其分配给代码中其他任何位置都不存在的变量Comment。我假设如果您执行以下操作,您将在PHP中看到数据。

data: {
    wo: wo,
    comment: 'Comment'
}