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

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

    $( "#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" ).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并从对话框中的那里获取。这是你所追求的吗?

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

Telerik单选按钮所需的字段验证器 - javascript

如何设置Telerik单选按钮所需的字段验证器?我想在按钮单击“ BtnSave”上设置必填字段验证器吗?请帮忙!<telerik:RadButton ID="radio_male" runat="server" ToggleType="Radio" AutoPostBack="fa…

表单不提交或按钮提交不起作用 - javascript

据我所知,此代码必须有效,但是我编码时却无效问题在于该表单未提交。我该如何解决?选中时,我什至没有得到复选框的值。<table id="example" class="display" cellspacing="0" width="100%"> <thead&g…

在PHP文件中调用javascript函数并在加载HTML文件之后? - javascript

我需要在我的php中调用js函数,但无法正常工作。有人可以告诉我我在做什么错吗?我该如何轻松地做到这一点?谢谢!我有三个文件:  mail.php负责发送$ _POST的内容(工作正常)。我调用我的javascript函数来切换模式,具体取决于邮件是否已发送。 <? ... $response = $sendgrid->send($email);…

如何使用PHP从动态输入字段捕获数组值? - javascript

我正在编写一个在线时间跟踪网页,允许用户将学习时间输入该系统。用户将首先输入名称,然后根据日期输入学习时间。一天中可能会有多个学习时间。以下是我第一页的编码,<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"…

使用php重新加载内容 - javascript

在对网站进行编程时,我以前使用过此代码,它可以完美工作,但是现在当我想使用一些Flash部件时,每次单击链接时,它都会重新加载所有网站。源代码: <!DOCTYPE html> <html> <head> <title>Hot King Staff</title> <meta charset=…