将数据变量从PHP传递到jquery ui对话框 [英] Passing data variables from PHP to jquery ui dialog

查看:91
本文介绍了将数据变量从PHP传递到jquery ui对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段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对话框:

And I want to pass the post id data variable to a 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脚本.

Problem is I'm mostly a php girl and I don't quite grasp the million JS examples I've checked already. What I'm trying to do is use the post id as a variable that comes from php that I can in turn use in my dialog to send to another php script via post.

推荐答案

您可以按以下方式更改链接后功能:

You could change your post-link function as follows:

$( ".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() {部分:

And then grab in in the "Add idea": function() { part with:

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

所以,您要做的基本上是将当前的post-id保存到#insert-answer,然后从对话框中获取它.这是你所追求的吗?

So what you are doing is basically saving the current post-id to #insert-answer and getting it from there in your dialog. Is this what you were after?

更新的代码:@charlietfl是完全正确的,var post-id是不正确的,我已经编辑了变量的名称.

UPDATED CODE: @charlietfl is absolutely right, var post-id is not correct, I have edited the name of the variable.

这篇关于将数据变量从PHP传递到jquery ui对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆