将数据变量从PHP传递到jquery ui对话框


Passing data variables from PHP to jquery ui dialog

我有这段php代码:

    <?php
$posts = new Posts();
foreach($posts->getPosts() as $post){ ?>
    <div class="post">
        <h3><a class="post-link" data-post-id="<?php echo $post['id']; ?>" href="javascript:void(0)"><?php echo $post['title']; ?></a></h3>
    </div>
<?php } ?>
<div id="insert-answer" title="Add new idea to post">
<form id="myForm" action="insertidea.php" method="post">
    <fieldset>
        <p><label for="idea">Your idea:</label>
            <input type="text" name="idea" class="idea"</p>
        <p><label for="pic">Have a pic? Paste its URL here! (optional)</label>
            <input type="text" name="pic" class="pic"></p>
        <input type="hidden"class="author" name="author" value="<?php echo $_SESSION['google_data']['id']; ?>" />
        <input type="hidden"class="forpost" name="forpost" value="<?php echo $post['id']; ?>" />
    </fieldset>
</form>

我想把post id数据变量传递给jquery ui dialog:

    $( "#insert-answer" ).dialog({
    autoOpen: false,
    modal:true,
    buttons: {
        "Add idea": function() {
            var
                forpost = $(this).data("post-id"), // HOW CAN I GET THIS???
                author = $(".author").val(),
                idea = $(".idea").val(),
                pic = $(".pic").val();
            $.post('insertidea.php',{
                forpost: forpost, author: author, idea: idea, pic: pic, action:'joined'
            });//End Post
            $(".forpost").val('');
            $(".author").val('');
            $(".idea").val('');
            $(".pic").val('');
            $(this).dialog("close");
        },
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    }
});
$( ".post-link" ).click(function() {
    $( "#insert-answer" ).dialog( "open" );
});

问题是我主要是一个php女孩,我不太掌握的百万JS的例子,我已经检查过了。我要做的是使用post id作为一个变量,来自php,我可以在我的对话框中使用,通过post发送到另一个php脚本。

您可以按如下方式更改post-link函数:

$( ".post-link" ).on('click', function() {
    var postid = $(this).data("post-id");
    var answer = $("#insert-answer");
    $(answer).data('post-id', postid);
    $(answer).dialog( "open" );
});

然后在"Add idea": function() {部分使用:

var forpost = $("#insert-answer").data("post-id"),
    author = $(".author").val(),
    idea = $(".idea").val(),
    pic = $(".pic").val();

所以你所做的基本上是保存当前的post-id#insert-answer,并从那里得到它在你的对话框。这就是你要找的吗?

UPDATED CODE: @charlietfl是绝对正确的,var post-id是不正确的,我已经编辑了变量的名称